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;

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

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

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

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

    public Task<StockTakingItem> GetByIdAsync(int stockTakingItemId, bool loadRelations) => GetAll(loadRelations).FirstOrDefaultAsync(sti => sti.Id == stockTakingItemId);

    public IQueryable<StockTakingItem> GetAllByProductId(int productId, bool loadRelations)
        => GetAll(loadRelations).Where(x => x.ProductId == productId);

    public IQueryable<StockTakingItem> GetAllByStockTakingId(int stockTakingId, bool loadRelations)
        => GetAll(loadRelations).Where(x => x.StockTakingId == stockTakingId);

}