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

    protected override void OnUpdate(ShippingItem entity)
    {
        PrepareValues(entity);
        base.OnUpdate(entity);
    }

    protected override void OnInsert(ShippingItem entity)
    {
        entity.MeasuringCount = entity.PalletsOnDocument;

        PrepareValues(entity);
        base.OnInsert(entity);
    }

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

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

    public IQueryable<ShippingItem> GetAllNotMeasured(bool loadRelations)
        => GetAll(loadRelations).Where(si => !si.IsMeasured);


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

    public IQueryable<ShippingItem> GetAllByProductIdAsync(int productid, bool loadRelations)
        => GetAll(loadRelations).Where(si => si.ProductId == productid);

    public IQueryable<ShippingItem> GetAllByShippingDocumentIdAsync(int shippingDocumentId, bool loadRelations)
        => GetAll(loadRelations).Where(si => si.ShippingDocumentId == shippingDocumentId);

    public IQueryable<ShippingItem> GetAllByShippingIdAsync(int shippingId, bool loadRelations)
        => GetAll(loadRelations).Where(si => si.ShippingDocument.ShippingId == shippingId);

    public IQueryable<ShippingItem> GetAllByPartnerIdAsync(int partnerId, bool loadRelations)
        => GetAll(loadRelations).Where(si => si.ShippingDocument.PartnerId == partnerId);

    private static void PrepareValues(ShippingItem shippingItem)
    {
        if (shippingItem.MeasuringCount < 1) shippingItem.MeasuringCount = 1;

        shippingItem.MeasuredNetWeight = double.Round(shippingItem.MeasuredNetWeight, 1);
        shippingItem.MeasuredGrossWeight = double.Round(shippingItem.MeasuredGrossWeight, 1);
    }
}