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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigDecimal;
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.prosperity.domain.BizCommonProsperityAchievement;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementAnnualReportVo;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementAnnualSummaryVo;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementMonthlyVo;

/**
 * GTFY-NB-UT-003～014；口径：1～最大月 + 最大月总览
 */
@DisplayName("AchievementReportAggregator")
class AchievementReportAggregatorTest
{

    @Test
    @DisplayName("GTFY-NB-UT-003 仅3月有数据：序列为1～3，缺月补零")
    void ut003_partialMonth()
    {
        List<AchievementMonthlyVo> monthly = AchievementReportAggregator.buildMonthly(2026,
                Collections.singletonList(row("2026-03", "10", 30)));
        assertEquals(3, monthly.size());
        assertEquals(0, monthly.get(0).getEmploymentDrivenCount().intValue());
        assertEquals(30, monthly.get(2).getEmploymentDrivenCount().intValue());
        assertEquals("2026-01", monthly.get(0).getStatPeriod());
        assertEquals("2026-03", monthly.get(2).getStatPeriod());
    }

    @Test
    @DisplayName("GTFY-NB-UT-004 12月均有数据")
    void ut004_fullYear()
    {
        List<BizCommonProsperityAchievement> rows = new java.util.ArrayList<>();
        for (int m = 1; m <= 12; m++)
        {
            rows.add(row(String.format("2026-%02d", m), String.valueOf(m), m));
        }
        List<AchievementMonthlyVo> monthly = AchievementReportAggregator.buildMonthly(2026, rows);
        assertEquals(12, monthly.size());
        assertEquals(new BigDecimal("3"), monthly.get(2).getCollectiveEconomyIncome());
    }

    @Test
    @DisplayName("GTFY-NB-UT-005 总览取最大月单月值")
    void ut005_summaryMaxMonth()
    {
        AchievementAnnualReportVo report = AchievementReportAggregator.buildReport(2026,
                Collections.singletonList(row("2026-03", "10", 30)));
        AchievementAnnualSummaryVo summary = report.getSummary();
        assertEquals(new BigDecimal("10.00"), summary.getCollectiveEconomyIncome());
        assertEquals(30, summary.getEmploymentDrivenCount().intValue());
    }

    @Test
    @DisplayName("GTFY-NB-UT-006 比率取最大月原值")
    void ut006_rateFromMaxMonth()
    {
        BizCommonProsperityAchievement r = row("2026-01", "0", 0);
        r.setDigitalPlatformUsageRate(new BigDecimal("0.6"));
        r.setBrandPremiumRate(BigDecimal.ZERO);
        AchievementAnnualSummaryVo summary = AchievementReportAggregator.buildReport(2026,
                Collections.singletonList(r)).getSummary();
        assertEquals(new BigDecimal("0.6000"), summary.getDigitalPlatformUsageRate());
    }

    @Test
    @DisplayName("GTFY-NB-UT-007 多月时总览取最大月")
    void ut007_multiMonthMax()
    {
        BizCommonProsperityAchievement m3 = row("2026-03", "10", 10);
        m3.setDigitalPlatformUsageRate(new BigDecimal("0.6"));
        BizCommonProsperityAchievement m6 = row("2026-06", "88", 50);
        m6.setDigitalPlatformUsageRate(new BigDecimal("0.75"));
        AchievementAnnualReportVo report =
                AchievementReportAggregator.buildReport(2026, Arrays.asList(m3, m6));
        assertEquals(6, report.getMonthly().size());
        assertEquals(new BigDecimal("88.00"), report.getSummary().getCollectiveEconomyIncome());
        assertEquals(new BigDecimal("0.7500"), report.getSummary().getDigitalPlatformUsageRate());
    }

    @Test
    @DisplayName("GTFY-NB-UT-008 全年无记录")
    void ut008_noData()
    {
        AchievementAnnualReportVo report = AchievementReportAggregator.buildReport(2026, Collections.emptyList());
        assertFalse(report.getHasData());
        assertNull(report.getSummary());
        assertEquals(0, report.getMonthly().size());
    }

    @Test
    @DisplayName("GTFY-NB-UT-009 至少一条")
    void ut009_hasData()
    {
        AchievementAnnualReportVo report = AchievementReportAggregator.buildReport(2026,
                Collections.singletonList(row("2026-05", "1", 1)));
        assertTrue(report.getHasData());
        assertTrue(report.getSummary() != null);
        assertEquals(5, report.getMonthly().size());
    }

    @Test
    @DisplayName("GTFY-NB-UT-010 跨年隔离")
    void ut010_crossYearFilter()
    {
        List<AchievementMonthlyVo> monthly = AchievementReportAggregator.buildMonthly(2026, Arrays.asList(
                row("2025-12", "99", 99),
                row("2026-01", "1", 1)));
        assertEquals(1, monthly.size());
        assertEquals(new BigDecimal("1"), monthly.get(0).getCollectiveEconomyIncome());
    }

    @Test
    @DisplayName("GTFY-NB-UT-011 stat_period 解析月份")
    void ut011_parseMonth()
    {
        assertEquals(9, AchievementReportAggregator.parseMonthFromStatPeriod("2026-09"));
        assertEquals(8, AchievementReportAggregator.resolveMaxMonth(2026,
                Collections.singletonList(row("2026-08", "1", 1))));
    }

    @Test
    @DisplayName("GTFY-NB-UT-012 monthly 月份顺序至最大月")
    void ut012_monthOrder()
    {
        List<AchievementMonthlyVo> monthly = AchievementReportAggregator.buildMonthly(2026, Arrays.asList(
                row("2026-08", "8", 8),
                row("2026-03", "3", 3),
                row("2026-01", "1", 1)));
        assertEquals(8, monthly.size());
        for (int i = 0; i < monthly.size(); i++)
        {
            assertEquals(i + 1, monthly.get(i).getMonth().intValue());
        }
    }

    @Test
    @DisplayName("GTFY-NB-UT-014 最大月收入为0仍展示")
    void ut014_zeroIncome()
    {
        BizCommonProsperityAchievement r = row("2026-01", "0", 5);
        AchievementAnnualSummaryVo summary = AchievementReportAggregator.buildReport(2026,
                Collections.singletonList(r)).getSummary();
        assertEquals(new BigDecimal("0.00"), summary.getCollectiveEconomyIncome());
        assertEquals(5, summary.getEmploymentDrivenCount().intValue());
    }

    private static BizCommonProsperityAchievement row(String statPeriod, String income, int employment)
    {
        BizCommonProsperityAchievement r = new BizCommonProsperityAchievement();
        r.setStatPeriod(statPeriod);
        r.setCollectiveEconomyIncome(new BigDecimal(income));
        r.setEmploymentDrivenCount(employment);
        r.setNewJobPositions(0);
        r.setEnvProjectCount(0);
        r.setNewGreenArea(BigDecimal.ZERO);
        r.setCulturalHeritageCount(0);
        r.setEthnicIntegrationProjectCount(0);
        r.setEthnicUnityActivityCount(0);
        r.setDigitalPlatformUsageRate(BigDecimal.ZERO);
        r.setRegionalPublicBrandCount(0);
        r.setBrandPremiumRate(BigDecimal.ZERO);
        return r;
    }
}
