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

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

            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 BankDataBase();

            return context.Rooms
                .Include(x => x.Credits)
                .ThenInclude(x => x.Credit)
                .ThenInclude(x => x.ConferenceBookingCredits)
                .ThenInclude(x => x.ConferenceBooking)
                .Include(x => x.Additions)
                .Include(x => x.Client)
                .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 BankDataBase();

            return context.Rooms
                .Include(x => x.Credits)
                .ThenInclude(x => x.Credit)
                .ThenInclude(x => x.ConferenceBookingCredits)
                .ThenInclude(x => x.ConferenceBooking)
                .Include(x => x.Credits)
                .Include(x => x.Client)
                .Where(x => x.RoomName.Contains(model.RoomName))
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<RoomViewModel> GetFullList()
        {
            using var context = new BankDataBase();
            return context.Rooms
                .Include(x => x.Credits)
                .ThenInclude(x => x.Credit)
                .ThenInclude(x => x.ConferenceBookingCredits)
                .ThenInclude(x => x.ConferenceBooking)
                .Include(x => x.Additions)
                .Include(x => x.Client)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public RoomViewModel? Insert(RoomBindingModel model)
        {
            using var context = new BankDataBase();
            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 BankDataBase();
            var room = context.Rooms.FirstOrDefault(x => x.Id == model.Id);
            if (room == null)
            {
                return null;
            }
            room.Update(model);
            context.SaveChanges();
            return room.GetViewModel;
        }
    }
}