using Microsoft.EntityFrameworkCore;
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDatabaseImplement.Models;

namespace SewingDressesDatabaseImplement.Implements
{
	public class OrderStorage : IOrderStorage
	{
		public List<OrderViewModel> GetFullList()
		{
			using var context = new SewingDressesDatabase();
			return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Select(x => x.GetViewModel).ToList();
		}
		public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
		{
			if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.ClientId.HasValue && !model.Status.HasValue)
			{
				return new();
			}
			using var context = new SewingDressesDatabase();
			if (model.Id.HasValue)
				return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); 
			else if (model.ClientId.HasValue)
				return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
			else if (model.DateFrom != null)
				return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList();
			else
				return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.Status.Equals(model.Status)).Select(x => x.GetViewModel).ToList();
		}
		public OrderViewModel? GetElement(OrderSearchModel model)
		{
			if (!model.Id.HasValue && (!model.ImplementId.HasValue || !model.Status.HasValue))
			{
				return null;
			}
			using var context = new SewingDressesDatabase();
			if (model.Id.HasValue)
				return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
			return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.ImplementId == model.ImplementId && x.Status == model.Status)?.GetViewModel;
		}
		public OrderViewModel? Insert(OrderBindingModel model)
		{
			using var context = new SewingDressesDatabase();
			var order = Order.Create(model, context);
			if (order == null)
			{
				return null;
			}
			context.Orders.Add(order);
			context.SaveChanges();
			return order.GetViewModel;

		}
		public OrderViewModel? Update(OrderBindingModel model)
		{

			using var context = new SewingDressesDatabase();
			var order = context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.Id == model.Id);
			if (order == null)
			{
				return null;
			}
			order.Update(model);
			context.SaveChanges();
			return order.GetViewModel;

		}
		public OrderViewModel? Delete(OrderBindingModel model)
		{
			using var context = new SewingDressesDatabase();
			var element = context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.Id == model.Id);
			if (element == null)
			{
				return null;
			}
			context.Orders.Remove(element);
			context.SaveChanges();
			return element.GetViewModel;
		}

	}
}
