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

namespace CarCenterDataBaseImplement.Implemets
{
    public class ManagerStorage : IManagerStorage
    {
        public ManagerViewModel? Delete(ManagerBindingModel model)
        {
            using var context = new CarCenterDataBase();

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

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

                return element.GetViewModel;
            }

            return null;
        }

        public ManagerViewModel? GetElement(ManagerSearchModel model)
        {
            using var context = new CarCenterDataBase();
            
            if (model.Id.HasValue)
                return context.Managers
                    .Include(x => x.PreSaleWorks)
                    .Include(x => x.Sales)
                    .Include(x => x.Employees)
                    .FirstOrDefault(x => x.Id == model.Id)?
                    .GetViewModel;
            
            if (!string.IsNullOrEmpty(model.ManagerEmail) && !string.IsNullOrEmpty(model.ManagerPassword))
                return context.Managers
                    .Include(x => x.PreSaleWorks)
                    .Include(x => x.Sales)
                    .Include(x => x.Employees)
                    .FirstOrDefault(x => x.ManagerEmail.Equals(model.ManagerEmail) && x.ManagerPassword.Equals(model.ManagerPassword))?
                    .GetViewModel;
            
            if (!string.IsNullOrEmpty(model.ManagerEmail))
                return context.Managers
                    .Include(x => x.PreSaleWorks)
                    .Include(x => x.Sales)
                    .Include(x => x.Employees)
                    .FirstOrDefault(x => x.ManagerEmail.Equals(model.ManagerEmail))?
                    .GetViewModel;
            
            return null;
        }

        public List<ManagerViewModel> GetFilteredList(ManagerSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ManagerFIO))
            {
                return new();
            }

            using var context = new CarCenterDataBase();

            return context.Managers
                .Include(x => x.PreSaleWorks)
                .Include(x => x.Sales)
                .Include(x => x.Employees)
                .Where(x => x.ManagerLogin.Contains(model.ManagerLogin) && x.ManagerPassword == model.ManagerPassword)
                .Select(x => x.GetViewModel)
                .ToList();
        }

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

            return context.Managers
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public ManagerViewModel? Insert(ManagerBindingModel model)
        {
            var newManager = Manager.Create(model);

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

            using var context = new CarCenterDataBase();

            context.Managers.Add(newManager);
            context.SaveChanges();

            return newManager.GetViewModel;
        }

        public ManagerViewModel? Update(ManagerBindingModel model)
        {
            using var context = new CarCenterDataBase();

            var Manager = context.Managers
                .FirstOrDefault(x => x.Id == model.Id);

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

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

            return Manager.GetViewModel;
        }
    }
}
