using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.SearchModels;
using VetClinicContracts.StoragesContracts;
using VetClinicContracts.ViewModels;
using VetClinicDataBaseImplement.Models;

namespace VetClinicDataBaseImplement.Implements
{
	public class VisitStorage : IVisitStorage
	{
		public List<VisitViewModel> GetFullList()
		{
			using var context = new VetClinicDatabase();

			return context.Visits
				
					 .Include(x => x.Admin)
			 
				 .Include(x => x.Services)
			.ThenInclude(x => x.Service)
					.Select(x => x.GetViewModel)
					.ToList();
		}
		public List<VisitViewModel> GetFilteredList(VisitSearchModel model)
		{
			using var context = new VetClinicDatabase();
			return context.Visits.Include(x => x.Admin).Include(x => x.Services)
			.ThenInclude(x => x.Service).Include(x => x.Animals).ThenInclude(x => x.Animal)
			.Where(x => (string.IsNullOrEmpty(model.NameVisit) || x.NameVisit.Contains(model.NameVisit))
			&& (!model.AdminId.HasValue || x.AdminId == model.AdminId))
			.Select(x => x.GetViewModel)
			.ToList();
		}

		public List<Tuple<VisitViewModel, List<Tuple<AnimalViewModel, List<VaccinationViewModel>>>>> GetVaccinationsInfo(MedicineVaccinationsSearchModel model)
		{
			if (model.visitsIds == null)
			{
				return new();
			}
			using var context = new VetClinicDatabase();
			return context.Visits
				.Where(visit => model.visitsIds.Contains(visit.Id))
				.Select(visit => new Tuple<VisitViewModel, List<Tuple<AnimalViewModel, List<VaccinationViewModel>>>>(visit.GetViewModel,
				context.VisitAnimals.Include(animal => animal.Animal)
				.Include(animal => animal.Visit).Where(animal => visit.Id == animal.VisitId).
				Select(animal => new Tuple<AnimalViewModel, List<VaccinationViewModel>>(animal.Animal.GetViewModel,
				 context.Vaccinations.Include(x => x.Animal).Where(x => x.AnimalId == animal.AnimalId && x.DateStamp >= model.DateFrom && x.DateStamp <= model.DateTo).
				Select(x => x.GetViewModel).ToList())).ToList())).ToList();

		}
		public List<Tuple<VisitViewModel, List<Tuple<AnimalViewModel, List<MedicineViewModel>>>>> GetMedicinesInfo(MedicineVaccinationsSearchModel model)
		{
			if (model.visitsIds == null)
			{
				return new();
			}
			using var context = new VetClinicDatabase();
			return context.Visits
				.Where(visit => model.visitsIds.Contains(visit.Id))
				.Select(visit => new Tuple<VisitViewModel, List<Tuple<AnimalViewModel, List<MedicineViewModel>>>>(visit.GetViewModel,
				context.VisitAnimals.Include(animal => animal.Animal)
				.Include(animal => animal.Visit).Where(animal => visit.Id == animal.VisitId).
				Select(animal => new Tuple<AnimalViewModel, List<MedicineViewModel>>(animal.Animal.GetViewModel,
				 context.MedicineAnimals.Include(x => x.Medicine).Where(x => x.AnimalId == animal.AnimalId).
				Select(x => x.Medicine.GetViewModel).ToList())).ToList())).ToList();

		}


		public VisitViewModel? GetElement(VisitSearchModel model)
		{
			if (!model.Id.HasValue)
			{
				return null;
			}
			using var context = new VetClinicDatabase();
			return context.Visits
				 .Include(x => x.Admin)
						 .Include(x => x.Services)
			.ThenInclude(x => x.Service)
			 .FirstOrDefault(x => (!string.IsNullOrEmpty(model.NameVisit) &&
			x.NameVisit == model.NameVisit) ||
			(model.Id.HasValue && x.Id ==
			model.Id))
			?.GetViewModel;
		}
		public VisitViewModel? Insert(VisitBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var newVisit = Visit.Create(context, model);
			if (newVisit == null)
			{
				return null;
			}
			context.Visits.Add(newVisit);
			context.SaveChanges();
			return newVisit.GetViewModel;
		}
		public VisitViewModel? Update(VisitBindingModel model)
		{
			using var context = new VetClinicDatabase();
			using var transaction = context.Database.BeginTransaction();

			try
			{
				var visit = context.Visits.FirstOrDefault(rec => rec.Id == model.Id);
				if (visit == null)
				{
					return null;
				}
				visit.Update(model);
				context.SaveChanges();
				if (model.ServiceVisits != null)
					visit.UpdateServices(context, model);
				transaction.Commit();
				return visit.GetViewModel;
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}
		public VisitViewModel? Delete(VisitBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var element = context.Visits
	.Include(x => x.Admin)
		.Include(x => x.Services).ThenInclude(x => x.Service)
		.FirstOrDefault(rec => rec.Id == model.Id);
			
			if (element != null)
			{

				context.Visits.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}

