using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace DinerDataBaseImplement.Implements
{
    public class SnackStorage : ISnackStorage
    {
        public List<SnackViewModel> GetFullList()
        {
            using var context = new DinerDatabase();
            return context.Snacks
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<SnackViewModel> GetFilteredList(SnackSearchModel model)
        {
            if (string.IsNullOrEmpty(model.SnackName))
            {
                return new();
            }
            using var context = new DinerDatabase();
            return context.Snacks
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.SnackName.Contains(model.SnackName))
            .ToList()
            .Select(x => x.GetViewModel).ToList();
        }
        public SnackViewModel? GetElement(SnackSearchModel model)
        {
            if (string.IsNullOrEmpty(model.SnackName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new DinerDatabase();
            return context.Snacks
             .Include(x => x.Components)
             .ThenInclude(x => x.Component)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SnackName) && x.SnackName == model.SnackName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }
        public SnackViewModel? Insert(SnackBindingModel model)
        {
            using var context = new DinerDatabase();
            var newSnack = Snack.Create(context, model);
            if (newSnack == null)
            {
                return null;
            }
            context.Snacks.Add(newSnack);
            context.SaveChanges();
            return newSnack.GetViewModel;
        }
        public SnackViewModel? Update(SnackBindingModel model)
        {
            using var context = new DinerDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var snack = context.Snacks.FirstOrDefault(rec => rec.Id == model.Id);
                if (snack == null)
                {
                    return null;
                }
                snack.Update(model);
                context.SaveChanges();
                snack.UpdateComponents(context, model);
                transaction.Commit();
                return snack.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public SnackViewModel? Delete(SnackBindingModel model)
        {
            using var context = new DinerDatabase();
            var element = context.Snacks
            .Include(x => x.Components)
            .Include(x => x.Orders)
            .FirstOrDefault(rec => rec.Id == model.Id); 
            if (element != null)
            {
                context.Snacks.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
