using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyDataModels.Models.TouristModels;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.ViewModels.TouristViewModels;
using TravelCompanyDataModels.Models.OperatorModels;

namespace TravelCompanyDatabaseImplement.Models.TouristModel
{
    public class Excursion : IExcursionModel
    {
        public int Id { get; set; }
        [Required]
        public string ExcursionName { get; set; } = string.Empty;
        [Required]
        public string ExcursionDescrition { get; set; } = string.Empty;
        [Required]
        public int Duration { get; set; }
        [Required]
        public int TouristId { get; set; }
        public virtual Tourist Tourist { get; set; }
        [ForeignKey("ExcursionId")]
        public virtual List<Place> Places { get; set; } = new();
        [ForeignKey("ExcursionId")]
        public virtual List<ExcursionTravel> ExcursionTravels { get; set; } = new();
        public Dictionary<int, IGuideModel>? _guideExcursions = null;
        [NotMapped]
        public Dictionary<int, IGuideModel> GuideExcursions
        {
            get
            {
                if (_guideExcursions == null)
                {
                    _guideExcursions = Guides.ToDictionary(rec => rec.GuideId, rec => rec.Guide as IGuideModel);
                }
                return _guideExcursions;
            }
        }
        [ForeignKey("ExcursionId")]
        public virtual List<GuideExcursion> Guides { get; set; } = new();
        public static Excursion? Create(ExcursionBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Excursion()
            {
                Id = model.Id,
                ExcursionName = model.ExcursionName,
                ExcursionDescrition = model.ExcursionDescrition,
                Duration = model.Duration,
                TouristId = model.TouristId
            };
        }
        public void Update(TravelCompanyDatabase context, ExcursionBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            ExcursionName = model.ExcursionName;
            ExcursionDescrition = model.ExcursionDescrition;
            Duration = model.Duration;

            var GuideExcursions = context.GuideExcursions.Where(rec => rec.ExcursionId == model.Id).ToList();
            if (GuideExcursions != null && GuideExcursions.Count > 0)
            {
                // удалили те, которых нет в модели
                context.GuideExcursions.RemoveRange(GuideExcursions.Where(rec => model.Id == rec.Excursion.Id));
                context.SaveChanges();
            }
            var Excursion = context.Excursions.First(x => x.Id == Id);
            foreach (var rc in model.GuideExcursions)
            {
                context.GuideExcursions.Add(new GuideExcursion
                {
                    Excursion = Excursion,
                    Guide = context.Guides.First(x => x.Id == rc.Key),
                });
                context.SaveChanges();
            }
            _guideExcursions = null;
        }
        public ExcursionViewModel GetViewModel => new()
        {
            Id = Id,
            ExcursionName = ExcursionName,
            ExcursionDescrition = ExcursionDescrition,
            Duration = Duration,
            TouristId = TouristId,
            GuideExcursions=GuideExcursions
        };
    }
}