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

namespace JewelryStoreDatabaseImplement.Implements
{
    public class JewelStorage : IJewelStorage
    {
        public List<JewelViewModel> GetFullList()
        {
            using var context = new JewelryStoreDatabase();
            return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
        }
        public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
        {
            if (string.IsNullOrEmpty(model.JewelName))
            {
                return new();
            }
            using var context = new JewelryStoreDatabase();
            return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component)
            .Where(x => x.JewelName.Contains(model.JewelName)).ToList().Select(x => x.GetViewModel).ToList();
        }
        public JewelViewModel? GetElement(JewelSearchModel model)
        {
            if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new JewelryStoreDatabase();
            return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) &&
            x.JewelName == model.JewelName) ||(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }
        public JewelViewModel? Insert(JewelBindingModel model)
        {
            using var context = new JewelryStoreDatabase();
            var newProduct = Jewel.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Jewels.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }
        public JewelViewModel? Update(JewelBindingModel model)
        {
            using var context = new JewelryStoreDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Jewels.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return null;
                }
                product.Update(model);
                context.SaveChanges();
                product.UpdateComponents(context, model);
                transaction.Commit();
                return product.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public JewelViewModel? Delete(JewelBindingModel model)
        {
            using var context = new JewelryStoreDatabase();
            var element = context.Jewels
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Jewels.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

    }
}
