using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace ComputerHardwareStoreDatabaseImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        public List<OrderViewModel> GetFullList()
        {
            using var context = new ComputerHardwareStoreDBContext();
            return context.Orders
                .Include(o => o.Products)
                .ThenInclude(o => o.Product)
                .Select(o => o.GetViewModel)
                .ToList();
        }

        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            using var context = new ComputerHardwareStoreDBContext();
            return context.Orders 
                .Include(o => o.Products)
                .ThenInclude(o => o.Product)
                .Where(o => 
                (model.Id.HasValue && o.Id == model.Id) ||
                ((model.DateFrom.HasValue && model.DateTo.HasValue) && 
                ((model.DateFrom <= o.DateCreate && o.DateCreate <= model.DateTo) ||
                (o.DateImplement.HasValue && o.DateCreate < model.DateFrom && model.DateFrom < o.DateImplement)))
                )
                .Select(o => o.GetViewModel)
                .ToList();
        }

        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new ComputerHardwareStoreDBContext();
            return context.Orders
                .Include(o => o.Products)
                .ThenInclude(o => o.Product)
                .FirstOrDefault(o => o.Id == model.Id)?
                .GetViewModel;

        }

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

        public OrderViewModel? Update(OrderBindingModel model)
        {
            using var context = new ComputerHardwareStoreDBContext();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var order = context.Orders
                    .Include(o => o.Products)
                    .ThenInclude(o => o.Product)
                    .FirstOrDefault(o => o.Id == model.Id);
                if (order == null)
                {
                    return null;
                }
                order.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return order.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public OrderViewModel? Delete(OrderBindingModel model)
        {
            using var context = new ComputerHardwareStoreDBContext();
            var element = context.Orders
                .Include(o => o.Products)
                .ThenInclude(o => o.Product)
                .FirstOrDefault(o => o.Id == model.Id);
            if (element != null)
            {
                context.Orders.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}