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 ExcursionStorage : IExcursionStorage
    {
        public List<ExcursionViewModel> GetFullList()
        {
            using var context = new TravelCompanyDatabase();
            return context.Excursions
                .Include(x => x.Guides)
                .ThenInclude(x => x.Guide)
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public List<ExcursionViewModel> GetFilteredList(ExcursionSearchModel
       model)
        {
            using var context = new TravelCompanyDatabase();
            if (model.TouristId.HasValue)
            {
               return context.Excursions
                .Where(x => x.TouristId == model.TouristId)
                .Include(x => x.Tourist)
                .Include(x => x.Guides)
                .ThenInclude(x => x.Guide)
                .Select(x => x.GetViewModel)
                .ToList();
            }
            else return new();
        }
        public ExcursionViewModel? GetElement(ExcursionSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ExcursionName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new TravelCompanyDatabase();
            return context.Excursions
                .Include(x => x.Guides)
                .ThenInclude(x => x.Guide)
                .FirstOrDefault(x =>
                !string.IsNullOrEmpty(model.ExcursionName) && x.ExcursionName ==
                model.ExcursionName ||
                model.Id.HasValue && x.Id == model.Id)
                ?.GetViewModel;
        }
        public ExcursionViewModel? Insert(ExcursionBindingModel model)
        {
            var newExcursion = Excursion.Create(model);
            if (newExcursion == null)
            {
                return null;
            }
            using var context = new TravelCompanyDatabase();
            context.Excursions.Add(newExcursion);
            context.SaveChanges();
            return newExcursion.GetViewModel;
        }
        public ExcursionViewModel? Update(ExcursionBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var component = context.Excursions.FirstOrDefault(x => x.Id ==
           model.Id);
            if (component == null)
            {
                return null;
            }
            component.Update(context, model);
            context.SaveChanges();
            return component.GetViewModel;
        }
        public ExcursionViewModel? Delete(ExcursionBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var element = context.Excursions.FirstOrDefault(rec => rec.Id ==
           model.Id);
            if (element != null)
            {
                context.Excursions.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
