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


namespace JewelryStoreDatabaseImplement.Implements
{
    public class JewelStorage : IJewelStorage
    {
        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;
        }

        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 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 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 JewelViewModel? Insert(JewelBindingModel model)
        {
            using var context = new JewelryStoreDataBase();

            var newJewel = Jewel.Create(context, model);

            if (newJewel == null)
            {
                return null;
            }

            context.Jewels.Add(newJewel);
            context.SaveChanges();

            return newJewel.GetViewModel;
        }

        public JewelViewModel? Update(JewelBindingModel model)
        {
            using var context = new JewelryStoreDataBase();

            using var transaction = context.Database.BeginTransaction();

            try
            {
                var jewel = context.Jewels.FirstOrDefault(rec => rec.Id == model.Id);

                if (jewel == null)
                {
                    return null;
                }

                jewel.Update(model);
                context.SaveChanges();
                jewel.UpdateComponents(context, model);
                transaction.Commit();

                return jewel.GetViewModel;
            }
            catch
            {
                transaction.Rollback();

                throw;
            }
        }
    }
}
