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 PreSaleWork : IPreSaleWorkModel
    {
        [Required]
        public string PreSaleWorkType { get; set; } = string.Empty;

        [Required]
        public double PreSaleWorkPrice { get; set; }

        public int ManagerId { get; private set; }

        public int Id { get; private set; }

        public virtual Manager Manager { get; set; }

        private Dictionary<int, ISaleModel> _PreSaleWorkSales = null;

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

        [ForeignKey("PreSaleWorkId")]
        public virtual List<Equipment> Equipments { get; set; } = new();

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

        public static PreSaleWork Create(CarCenterDataBase context, PreSaleWorkBindingModel model)
        {
            return new PreSaleWork()
            {
                Id = model.Id,
                PreSaleWorkType = model.PreSaleWorkType,
                PreSaleWorkPrice = model.PreSaleWorkPrice,
                ManagerId = model.ManagerId,
                Sales = model.PreSaleWorkSales.Select(x => new PreSaleWorkSale
                {
                    Sale = context.Sales.First(y => y.Id == x.Key),
                }).ToList()
            };
        }

        public void Update(PreSaleWorkBindingModel model)
        {
            PreSaleWorkType = model.PreSaleWorkType;
            PreSaleWorkPrice = model.PreSaleWorkPrice;
            ManagerId = model.ManagerId;
        }

        public PreSaleWorkViewModel GetViewModel => new()
        {
            Id = Id,
            PreSaleWorkType = PreSaleWorkType,
            PreSaleWorkPrice = PreSaleWorkPrice,
            ManagerId=ManagerId,
            PreSaleWorkSales = PreSaleWorkSales
        };

        public void UpdateSales(CarCenterDataBase context, PreSaleWorkBindingModel model)
        {
            var PreSaleWorkSales = context.PreSaleWorkSales.Where(rec => rec.PreSaleWorkId == model.Id).ToList();

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

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

            var PreSaleWork = context.PreSaleWorks.First(x => x.Id == Id);

            foreach (var cm in model.PreSaleWorkSales)
            {
                context.PreSaleWorkSales.Add(new PreSaleWorkSale
                {
                    PreSaleWork = PreSaleWork,
                    Sale = context.Sales.First(x => x.Id == cm.Key)
                });
                context.SaveChanges();
            }
            _PreSaleWorkSales = null;
        }
    }
}
