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.Animals)
			.ThenInclude(x => x.Animal)
				 .Include(x => x.Services)
			.ThenInclude(x => x.Service)
					.Select(x => x.GetViewModel)
					.ToList();
		}
		public List<VisitViewModel> GetFilteredList(VisitSearchModel model)
		{

			if (string.IsNullOrEmpty(model.NameVisit))
			{
				return new();
			}
			using var context = new VetClinicDatabase();
			return context.Visits
			 .Include(x => x.Admin)
			 .Include(x => x.Animals)
			.ThenInclude(x => x.Animal)
			.Where(x => x.NameVisit.Contains(model.NameVisit))
			 .Include(x => x.Services)
			.ThenInclude(x => x.Service)
			.Where(x => x.NameVisit.Contains(model.NameVisit))
			.ToList()
			.Select(x => x.GetViewModel).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.Animals)
			.ThenInclude(x => x.Animal)
						 .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();
				visit.UpdateAnimals(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.Animals).ThenInclude(x => x.Animal)
		.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;
		}
	}
}

