using CarCenterContracts.BindingModels;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace CarCenterDataBaseImplement.Implemets
{
    public class SaleStorage : ISaleStorage
    {
        public SaleViewModel? Delete(SaleBindingModel model)
        {
            using var context = new CarCenterDataBase();

            var element = context.Sales.FirstOrDefault(rec => rec.Id == model.Id);

            if (element != null)
            {
                context.Sales.Remove(element);
                context.SaveChanges();

                return element.GetViewModel;
            }

            return null;
        }

        public SaleViewModel? GetElement(SaleSearchModel model)
        {
            if (model.SaleDate == default(DateTime) && !model.Id.HasValue)
            {
                return null;
            }

            using var context = new CarCenterDataBase();

            return context.Sales
                .Include(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Manager)
                .FirstOrDefault(x => (model.SaleDate == default(DateTime) && x.SaleDate == model.SaleDate) || (model.Id.HasValue && x.Id == model.Id))?
                .GetViewModel;
        }

        public List<SaleViewModel> GetFilteredList(SaleSearchModel model)
        {
            if (model.SaleDate == default(DateTime) && !model.ManagerId.HasValue)
            {
                return new();
            }

            using var context = new CarCenterDataBase();

            if (model.ManagerId.HasValue)
            {
                return context.Sales
                .Include(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Manager)
                    .Where(x => x.ManagerId == model.ManagerId)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }

            return context.Sales
                .Include(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Manager)
                .Where(x => x.SaleDate == model.SaleDate)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<SaleViewModel> GetFullList()
        {
            using var context = new CarCenterDataBase();

            return context.Sales
                .Include(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Manager)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public SaleViewModel? Insert(SaleBindingModel model)
        {
            using var context = new CarCenterDataBase();

            var newSale = Sale.Create(model);

            if (newSale == null)
            {
                return null;
            }

            context.Sales.Add(newSale);
            context.SaveChanges();

            return newSale.GetViewModel;
        }

        public SaleViewModel? Update(SaleBindingModel model)
        {
            using var context = new CarCenterDataBase();

            var Sale = context.Sales.FirstOrDefault(x => x.Id == model.Id);

            if (Sale == null)
            {
                return null;
            }

            Sale.Update(model);
            context.SaveChanges();

            return Sale.GetViewModel;
        }
    }
}