package com.ruoyi.web.modules.freight.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;

class FreightCalcSupportTest
{
    @Test
    void calcByPiece_withinFirstUnit()
    {
        FreightCalcRule rule = pieceRule("1", "8", "1", "2");
        assertEquals(0, FreightCalcSupport.calcByPiece(new BigDecimal("1"), rule).compareTo(new BigDecimal("8")));
    }

    @Test
    void calcByPiece_extraUnitsCeiling()
    {
        FreightCalcRule rule = pieceRule("1", "8", "1", "2");
        assertEquals(0, FreightCalcSupport.calcByPiece(new BigDecimal("3"), rule).compareTo(new BigDecimal("12")));
    }

    @Test
    void calcByWeight_withinFirstUnit()
    {
        FreightCalcRule rule = pieceRule("1", "10", "0.5", "3");
        assertEquals(0, FreightCalcSupport.calcByWeight(new BigDecimal("0.8"), rule).compareTo(new BigDecimal("10")));
    }

    @Test
    void calcByWeight_extraWeightCeiling()
    {
        FreightCalcRule rule = pieceRule("1", "10", "0.5", "3");
        assertEquals(0, FreightCalcSupport.calcByWeight(new BigDecimal("1.6"), rule).compareTo(new BigDecimal("16")));
    }

    @Test
    void isFreeByThreshold()
    {
        assertTrue(FreightCalcSupport.isFreeByThreshold(new BigDecimal("100"), new BigDecimal("99")));
        assertFalse(FreightCalcSupport.isFreeByThreshold(new BigDecimal("50"), new BigDecimal("99")));
    }

    @Test
    void buildFreightDesc_freeShipping()
    {
        assertEquals("包邮", FreightCalcSupport.buildFreightDesc(true, null, null));
    }

    @Test
    void buildFreightDesc_withThreshold()
    {
        assertEquals("运费 ¥8.00 起，满 ¥99.00 包邮",
                FreightCalcSupport.buildFreightDesc(false, new BigDecimal("8"), new BigDecimal("99")));
    }

    @Test
    void buildFreightDesc_goodsAmountMeetsThreshold()
    {
        assertEquals("包邮",
                FreightCalcSupport.buildFreightDesc(false, new BigDecimal("8"), new BigDecimal("99"),
                        new BigDecimal("120")));
    }

    private FreightCalcRule pieceRule(String firstUnit, String firstFee, String addUnit, String addFee)
    {
        FreightCalcRule rule = new FreightCalcRule();
        rule.setFirstUnit(new BigDecimal(firstUnit));
        rule.setFirstFee(new BigDecimal(firstFee));
        rule.setAddUnit(new BigDecimal(addUnit));
        rule.setAddFee(new BigDecimal(addFee));
        return rule;
    }
}
