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 MedicineStorage : IMedicineStorage
	{
		public List<MedicineViewModel> GetFullList()
		{
			using var context = new VetClinicDatabase();
			return context.Medicines.Include(x => x.Pharmacist).Include(x => x.Animals)
			.ThenInclude(x => x.Animal)
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<MedicineViewModel> GetFilteredList(MedicineSearchModel model)
		{
			using var context = new VetClinicDatabase();
			return context.Medicines.Include(x => x.Pharmacist).Include(x => x.Animals)
			.ThenInclude(x => x.Animal)
			.Where(x => (string.IsNullOrEmpty(model.MedicineName) || x.MedicineName.Contains(model.MedicineName))
			&& (!model.PharmacistId.HasValue || x.PharmacistId == model.PharmacistId))
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public MedicineViewModel? GetElement(MedicineSearchModel model)
		{
			if (string.IsNullOrEmpty(model.MedicineName) &&
			!model.Id.HasValue)
			{
				return null;
			}
			using var context = new VetClinicDatabase();
			return context.Medicines.Include(x => x.Pharmacist)
			 .Include(x => x.Animals)
			 .ThenInclude(x => x.Animal)
			 .FirstOrDefault(x => (!string.IsNullOrEmpty(model.MedicineName) &&
			x.MedicineName == model.MedicineName) ||
			(model.Id.HasValue && x.Id ==
			model.Id))
			?.GetViewModel;
		}
		public MedicineViewModel? Insert(MedicineBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var newMedicine = Medicine.Create(context, model);
			if (newMedicine == null)
			{
				return null;
			}
			context.Medicines.Add(newMedicine);
			context.SaveChanges();
			return newMedicine.GetViewModel;
		}
		public MedicineViewModel? Update(MedicineBindingModel model)
		{
			using var context = new VetClinicDatabase();
			using var transaction = context.Database.BeginTransaction();
			try
			{
				var medicine = context.Medicines.Include(x => x.Pharmacist)
			 .Include(x => x.Animals)
			 .ThenInclude(x => x.Animal).FirstOrDefault(rec =>
				rec.Id == model.Id);
				if (medicine == null)
				{
					return null;
				}
				medicine.Update(model);
				context.SaveChanges();
				medicine.UpdateAnimals(context, model);
				transaction.Commit();
				return medicine.GetViewModel;
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}
		public MedicineViewModel? Delete(MedicineBindingModel model)
		{
			using var context = new VetClinicDatabase();
			var element = context.Medicines.Include(x => x.Pharmacist)
			.Include(x => x.Animals).ThenInclude(x => x.Animal)
			.FirstOrDefault(rec => rec.Id == model.Id);
			if (element != null)
			{
				context.Medicines.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}
