using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.SearchModels.TouristSearchModels;
using TravelCompanyContracts.StoragesContracts.TouristStoragesContracts;
using TravelCompanyContracts.ViewModels.TouristViewModels;
using TravelCompanyDatabaseImplement.Models.TouristModel;

namespace TravelCompanyDatabaseImplement.Implements
{
    public class TravelStorage : ITravelStorage
    {
        public List<TravelViewModel> GetFullList()
        {
            using var context = new TravelCompanyDatabase();
            return context.Travels
                 .Include(x => x.Tours)
                 .ThenInclude(x => x.Tour)
                 .Include(x => x.Excursions)
                 .ThenInclude(x => x.Excursion)
                .Select(x => x.GetViewModel).ToList();
        }
        public List<TravelViewModel> GetFilteredList(TravelSearchModel model)
        {
            using var context = new TravelCompanyDatabase();
            if (model.TouristId.HasValue)
            {
                var travels = context.Travels
                .Where(x => x.TouristId == model.TouristId)
                .Include(x => x.Tourist)
                .Include(x => x.Tours)
                .ThenInclude(x => x.Tour)
                .Include(x => x.Excursions)
                .ThenInclude(x => x.Excursion)
                    .Select(x => x.GetViewModel)
                .ToList();
                if (model.DateTo.HasValue &&  model.DateFrom.HasValue)
                {
                    travels.Where(x =>
                     (x.DateStart >= model.DateFrom && x.DateStart <= model.DateTo)).ToList();
                }
                return travels;
            }
            else return new();
        }
        public TravelViewModel? GetElement(TravelSearchModel model)
        {
            if (string.IsNullOrEmpty(model.TravelName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new TravelCompanyDatabase();
            return context.Travels
             .Include(x => x.Tours)
             .ThenInclude(x => x.Tour)
             .Include(x => x.Excursions)
             .ThenInclude(x => x.Excursion)
            .FirstOrDefault(x =>
            !string.IsNullOrEmpty(model.TravelName) && x.TravelName == model.TravelName ||
            model.Id.HasValue && x.Id == model.Id)
            ?.GetViewModel;
        }
        public TravelViewModel? Insert(TravelBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var newTravel = Travel.Create(context,model);
            if (newTravel == null)
            {
                return null;
            }
            context.Travels.Add(newTravel);
            context.SaveChanges();
            return newTravel.GetViewModel;
        }

        public TravelViewModel? Update(TravelBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var component = context.Travels.FirstOrDefault(x => x.Id == model.Id);
            if (component == null)
            {
                return null;
            }
            component.Update(context,model);
            context.SaveChanges();
            return component.GetViewModel;
        }
        public TravelViewModel? Delete(TravelBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var element = context.Travels.FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Travels.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
