using Microsoft.EntityFrameworkCore;
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryDatabaseImplement.Models;

namespace FishFactoryDatabaseImplement.Implements
{
    public class CannedStorage : ICannedStorage
    {
        public List<CannedViewModel> GetFullList()
        {
            using var context = new FishFactoryDatabase();
            return context.CannedList
                    .Include(x => x.Ingredients)
                    .ThenInclude(x => x.Ingredient)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<CannedViewModel> GetFilteredList(CannedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.CannedName))
            {
                return new();
            }
            using var context = new FishFactoryDatabase();
            return context.CannedList
                    .Include(x => x.Ingredients)
                    .ThenInclude(x => x.Ingredient)
                    .Where(x => x.CannedName.Contains(model.CannedName))
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public CannedViewModel? GetElement(CannedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.CannedName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new FishFactoryDatabase();
            return context.CannedList
                .Include(x => x.Ingredients)
                .ThenInclude(x => x.Ingredient)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) && x.CannedName == model.CannedName) ||
                                (model.Id.HasValue && x.Id == model.Id))
                ?.GetViewModel;
        }

        public CannedViewModel? Insert(CannedBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var newCanned = Canned.Create(context, model);
            if (newCanned == null)
            {
                return null;
            }
            context.CannedList.Add(newCanned);
            context.SaveChanges();
            return newCanned.GetViewModel;
        }

        public CannedViewModel? Update(CannedBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var Canned = context.CannedList.FirstOrDefault(rec => rec.Id == model.Id);
                if (Canned == null)
                {
                    return null;
                }
                Canned.Update(model);
                context.SaveChanges();
                Canned.UpdateIngredients(context, model);
                transaction.Commit();
                return Canned.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public CannedViewModel? Delete(CannedBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var element = context.CannedList
                .Include(x => x.Ingredients)
                .Include(x => x.Orders)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.CannedList.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
