using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using HotelDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelDataBaseImplement.Implemets
{
    public class RoomStorage : IRoomStorage
    {
        public RoomViewModel? Delete(RoomBindingModel model)
        {
            using var context = new HotelDataBase();

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

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

                return element.GetViewModel;
            }

            return null;
        }

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

            using var context = new HotelDataBase();

            return context.Rooms
                .Include(x => x.Dinners)
                .ThenInclude(x => x.Dinner)
                .ThenInclude(x => x.ConferenceBookingDinners)
                .ThenInclude(x => x.ConferenceBooking)
                .Include(x => x.MealPlan)
                .Include(x => x.Headwaiter)
                .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
                .GetViewModel;
        }

        public List<RoomViewModel> GetFilteredList(RoomSearchModel model)
        {
            if (string.IsNullOrEmpty(model.RoomName))
            {
                return new();
            }

            using var context = new HotelDataBase();

            return context.Rooms
                .Include(x => x.Dinners)
                .ThenInclude(x => x.Dinner)
                .ThenInclude(x => x.ConferenceBookingDinners)
                .ThenInclude(x => x.ConferenceBooking)
                .Include(x => x.MealPlan)
                .Include(x => x.Headwaiter)
                .Where(x => x.RoomName.Contains(model.RoomName))
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<RoomViewModel> GetFullList()
        {
            using var context = new HotelDataBase();
            return context.Rooms
                .Include(x => x.Dinners)
                .ThenInclude(x => x.Dinner)
                .ThenInclude(x => x.ConferenceBookingDinners)
                .ThenInclude(x => x.ConferenceBooking)
                .Include(x => x.MealPlan)
                .Include(x => x.Headwaiter)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public RoomViewModel? Insert(RoomBindingModel model)
        {
            using var context = new HotelDataBase();
            var newRoom = Room.Create(context, model);

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

            context.Rooms.Add(newRoom);
            context.SaveChanges();
            return newRoom.GetViewModel;
        }

        public RoomViewModel? Update(RoomBindingModel model)
        {
            using var context = new HotelDataBase();
            var room = context.Rooms.FirstOrDefault(x => x.Id == model.Id);
            if (room == null)
            {
                return null;
            }
            room.Update(model);
            context.SaveChanges();
            return room.GetViewModel;
        }
    }
}
