using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaseAccountingDataBaseImplement.Models
{
    public class Contract : IContractModel
    {
        public int Id { get; set; }

        [Required]
        public string Service { get; set; } = string.Empty;

        [Required]
        public decimal Coast { get; set; }

        [Required]
        public DateTime Date { get; set; }

        [Required]
        public int UserId { get; set; }

        public virtual User User { get; set; } = new();

        [ForeignKey("ContractId")]
        public virtual List<LawyerContract> LawyerContracts { get; set; } = new();

        [ForeignKey("ContractId")]
        public virtual List<DealContract> DealContracts { get; set; } = new();

        private Dictionary<int, IDealModel>? _deals;

        [NotMapped]
        public Dictionary<int, IDealModel> Deals 
        {
            get
            {
                if (_deals == null)
                {
                    _deals = DealContracts.ToDictionary(
                        x => x.DealId, x => x.Deal as IDealModel);
                }
                return _deals;
            }
        }

        public static Contract? Create(CaseAccountingDatabase context, ContractBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Contract()
            {
                Id = model.Id,
                Service = model.Service,
                Coast = model.Coast,
                Date = model.Date,
                UserId = model.UserId,
                User = context.Users.FirstOrDefault(x => x.Id == model.UserId) ?? throw new Exception("Not found user")
            };
        }

        public void Update(ContractBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Service = model.Service;
            Coast = model.Coast;
            Date = model.Date;
        }

        public void UpdateDeals(CaseAccountingDatabase context, ContractBindingModel model)
        {
            var contractDeals = context.DealContracts
                .Where(x => x.ContractId == model.Id)
                .ToList();
            if (contractDeals != null)
            {
                context.DealContracts
                    .RemoveRange(contractDeals
                            .Where(x => !model.Deals
                            .ContainsKey(x.DealId))
                        );
                context.SaveChanges();
                var contract = context.Contracts
                    .First(x => x.Id == Id);
                foreach (var cd in contractDeals)
                {
                    model.Deals.Remove(cd.DealId);
                }
                foreach (var cd in model.Deals)
                {
                    context.DealContracts.Add(new DealContract
                    {
                        Contract = contract,
                        Deal = context.Deals.First(x => x.Id == cd.Key),
                    });
                    context.SaveChanges();
                }
                _deals = null;
            }
        }

        public ContractViewModel GetViewModel => new()
        {
            Id = Id,
            Service = Service,
            Coast = Coast,
            Date = Date,
            UserId = UserId
        };
    }
}
