using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;

namespace VeterinaryDatabaseImplement.Implements
{
	public class PetStorage : IPetStorage
	{
		public List<PetViewModel> GetFullList()
		{
			using var context = new VeterinaryDatabase();
			return context.Pets
			.Select(x => x.GetViewModel)
		   .ToList();
		}
		public List<PetViewModel> GetFilteredList(PetSearchModel model)
		{

			using var context = new VeterinaryDatabase();
			return context.Pets.Where(x => x.OwnerId == model.OwnerId)
			.Where(x => String.IsNullOrEmpty(model.PetName) || x.PetName.Contains(model.PetName))
		   .Select(x => x.GetViewModel)
		   .ToList();
		}
		public PetViewModel? GetElement(PetSearchModel model)
		{
			if (string.IsNullOrEmpty(model.PetName) && !model.Id.HasValue)
			{
				return null;
			}
			using var context = new VeterinaryDatabase();
			return context.Pets
			.FirstOrDefault(x =>
		   (!string.IsNullOrEmpty(model.PetName) && x.PetName ==
		   model.PetName) ||
			(model.Id.HasValue && x.Id == model.Id))
			?.GetViewModel;
		}
		public List<ReportServicesViewModel> GetReportServices(ReportServicesSearchModel model)
		{
			if (model.petsIds == null)
			{
				return new();
			}
			using var context = new VeterinaryDatabase();
			return context.Pets
				.Where(pet => model.petsIds == null || model.petsIds.Contains(pet.Id))
				.Select(pet => new ReportServicesViewModel()
				{
					PetName = pet.PetName,
					Services = context.Services.Include(service => service.Visit).ThenInclude(visit => visit.Pets)
				.Where(service => service.Visit!=null&&service.Visit.Pets.Any(p => p.PetId == pet.Id))
				.Select(service => service.GetViewModel).ToList()
				}).ToList();
		}
		public List<ReportVisitsDrugsViewModel> GetReportVisitsDrugs(ReportVisitsDrugsSearchModel model)
		{
			using var context = new VeterinaryDatabase();

			return context.Pets
			  .Select(pet => new ReportVisitsDrugsViewModel()
			  {
				  PetName = pet.PetName,
				  Visits = context.Visits
				  .Where(visit => visit.DateVisit <= model.DateTo &&
					visit.DateVisit >= model.DateFrom && visit.Pets.Any(p => p.PetId == pet.Id))
				  .Select(visit => visit.GetViewModel)
				  .ToList(),
				  Drugs = context.Purchases
				  .Include(purchase => purchase.Drug)
				  .Where(purchase => purchase.DateCreate <= model.DateTo &&
					purchase.DateCreate >= model.DateFrom && purchase.Pets.Any(p => p.PetId == pet.Id))
				  .Select(purchase => purchase.Drug.GetViewModel)
				  .ToList(),
			  })
			  .ToList();
		}
		public PetViewModel? Insert(PetBindingModel model)
		{
			var newPet = Pet.Create(model);
			if (newPet == null)
			{
				return null;
			}
			using var context = new VeterinaryDatabase();
			context.Pets.Add(newPet);
			context.SaveChanges();
			return newPet.GetViewModel;
		}
		public PetViewModel? Update(PetBindingModel model)
		{
			using var context = new VeterinaryDatabase();
			var pet = context.Pets.FirstOrDefault(x => x.Id ==model.Id);
			if (pet == null)
			{
				return null;
			}
			pet.Update(model);
			context.SaveChanges();
			return pet.GetViewModel;
		}
		public PetViewModel? Delete(PetBindingModel model)
		{
			using var context = new VeterinaryDatabase();
			var element = context.Pets.FirstOrDefault(rec => rec.Id ==
		   model.Id);
			if (element != null)
			{
				context.Pets.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}
