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 Mango.Nop.Core.Loggers;

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

public class ShippingItemPalletDbTable : MeasuringItemPalletBaseDbTable<ShippingItemPallet>
{
    public ShippingItemPalletDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings)
        : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings)
    {
    }

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

    public IQueryable<ShippingItemPallet> GetAll(bool loadRelations)
    {
        return loadRelations
            ? GetAll()
                .LoadWith(sip => sip.ShippingItem).ThenLoad(si => si.ProductDto).ThenLoad(prod => prod.GenericAttributes)
                .LoadWith(sip => sip.ShippingItem).ThenLoad(si => si.ShippingDocument).ThenLoad(sd => sd.Shipping)
            //.LoadWith(sip => sip.ShippingItem).ThenLoad(si => si.ShippingDocument).ThenLoad(sd => sd.Shipping)
            //.LoadWith(sip => sip.ShippingItem).ThenLoad(si => si.ShippingDocument).ThenLoad(p => p.Partner)
            //.LoadWith(sip => sip.ShippingItem).ThenLoad(si => si.ShippingDocument).ThenLoad(sd => sd.ShippingDocumentToFiles).ThenLoad(sdtof => sdtof.ShippingDocumentFile)
            //.LoadWith(sip => sip.ShippingItem).ThenLoad(si => si.Product)
            : GetAll();
    }

    public Task<ShippingItemPallet> GetByIdAsync(int id, bool loadRelations)
        => GetAll(loadRelations).FirstOrDefaultAsync(sip => sip.Id == id);

    public IQueryable<ShippingItemPallet> GetAllByShippingItemIdAsync(int shippingItemId, bool loadRelations)
        => GetAll(loadRelations).Where(sip => sip.ShippingItemId == shippingItemId);
}