using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace CarCenterDataBaseImplement.Models
{
    public class Employee : IEmployeeModel
    {
        [Required]
        public string EmployeeFIO { get; set; } = string.Empty;
        [Required]
        public string Specialization { get; set; } = string.Empty;

        public int ManagerId { get; private set; }

        public int Id { get; private set; }

        public virtual Manager Manager { get; set; }

        private Dictionary<int, ISaleModel> _employeeSales = null;

        [NotMapped]
        public Dictionary<int, ISaleModel> EmployeeSales
        {
            get
            {
                if (_employeeSales == null)
                {
                    using var context = new CarCenterDataBase();
                    _employeeSales = Sales
                        .ToDictionary(x => x.SaleId, x => (context.Sales
                        .FirstOrDefault(y => y.Id == x.SaleId)! as ISaleModel));
                }
                return _employeeSales;
            }
        }

        [ForeignKey("EmployeeId")]
        public virtual List<Inspection> Inspections { get; set; } = new();

        [ForeignKey("EmployeeId")]
        public virtual List<EmployeeSale> Sales { get; set; } = new();

        public static Employee Create(CarCenterDataBase context, EmployeeBindingModel model)
        {
            return new Employee()
            {
                Id = model.Id,
                EmployeeFIO = model.EmployeeFIO,
                Specialization = model.Specialization,
                ManagerId=model.ManagerId,
                Sales = model.EmployeeSales.Select(x => new EmployeeSale
                {
                    Sale = context.Sales.First(y => y.Id == x.Key),
                }).ToList()
            };
        }

        public void Update(EmployeeBindingModel model)
        {
            EmployeeFIO = model.EmployeeFIO;
            Specialization = model.Specialization;
            ManagerId = model.ManagerId;
        }

        public EmployeeViewModel GetViewModel => new()
        {
            Id = Id,
            EmployeeFIO = EmployeeFIO,
            Specialization = Specialization,
            ManagerId = ManagerId,
            EmployeeSales = EmployeeSales
        };

        public void UpdateSales(CarCenterDataBase context, EmployeeBindingModel model)
        {
            var EmployeeSales = context.EmployeeSales.Where(rec => rec.EmployeeId == model.Id).ToList();

            if (EmployeeSales != null && EmployeeSales.Count > 0)
            {
                context.EmployeeSales.RemoveRange(EmployeeSales.Where(rec => !model.EmployeeSales.ContainsKey(rec.SaleId)));
                context.SaveChanges();

                foreach (var updateSale in EmployeeSales)
                {
                    model.EmployeeSales.Remove(updateSale.SaleId);
                }
                context.SaveChanges();
            }

            var Employee = context.Employees.First(x => x.Id == Id);
            foreach (var cm in model.EmployeeSales)
            {
                context.EmployeeSales.Add(new EmployeeSale
                {
                    Employee = Employee,
                    Sale = context.Sales.First(x => x.Id == cm.Key),
                });
                context.SaveChanges();
            }
            _employeeSales = null;
        }
    }
}
