using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDatabaseImplement;
using SoftwareInstallationDataModels;
using SoftwareInstallationDataModels.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Xml.Linq;

namespace SoftwareInstallationDatabaseImplement.Models
{
    public class Shop : IShopModel
    {
        [Required]
        public string Name { get; private set; } = string.Empty;

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

        [Required]
        public int MaxCountPackages { get; private set; }

        public DateTime DateOpening { get; private set; }

        private Dictionary<int, (IPackageModel, int)>? _cachedPackages = null;
        [NotMapped]
        public  Dictionary<int, (IPackageModel, int)> Packages
        {
            get
            {
                if (_cachedPackages == null)
                {
                    using var context = new SoftwareInstallationDatabase();
                    _cachedPackages = ShopPackages
                        .ToDictionary(x => x.PackageId, x => (context.Packages
                        .FirstOrDefault(y => y.Id == x.PackageId)! as IPackageModel, x.Count));
                }
                return _cachedPackages;
            }
        }

        public int Id { get; private set; }

        [ForeignKey("ShopId")]
        public virtual List<ShopPackage> ShopPackages { get; set; } = new();

        public static Shop? Create(SoftwareInstallationDatabase context, ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Address = model.Address,
                DateOpening = model.DateOpening,
                MaxCountPackages = model.MaxCountPackages,
                ShopPackages = model.Packages.Select(x => new ShopPackage
                {
                    Package = context.Packages.FirstOrDefault(y => y.Id == x.Key)!,
                    Count = x.Value.Item2,
                }).ToList()
            };
        }
        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
            Address = model.Address;
            DateOpening = model.DateOpening;
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Address = Address,
            Packages = Packages,
            DateOpening = DateOpening,
            MaxCountPackages = MaxCountPackages,
        };

        public void UpdatePackages(SoftwareInstallationDatabase context, ShopBindingModel model)
        {
            var shopPackages = context.ShopPackages
                .Where(rec => rec.ShopId == model.Id)
                .ToList();
            // удалили те, которых нет в модели
            if (shopPackages != null && shopPackages.Count > 0)
            {
                context.ShopPackages
                    .RemoveRange(shopPackages
                    .Where(rec => !model.Packages
                                        .ContainsKey(rec.PackageId)));
                // обновили количество у существующих записей
                foreach (var updatePackage in shopPackages.Where(x => model.Packages.ContainsKey(x.PackageId)))
                {
                    updatePackage.Count = model.Packages[updatePackage.PackageId].Item2;
                    model.Packages.Remove(updatePackage.PackageId);
                }
            }
            var shop = context.Shops.First(x => x.Id == model.Id);
            shop.ShopPackages.AddRange(model.Packages.Select(x => new ShopPackage
            {
                Package = context.Packages.First(y => y.Id == x.Key),
                Count = x.Value.Item2,
            }).Except(shopPackages ?? new()));
            context.SaveChanges();
            _cachedPackages = null;
        }
    }
}