using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;

namespace VeterinaryDatabaseImplement.Implements
{
	public class ServiceStorage : IServiceStorage
	{
		public List<ServiceViewModel> GetFullList()
		{
			using var context = new VeterinaryDatabase();
			return context.Services.Include(x=>x.Doctor)
			.Include(x => x.Visit)
			 .Include(x => x.Medications)
			.ThenInclude(x => x.Medication)
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
		{
			
			using var context = new VeterinaryDatabase();
			return context.Services.Where(x => x.DoctorId == model.DoctorId).Include(x => x.Doctor).Include(x => x.Visit).Include(x => x.Medications)
			.ThenInclude(x => x.Medication)
			.Where(x => String.IsNullOrEmpty(model.ServiceName) ||  x.ServiceName.Contains(model.ServiceName))
			.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 VeterinaryDatabase();
			return context.Services.Include(x => x.Doctor)
			.Include(x => x.Visit)
			 .Include(x => x.Medications)
			 .ThenInclude(x => x.Medication)
			 .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 VeterinaryDatabase();
			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 VeterinaryDatabase();
			using var transaction = context.Database.BeginTransaction();
			try
			{
				var service = context.Services.FirstOrDefault(rec =>
				rec.Id == model.Id);
				if (service == null)
				{
					return null;
				}
				service.Update(model);
				context.SaveChanges();
				service.UpdateMedications(context, model);
				transaction.Commit();
				return service.GetViewModel;
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}
		public ServiceViewModel? Delete(ServiceBindingModel model)
		{
			using var context = new VeterinaryDatabase();
			var element = context.Services.Include(x => x.Doctor).Include(x => x.Visit)
			.Include(x => x.Medications)
			.FirstOrDefault(rec => rec.Id == model.Id);
			if (element != null)
			{
				context.Services.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
	}
}
