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

namespace FurnitureAssemblyDatabaseImplement.Implements
{
    public class FurnitureStorage : IFurnitureStorage
    {
        public List<FurnitureViewModel> GetFullList()
        {
            using var context = new FurnitureAssemblyDatabase();
            return context.Furnitures
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public List<FurnitureViewModel> GetFilteredList(FurnitureSearchModel model)
        {
            if (string.IsNullOrEmpty(model.FurnitureName))
            {
                return new();
            }
            using var context = new FurnitureAssemblyDatabase();
            return context.Furnitures
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.FurnitureName.Contains(model.FurnitureName))
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public FurnitureViewModel? GetElement(FurnitureSearchModel model)
        {
            if (string.IsNullOrEmpty(model.FurnitureName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new FurnitureAssemblyDatabase();
            return context.Furnitures
             .Include(x => x.Components)
             .ThenInclude(x => x.Component)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.FurnitureName) &&
            x.FurnitureName == model.FurnitureName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public FurnitureViewModel? Insert(FurnitureBindingModel model)
        {
            using var context = new FurnitureAssemblyDatabase();
            var newProduct = Furniture.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Furnitures.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }
        public FurnitureViewModel? Update(FurnitureBindingModel model)
        {
            using var context = new FurnitureAssemblyDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Furnitures.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 FurnitureViewModel? Delete(FurnitureBindingModel model)
        {
            using var context = new FurnitureAssemblyDatabase();
            var element = context.Furnitures.Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Furnitures.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }



    }
}