using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace BarDatabaseImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        public List<OrderViewModel> GetFullList()
        {
            using var Context = new BarDatabase();

            return Context.Orders
                .Include(x => x.Cocktail)
                .Include(x => x.Client)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<OrderViewModel> GetFilteredList(OrderSearchModel Model)
        {
            using var Context = new BarDatabase();

            if (Model.DateFrom.HasValue)
            {
                return Context.Orders
                    .Include(x => x.Cocktail)
                    .Include(x => x.Client)
                    .Where(x => (x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo) && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId))
                    .Select(x => x.GetViewModel)
                    .ToList();
            }

            return Context.Orders
                .Include(x => x.Cocktail)
                .Include(x => x.Client)
                .Where(x => (Model.Id.HasValue && x.Id == Model.Id) || (Model.ClientId.HasValue && x.ClientId == Model.ClientId))
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public OrderViewModel? GetElement(OrderSearchModel Model)
        {
            if (!Model.Id.HasValue)
                return null;

            using var Context = new BarDatabase();

            return Context.Orders
                .Include(x => x.Cocktail)
                .Include(x => x.Client)
                .FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)?
                .GetViewModel;
        }

        public OrderViewModel? Insert(OrderBindingModel Model)
        {
            if (Model == null)
                return null;

            var NewOrder = Order.Create(Model);
            if (NewOrder == null)
                return null;

            using var Context = new BarDatabase();

            Context.Orders.Add(NewOrder);
            Context.SaveChanges();

            return Context.Orders.Include(x => x.Cocktail).Include(x => x.Client).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel;
        }

        public OrderViewModel? Update(OrderBindingModel Model)
        {
            using var Context = new BarDatabase();

            var Order = Context.Orders.FirstOrDefault(x => x.Id == Model.Id);
            if (Order == null)
                return null;

            Order.Update(Model);
            Context.SaveChanges();

            return Context.Orders.Include(x => x.Cocktail).Include(x => x.Client).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel;
        }

        public OrderViewModel? Delete(OrderBindingModel Model)
        {
            using var Context = new BarDatabase();
            var Order = Context.Orders.Include(x => x.Cocktail).Include(x => x.Client).FirstOrDefault(rec => rec.Id == Model.Id);

            if (Order == null)
                return null;

            Context.Orders.Remove(Order);
            Context.SaveChanges();

            return Order.GetViewModel;
        }
    }
}
