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

namespace TravelCompanyDatabaseImplement.Models.TouristModel
{
    public class Travel : ITravelModel
    {
        public int Id { get; set; }
        [Required]
        public string TravelName { get; set; } = string.Empty;
        [Required]
        public DateTime DateStart { get; set; } = DateTime.Now;
        [Required]
        public string TravelDescription { get; private set; } = string.Empty;
        [Required]
        public int TouristId { get; private set; }
        public virtual Tourist Tourist { get; set; }
        [NotMapped]
        public Dictionary<int, ITourModel>? _tourTravels = null;
        public Dictionary<int, ITourModel> TourTravels
        {
            get
            {
                if (_tourTravels == null)
                {
                    
                    _tourTravels = Tours.ToDictionary(rec => rec.TourId, rec => rec.Tour as ITourModel);
                }
                return _tourTravels;
            }
        }

        [ForeignKey("TravelId")]
        public virtual List<ExcursionTravel> ExcursionTravels { get; set; } = new();
        [ForeignKey("TravelId")]
        public virtual List<TourTravel> Tours { get; set; } = new();

        public static Travel? Create(TravelCompanyDatabase context, TravelBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Travel()
            {
                Id = model.Id,
                TravelName = model.TravelName,
                DateStart = model.DateStart,
                TouristId = model.TouristId,
                TravelDescription = model.TravelDescription,
                Tours = model.TourTravels.Select(x => new TourTravel
                {
                    Tour = context.Tours.First(y => y.Id == x.Key)

                }).ToList()
            };
        }
        public static Travel Create(TravelBindingModel model)
        {
            return new Travel
            {
                Id = model.Id,
                TravelName = model.TravelName,
                DateStart = model.DateStart,
                TouristId = model.TouristId,
                TravelDescription = model.TravelDescription
            };
        }
        public void Update(TravelBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            TravelName = model.TravelName;
            DateStart = model.DateStart;
            TravelDescription = model.TravelDescription;
        }
        public TravelViewModel GetViewModel => new()
        {
            Id = Id,
            TravelName = TravelName,
            DateStart = DateStart,
            TouristId = TouristId,
            TravelDescription = TravelDescription,
            TourTravels = TourTravels
        };
    }
}
