using FruitBank.Common.Entities;
using LinqToDB;
using Mango.Nop.Data.Repositories;
using Nop.Core.Caching;
using Nop.Core.Configuration;
using Nop.Core.Events;
using Nop.Data;
using static LinqToDB.Reflection.Methods.LinqToDB.Insert;

namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;

public class StockTakingDbTable : MgDbTableBase<StockTaking>
{
    public StockTakingDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings)
        : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings)
    {
    }

    public override IQueryable<StockTaking> GetAll() => base.GetAll();

    public IQueryable<StockTaking> GetAll(bool loadRelations)
    {
        return loadRelations
            ? GetAll()
                .LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes)
                //.LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.StockTakingItemPallets)
            : GetAll();//.LoadWith(st => st.StockTakingItems);
    }

    public Task<StockTaking> GetByIdAsync(int id, bool loadRelations)
        => GetAll(loadRelations).FirstOrDefaultAsync(st => st.Id == id);
}