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

namespace CarCenterDataBaseImplement.Implemets
{
    public class PreSaleWorkStorage : IPreSaleWorkStorage
    {
        public PreSaleWorkViewModel? Delete(PreSaleWorkBindingModel model)
        {
            using var context = new CarCenterDataBase();

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

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

                return element.GetViewModel;
            }

            return null;
        }

        public PreSaleWorkViewModel? GetElement(PreSaleWorkSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PreSaleWorkType) && !model.Id.HasValue)
            {
                return null;
            }

            using var context = new CarCenterDataBase();

            return context.PreSaleWorks
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.Equipments)
                .Include(x => x.Manager)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PreSaleWorkType) && x.PreSaleWorkType == model.PreSaleWorkType) || (model.Id.HasValue && x.Id == model.Id))?
                .GetViewModel;
        }

        public List<PreSaleWorkViewModel> GetFilteredList(PreSaleWorkSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PreSaleWorkType) && !model.ManagerId.HasValue)
            {
                return new();
            }

            using var context = new CarCenterDataBase();

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

            return context.PreSaleWorks
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.Equipments)
                .Include(x => x.Manager)
                .Where(x => x.PreSaleWorkType.Contains(model.PreSaleWorkType))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<PreSaleWorkViewModel> GetFullList()
        {
            using var context = new CarCenterDataBase();
            return context.PreSaleWorks
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.EmployeeSales)
                .ThenInclude(x => x.Employee)
                .Include(x => x.Equipments)
                .Include(x => x.Manager)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public PreSaleWorkViewModel? Insert(PreSaleWorkBindingModel model)
        {
            using var context = new CarCenterDataBase();
            var newPreSaleWork = PreSaleWork.Create(context,model);

            if (newPreSaleWork == null)
            {
                return null;
            }
            
            context.PreSaleWorks.Add(newPreSaleWork);
            context.SaveChanges();
            return newPreSaleWork.GetViewModel;
        }

        public PreSaleWorkViewModel? Update(PreSaleWorkBindingModel model)
        {
            using var context = new CarCenterDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var elem = context.PreSaleWorks.FirstOrDefault(rec => rec.Id == model.Id);
                if (elem == null)
                {
                    return null;
                }
                elem.Update(model);
                context.SaveChanges();
                if (model.PreSaleWorkSales != null)
                    elem.UpdateSales(context, model);
                transaction.Commit();
                return elem.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
