using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace GiftShopDatabaseImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            using var context = new GiftShopDatabase();
            var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Orders.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public OrderViewModel? GetElement(OrderSearchModel model)
        {
			using var context = new GiftShopDatabase();
			return context.Orders.Include(x => x.Gift).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(
				x => ((model.Id.HasValue && x.Id == model.Id) ||
				(x.ImplementerId == model.ImplementerId && x.Status == model.Status)))?.GetViewModel;
		}

		public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
		{
			if (model is null)
			{
				return new();
			}
			using var context = new GiftShopDatabase();
			return context.Orders
			.Include(x => x.Gift)
				.Include(x => x.Client).Include(x => x.Implementer).Where(x => (
						(!model.Id.HasValue || x.Id == model.Id) &&
						(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
						(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)
						&&
						(!model.ClientId.HasValue || x.ClientId == model.ClientId)
					)
				)
		   .Select(x => x.GetViewModel)
		   .ToList();
		}

		public List<OrderViewModel> GetFullList()
        {
            using var context = new GiftShopDatabase();
			return context.Orders.Include(x => x.Gift)
                .Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList();
        }

        public OrderViewModel? Insert(OrderBindingModel model)
        {
            using var context = new GiftShopDatabase();
            var newOrder = Order.Create(model, context);
            if (newOrder == null)
            {
                return null;
            }
            context.Orders.Add(newOrder);
            context.SaveChanges();

			return context.Orders
						  .Include(x => x.Gift)
						  .Include(x => x.Client)
                          .Include(x => x.Implementer)
                          .FirstOrDefault(x => x.Id == newOrder.Id)
						  ?.GetViewModel;
		}

        public OrderViewModel? Update(OrderBindingModel model)
        {
            using var context = new GiftShopDatabase();
			var order = context.Orders.Include(x => x.Gift)
                .Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id);
            if (order == null)
            {
                return null;
            }
            order.Update(model);
            context.SaveChanges();
            return order.GetViewModel;
        }
    }
}
