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

namespace CarCenterDataBaseImplement.Implemets
{
    public class EmployeeStorage : IEmployeeStorage
    {
        public EmployeeViewModel? Delete(EmployeeBindingModel model)
        {
            using var context = new CarCenterDataBase();

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

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

                return element.GetViewModel;
            }

            return null;
        }

        public EmployeeViewModel? GetElement(EmployeeSearchModel model)
        {
            if (string.IsNullOrEmpty(model.EmployeeFIO) && !model.Id.HasValue)
            {
                return null;
            }

            using var context = new CarCenterDataBase();

            return context.Employees
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Inspections)
                .Include(x => x.Manager)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.EmployeeFIO) && x.EmployeeFIO == model.EmployeeFIO) || (model.Id.HasValue && x.Id == model.Id))?
                .GetViewModel;
        }

        public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
        {
            if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ManagerId.HasValue)
            {
                return new();
            }
            using var context = new CarCenterDataBase();
            if (model.DateFrom.HasValue)
            {
                return context.Employees
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Inspections)
                .Include(x => x.Manager)
                .Where(x => x.ManagerId == model.ManagerId)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            else if (model.ManagerId.HasValue)
                return context.Employees
                    .Include(x => x.Sales)
                    .ThenInclude(x => x.Sale)
                    .ThenInclude(x => x.PreSaleWorkSale)
                    .ThenInclude(x => x.PreSaleWork)
                    .Include(x => x.Inspections)
                    .Include(x => x.Manager)
                    .Where(x => x.ManagerId == model.ManagerId)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();

            return context.Employees
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Inspections)
                .Include(x => x.Manager)
                .Where(x => x.EmployeeFIO.Contains(model.EmployeeFIO))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

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

            return context.Employees
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Inspections)
                .Include(x => x.Manager)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public EmployeeViewModel? Insert(EmployeeBindingModel model)
        {
            using var context = new CarCenterDataBase();
            var newEmployee = Employee.Create(context,model);

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

            context.Employees.Add(newEmployee);
            context.SaveChanges();

            return context.Employees
                .Include(x => x.Sales)
                .ThenInclude(x => x.Sale)
                .ThenInclude(x => x.PreSaleWorkSale)
                .ThenInclude(x => x.PreSaleWork)
                .Include(x => x.Inspections)
                .Include(x => x.Manager)
                .FirstOrDefault(x => x.Id == newEmployee.Id)
                ?.GetViewModel;
        }
    

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