package com.ruoyi.web.modules.industryservice.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 java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.industryservice.BreedingIndustryTestSamples;
import com.ruoyi.web.modules.industryservice.domain.dto.BreedingIndustryYakSnapshot;

@DisplayName("BreedingIndustryCalculator")
class BreedingIndustryCalculatorTest
{
    private static final LocalDate STAT_DATE = LocalDate.of(2026, 5, 20);

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-010 trim 与归一")
    void ut010_normalizeGender()
    {
        assertEquals(BreedingIndustryRules.GENDER_BULL, BreedingIndustryCalculator.normalizeGender(" 公 "));
        assertEquals(BreedingIndustryRules.GENDER_COW, BreedingIndustryCalculator.normalizeGender("母牛"));
    }

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-011 年龄三档边界")
    void ut011_ageBands()
    {
        assertEquals(BreedingIndustryRules.BAND_M1_6, BreedingIndustryCalculator.bandCodeOf(0));
        assertEquals(BreedingIndustryRules.BAND_M1_6, BreedingIndustryCalculator.bandCodeOf(6));
        assertEquals(BreedingIndustryRules.BAND_M7_12, BreedingIndustryCalculator.bandCodeOf(7));
        assertEquals(BreedingIndustryRules.BAND_M7_12, BreedingIndustryCalculator.bandCodeOf(12));
        assertEquals(BreedingIndustryRules.BAND_M13_PLUS, BreedingIndustryCalculator.bandCodeOf(13));
    }

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-012 占比零分母")
    void ut012_ratioZero()
    {
        assertEquals(new BigDecimal("0.0"), BreedingIndustryCalculator.ratioPercent(10, 0));
        assertEquals(new BigDecimal("30.0"), BreedingIndustryCalculator.ratioPercent(30, 100));
    }

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-013 历史月含已出栏")
    void ut013_historicalMonth()
    {
        LocalDate febEnd = LocalDate.of(2026, 2, 28);
        assertTrue(BreedingIndustryCalculator.countsAtMonthEnd(
                BreedingIndustryTestSamples.historicalOutbound(), febEnd));
    }

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-019 变更日≤月末排除")
    void ut019_outboundOnMonthEnd()
    {
        LocalDate febEnd = LocalDate.of(2026, 2, 28);
        assertFalse(BreedingIndustryCalculator.countsAtMonthEnd(
                BreedingIndustryTestSamples.outboundOnFebEnd(), febEnd));
    }

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-014 非正常且变更日空")
    void ut014_deadNoDate()
    {
        LocalDate any = LocalDate.of(2026, 1, 31);
        assertFalse(BreedingIndustryCalculator.countsAtMonthEnd(
                BreedingIndustryTestSamples.deadWithoutChangeDate(), any));
    }

    @Test
    @DisplayName("ZCZX-YZCYSJ-UT-004 pasture_id 空")
    void ut004_noPasture()
    {
        BreedingIndustryYakSnapshot row = BreedingIndustryTestSamples.normalBull();
        row.setPastureId(null);
        assertFalse(BreedingIndustryCalculator.countsAsCurrentInventory(row));
    }
}
