using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using HotelDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace HotelDataBaseImplement.Implemets
{
    public class ConferenceStorage : IConferenceStorage
    {
        public ConferenceViewModel? Delete(ConferenceBindingModel model)
        {
            using var context = new HotelDataBase();

            var element = context.Conferences
                .FirstOrDefault(rec => rec.Id == model.Id);

            if (element != null)
            {
                context.Conferences.Remove(element);
                context.SaveChanges();

                return element.GetViewModel;
            }

            return null;
        }

        public ConferenceViewModel? GetElement(ConferenceSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }

            using var context = new HotelDataBase();

            return context.Conferences
                .Include(x => x.Members)
                .ThenInclude(x => x.Member)
                .ThenInclude(x => x.MealPlanMember)
                .ThenInclude(x => x.MealPlan)
                .Include(x => x.ConferenceBookings)
                .Include(x => x.Organiser)
                .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
                .GetViewModel;
        }

        public List<ConferenceViewModel> GetFilteredList(ConferenceSearchModel model)
        {
            if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.OrganiserId.HasValue)
            {
                return new();
            }
            using var context = new HotelDataBase();

            if (model.DateFrom.HasValue)
            {
                return context.Conferences
                        .Include(x => x.Members)
                        .ThenInclude(x => x.Member)
                        .ThenInclude(x => x.MealPlanMember)
                        .ThenInclude(x => x.MealPlan)
                        .Include(x => x.ConferenceBookings)
                        .Include(x => x.Organiser)
                        .Where(x => x.StartDate >= model.DateFrom && x.StartDate <= model.DateTo)
                        .Select(x => x.GetViewModel)
                        .ToList();
            }
            else if (model.OrganiserId.HasValue)
                return context.Conferences
                    .Include(x => x.Members)
                    .ThenInclude(x => x.Member)
                    .ThenInclude(x => x.MealPlanMember)
                    .ThenInclude(x => x.MealPlan)
                    .Include(x => x.ConferenceBookings)
                    .Include(x => x.Organiser)
                    .Where(x => x.OrganiserId == model.OrganiserId)
                    .Select(x => x.GetViewModel)
                    .ToList();

            return context.Conferences
                .Include(x => x.Members)
                .ThenInclude(x => x.Member)
                .ThenInclude(x => x.MealPlanMember)
                .ThenInclude(x => x.MealPlan)
                .Include(x => x.ConferenceBookings)
                .Include(x => x.Organiser)
                .Where(x => x.Id == model.Id)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<ConferenceViewModel> GetFullList()
        {
            using var context = new HotelDataBase();

            return context.Conferences
                .Include(x => x.Members)
                .ThenInclude(x => x.Member)
                .ThenInclude(x => x.MealPlanMember)
                .ThenInclude(x => x.MealPlan)
                .Include(x => x.ConferenceBookings)
                .Include(x => x.Organiser)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public ConferenceViewModel? Insert(ConferenceBindingModel model)
        {
            using var context = new HotelDataBase();
            var newConference = Conference.Create(context,model);

            if (newConference == null)
            {
                return null;
            }

            context.Conferences.Add(newConference);
            context.SaveChanges();

            return context.Conferences
                .Include(x => x.Members)
                .ThenInclude(x => x.Member)
                .ThenInclude(x => x.MealPlanMember)
                .ThenInclude(x => x.MealPlan)
                .Include(x => x.ConferenceBookings)
                .Include(x => x.Organiser)
                .FirstOrDefault(x => x.Id == newConference.Id)
                ?.GetViewModel;
        }
    

        public ConferenceViewModel? Update(ConferenceBindingModel model)
        {
            using var context = new HotelDataBase();

            var conference = context.Conferences
                .Include(x => x.Members)
                .ThenInclude(x => x.Member)
                .ThenInclude(x => x.MealPlanMember)
                .ThenInclude(x => x.MealPlan)
                .Include(x => x.ConferenceBookings)
                .Include(x => x.Organiser)
                .FirstOrDefault(x => x.Id == model.Id);

            if (conference == null)
            {
                return null;
            }

            conference.Update(model);
            context.SaveChanges();

            return conference.GetViewModel;
        }
    }
}
