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


namespace ConfectioneryDatabaseImplement.Implements
{
    public class PastryStorage : IPastryStorage
    {
        public List<PastryViewModel> GetFullList()
        {
            using var context = new ConfectioneryDatabase();
            return context.Pastries
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<PastryViewModel> GetFilteredList(PastrySearchModel model)
        {
            if (string.IsNullOrEmpty(model.PastryName))
            {
                return new();
            }
            using var context = new ConfectioneryDatabase();
            return context.Pastries
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.PastryName.Contains(model.PastryName))
            .ToList()
            .Select(x => x.GetViewModel).ToList();
        }
        public PastryViewModel? GetElement(PastrySearchModel model)
        {
            if (string.IsNullOrEmpty(model.PastryName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ConfectioneryDatabase();
            return context.Pastries
             .Include(x => x.Components)
             .ThenInclude(x => x.Component)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) &&
            x.PastryName == model.PastryName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public PastryViewModel? Insert(PastryBindingModel model)
        {
            using var context = new ConfectioneryDatabase();
            var newPastry = Pastry.Create(context, model);
            if (newPastry == null)
            {
                return null;
            }
            context.Pastries.Add(newPastry);
            context.SaveChanges();
            return newPastry.GetViewModel;
        }
        public PastryViewModel? Update(PastryBindingModel model)
        {
            using var context = new ConfectioneryDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var pastry = context.Pastries.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (pastry == null)
                {
                    return null;
                }
                pastry.Update(model);
                context.SaveChanges();
                pastry.UpdateComponents(context, model);
                transaction.Commit();
                return pastry.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public PastryViewModel? Delete(PastryBindingModel model)
        {
            using var context = new ConfectioneryDatabase();
            var element = context.Pastries
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Pastries.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
