using Microsoft.EntityFrameworkCore;
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDatabaseImplement.Models;

namespace SewingDressesDatabaseImplement.Implements
{
    public class DressStorage : IDressStorage
    {
        public List<DressViewModel> GetFullList()
        {
            using var context = new SewingDressesDatabase();
            return context.Dresses.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
        }
        public List<DressViewModel> GetFilteredList(DressSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DressName))
            {
                return new();
            }
            using var context = new SewingDressesDatabase();
            return context.Dresses.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.DressName.Contains(model.DressName)).ToList().Select(x => x.GetViewModel).ToList();

        }
        public DressViewModel? GetElement(DressSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DressName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new SewingDressesDatabase();
            return context.Dresses.Include(x => x.Components).ThenInclude(x => x.Component)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DressName) && x.DressName == model.DressName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }
        public DressViewModel? Insert(DressBindingModel model)
        {
            using var context = new SewingDressesDatabase();
            var newDress = Dress.Create(context, model);
            if (newDress == null)
            {
                return null;
            }
            context.Dresses.Add(newDress);
            context.SaveChanges();
            return newDress.GetViewModel;
        }
        public DressViewModel? Update(DressBindingModel model)
        {
            using var context = new SewingDressesDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var dress = context.Dresses.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (dress == null)
                {
                    return null;
                }
                dress.Update(model);
                context.SaveChanges();
                dress.UpdateComponents(context, model);
                transaction.Commit();
                return dress.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public DressViewModel? Delete(DressBindingModel model)
        {
            using var context = new SewingDressesDatabase();
            var element = context.Dresses
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Dresses.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

    }
}
