using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputerShopDatabaseImplement.Implements
{
    public class SupplyStorage : ISupplyStorage
    {
        public SupplyViewModel? Delete(SupplyBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var element = context.Supplies
            .Include(x => x.Orders)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Supplies.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public SupplyViewModel? GetElement(SupplySearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new ComputerShopDatabase();
            return context.Supplies
            .Include(x => x.Orders)
            .ThenInclude(x => x.Order).Include(x => x.Receiving)
            .FirstOrDefault(x =>
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }

        public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
        {
            if (!model.Id.HasValue && !model.ComponentId.HasValue)
            {
                return new();
            }
            using var context = new ComputerShopDatabase();
            if(!model.ComponentId.HasValue)
                return context.Supplies
                .Include(x => x.Orders)
                .ThenInclude(x => x.Order)
                .Include(x => x.Receiving)
                .Where(x => x.Id == model.Id)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            else 
                 return context.Supplies
                .Include(x => x.Orders)
                .ThenInclude(x => x.Order)
                .Include(x => x.Receiving)
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .Where(x => x.Components.Any(x => x.ComponentId == model.ComponentId))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<SupplyViewModel> GetFullList()
        {
            using var context = new ComputerShopDatabase();
            return context.Supplies
            .Include(x => x.Orders)
            .ThenInclude(x => x.Order)
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Include(x => x.Receiving)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public SupplyViewModel? Insert(SupplyBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var newProduct = Supply.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Supplies.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;

        }

        public SupplyViewModel? Update(SupplyBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Supplies.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return null;
                }
                product.Update(model);
                context.SaveChanges();
                product.UpdateOrders(context, model);
                transaction.Commit();
                return product.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
