using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using VetClinicDataBaseImplement;
using System.Linq;

namespace VetClinicBaseImplement.Implements
{
	public class AnimalStorage : IAnimalStorage
	{
		public List<AnimalViewModel> GetFullList()
		{
			using var context = new VetClinicDatabase();

			return context.Animals
					 .Include(x => x.Admin)
					 .Include(x => x.Visits)
			.ThenInclude(x => x.Visit)
					.Select(x => x.GetViewModel)
					.ToList();
		}
		public List<AnimalViewModel> GetFilteredList(AnimalSearchModel model)
		{

			using var context = new VetClinicDatabase();
			return context.Animals.Include(x => x.Admin).Include(x => x.Visits)
			.ThenInclude(x => x.Visit).Include(x => x.Medicines).ThenInclude(x => x.Medicine)
			.Where(x => (string.IsNullOrEmpty(model.AnimalName) || x.AnimalName.Contains(model.AnimalName))
			&& (!model.AdminId.HasValue || x.AdminId == model.AdminId))
			.Select(x => x.GetViewModel)
			.ToList();
		}

		public List<Tuple<AnimalViewModel, List<Tuple<MedicineViewModel, List<ServiceViewModel>>>>> GetReportInfo(ListServicesSearchModel model)
		{
			if (model.animalsIds == null)
			{
				return new();
			}
			using var context = new VetClinicDatabase();
			return context.Animals
				.Where(animal => model.animalsIds.Contains(animal.Id))
				.Select(animal => new Tuple<AnimalViewModel, List<Tuple<MedicineViewModel, List<ServiceViewModel>>>>(animal.GetViewModel,
				context.MedicineAnimals.Include(medicine => medicine.Medicine)
				.Include(medicine => medicine.Animal).Where(medicine => animal.Id == medicine.AnimalId).
				Select(medicine => new Tuple<MedicineViewModel, List<ServiceViewModel>>(medicine.Medicine.GetViewModel,
				 context.ServiceMedicines.Include(x => x.Service).Where(x => x.MedicineId == medicine.Medicine.Id).
				Select(x => x.Service.GetViewModel).ToList())).ToList())).ToList();

		}
		public AnimalViewModel? GetElement(AnimalSearchModel model)
		{
			if (!model.Id.HasValue)
			{
				return null;
			}
			using var context = new VetClinicDatabase();
			return context.Animals
				 .Include(x => x.Admin)
				  .Include(x => x.Visits)
			.ThenInclude(x => x.Visit)
			.Include(x => x.Vaccinations)
			 .FirstOrDefault(x => (!string.IsNullOrEmpty(model.AnimalName) && x.AnimalName == model.AnimalName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
		}
		public AnimalViewModel? Insert(AnimalBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var newAnimal = Animal.Create(context,model);
			if (newAnimal == null)
			{
				return null;
			}
			context.Animals.Add(newAnimal);

			context.SaveChanges();
			return newAnimal.GetViewModel;
		}
		public AnimalViewModel? Update(AnimalBindingModel model)
		{
			using var context = new VetClinicDatabase();
			using var transaction = context.Database.BeginTransaction();
		
			try
			{
				var animal = context.Animals.Include(x => x.Admin)
				 .Include(x => x.Visits)
				 .ThenInclude(x => x.Visit).FirstOrDefault(rec =>
					rec.Id == model.Id);
				if (animal == null)
				{
					return null;
				}
				animal.Update(model);
				context.SaveChanges();
				if (model.VisitAnimals != null)
				{
					animal.UpdateVisits(context, model);
				}
					
				transaction.Commit();
				return animal.GetViewModel;
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}
		public AnimalViewModel? Delete(AnimalBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var element = context.Animals
					.Include(x => x.Vaccinations)
						.Include(x => x.Visits).ThenInclude(x => x.Visit)
			.FirstOrDefault(rec => rec.Id == model.Id);

			if (element != null)
			{
				context.Animals.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}
