using FruitBank.Common.Entities;
using LinqToDB;
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 OrderItemPalletDbTable : MeasuringItemPalletBaseDbTable<OrderItemPallet>
{
    public OrderItemPalletDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings)
        : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings)
    {
    }

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

    public IQueryable<OrderItemPallet> GetAll(bool loadRelations)
    {
        return loadRelations
            ? GetAll()
                .LoadWith(oip => oip.OrderItemDto).ThenLoad(oi => oi.OrderDto).ThenLoad(o => o.GenericAttributes)
                .LoadWith(oip => oip.OrderItemDto).ThenLoad(oi => oi.ProductDto).ThenLoad(p => p.GenericAttributes)
            : GetAll();
    }

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

    public IQueryable<OrderItemPallet> GetAllByOrderItemId(int orderItemId, bool loadRelations)
        => GetAll(loadRelations).Where(oip => oip.OrderItemId == orderItemId);

    public IQueryable<OrderItemPallet> GetAllByOrderId(int orderId)
        => GetAll(true).Where(oip => oip.OrderItemDto.OrderId == orderId);

    public IQueryable<OrderItemPallet> GetAllByProductId(int productId)
        => GetAll(true).Where(oip => oip.OrderItemDto.ProductId == productId);

}