using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.StoragesContracts;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;


namespace GarmentFactoryDatabaseImplement.Implements
{
    public class TextileStorage : ITextileStorage
    {
        public List<TextileViewModel> GetFullList()
        {
            using var context = new GarmentFactoryDatabase();
            return context.Textiles
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<TextileViewModel> GetFilteredList(TextileSearchModel model)
        {
            if (string.IsNullOrEmpty(model.TextileName))
            {
                return new();
            }
            using var context = new GarmentFactoryDatabase();
            return context.Textiles
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.TextileName.Contains(model.TextileName))
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public TextileViewModel? GetElement(TextileSearchModel model)
        {
            if (string.IsNullOrEmpty(model.TextileName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new GarmentFactoryDatabase();
            return context.Textiles
             .Include(x => x.Components)
             .ThenInclude(x => x.Component)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.TextileName) && x.TextileName == model.TextileName) ||
            (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }
        public TextileViewModel? Insert(TextileBindingModel model)
        {
            using var context = new GarmentFactoryDatabase();
            var newTextile = Textile.Create(context, model);
            if (newTextile == null)
            {
                return null;
            }
            context.Textiles.Add(newTextile);
            context.SaveChanges();
            return newTextile.GetViewModel;
        }
        public TextileViewModel? Update(TextileBindingModel model)
        {
            using var context = new GarmentFactoryDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var textile = context.Textiles.FirstOrDefault(rec => rec.Id == model.Id);
                if (textile == null)
                {
                    return null;
                }
                textile.Update(model);
                context.SaveChanges();
                textile.UpdateComponents(context, model);
                transaction.Commit();
                return textile.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public TextileViewModel? Delete(TextileBindingModel model)
        {
            using var context = new GarmentFactoryDatabase();
            var element = context.Textiles
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Textiles.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

    }
}
