using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
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 ServiceStorage : IServiceStorage
	{
		public List<ServiceViewModel> GetFullList()
		{
			using var context = new VetClinicDatabase();
			return context.Services.Include(x => x.Pharmacist)
			 .Include(x => x.Medicines)
			.ThenInclude(x => x.Medicine)
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
		{
			using var context = new VetClinicDatabase();
			return context.Services.Include(x => x.Pharmacist)
			 .Include(x => x.Medicines)
			.ThenInclude(x => x.Medicine)
			.Where(x => (string.IsNullOrEmpty(model.ServiceName) || x.ServiceName.Contains(model.ServiceName))
			&& (!model.PharmacistId.HasValue || x.PharmacistId == model.PharmacistId))
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public ServiceViewModel? GetElement(ServiceSearchModel model)
		{
			if (string.IsNullOrEmpty(model.ServiceName) &&
			!model.Id.HasValue)
			{
				return null;
			}
			using var context = new VetClinicDatabase();
			return context.Services.Include(x => x.Pharmacist)
			 .Include(x => x.Medicines)
			.ThenInclude(x => x.Medicine)
			 .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ServiceName) &&
			x.ServiceName == model.ServiceName) ||
			(model.Id.HasValue && x.Id ==
			model.Id))
			?.GetViewModel;
		}
		public ServiceViewModel? Insert(ServiceBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var newService = Service.Create(context, model);
			if (newService == null)
			{
				return null;
			}
			context.Services.Add(newService);
			context.SaveChanges();
			return newService.GetViewModel;
		}
		public ServiceViewModel? Update(ServiceBindingModel model)
		{
			using var context = new VetClinicDatabase();
			using var transaction = context.Database.BeginTransaction();
			try
			{
				var iceCream = context.Services.Include(x => x.Pharmacist)
					.Include(x => x.Medicines).ThenInclude(x => x.Medicine).FirstOrDefault(rec =>
				rec.Id == model.Id);
				if (iceCream == null)
				{
					return null;
				}
				iceCream.Update(model);
				context.SaveChanges();
				iceCream.UpdateMedicines(context, model);
				transaction.Commit();
				return iceCream.GetViewModel;
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}
		public ServiceViewModel? Delete(ServiceBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var element = context.Services.Include(x => x.Pharmacist)
			 .Include(x => x.Medicines)
			.ThenInclude(x => x.Medicine)
			.FirstOrDefault(rec => rec.Id == model.Id);
			if (element != null)
			{
				context.Services.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}
