using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using CanteenDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanteenDatabaseImplement.Implements
{
    public class ProductStorage : IProductStorage
    {
        public ProductViewModel? Delete(ProductBindingModel model)
        {
            using var context = new CanteenDatabase();

            var element = context.Products.FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Products.Remove(element);
                context.SaveChanges();

                return element.GetViewModel;
            }

            return null;
        }

        public ProductViewModel? GetElement(ProductSearchModel model)
        {
            if (!model.Id.HasValue && string.IsNullOrEmpty(model.ProductName))
            {
                return null;
            }

            using var context = new CanteenDatabase();

            return context.Products.Include(x => x.Cooks).ThenInclude(x => x.Cook)
                .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || x.ProductName.Contains(model.ProductName)
            )?.GetViewModel;
        }

        public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ProductName))
            {
                return new();
            }

            using var context = new CanteenDatabase();

            return context.Products.Include(x => x.Cooks).ThenInclude(x => x.Cook).Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList();
        }

        public List<ProductViewModel> GetFullList()
        {
            using var context = new CanteenDatabase();
            return context.Products.Include(x => x.Cooks).ThenInclude(x => x.Cook).Select(x => x.GetViewModel).ToList();
        }

        public ProductViewModel? Insert(ProductBindingModel model)
        {
            using var context = new CanteenDatabase();

            using var transaction = context.Database.BeginTransaction();
            {
                try
                {
                    var newProduct = Product.Create(context, model);
                    if (newProduct == null)
                    {
                        transaction.Rollback();
                        return null;
                    }

                    context.Products.Add(newProduct);

                    context.SaveChanges();
                    context.Database.CommitTransaction();

                    return newProduct.GetViewModel;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }

        public ProductViewModel? Update(ProductBindingModel model)
        {
            using var context = new CanteenDatabase();

            using var transaction = context.Database.BeginTransaction();
            {
                try
                {
                    var product = context.Products.FirstOrDefault(x => x.Id == model.Id);
                    if (product == null)
                    {
                        transaction.Rollback();
                        return null;
                    }

                    product.Update(model);

                    context.SaveChanges();
                    context.Database.CommitTransaction();

                    return product.GetViewModel;
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }
    }
}
