package com.ruoyi.web.modules.screen.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.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProjectOpImage;
import com.ruoyi.web.modules.screen.domain.vo.ProjectTypeStatVo;

/**
 * DP-GTFY-UT-006～012、014
 */
@DisplayName("CommonProsperityScreenSupport")
class CommonProsperityScreenSupportTest
{

    @Test
    @DisplayName("DP-GTFY-UT-006 实施年限等于统计年")
    void ut006_matchesStatYear()
    {
        assertTrue(CommonProsperityScreenSupport.matchesStatYear(published(2026), 2026));
        assertFalse(CommonProsperityScreenSupport.matchesStatYear(published(2024), 2026));
        assertTrue(CommonProsperityScreenSupport.intersectsStatYear(published(2026), 2026));
    }

    @Test
    @DisplayName("DP-GTFY-UT-007 未发布排除")
    void ut007_unpublishedExcluded()
    {
        BizCommonProsperityProject p = published(2026);
        p.setPublishStatus(0);
        assertFalse(CommonProsperityScreenSupport.matchesStatYear(p, 2026));
    }

    @Test
    @DisplayName("DP-GTFY-UT-009 分类占比")
    void ut009_typeStatsRatio()
    {
        List<BizCommonProsperityProject> projects = Arrays.asList(
                published(2026), published(2026), published(2026));
        projects.get(0).setProjectType(1);
        projects.get(1).setProjectType(1);
        projects.get(2).setProjectType(2);
        List<ProjectTypeStatVo> stats = CommonProsperityScreenSupport.buildProjectTypeStats(projects);
        assertEquals(3, stats.size());
        assertEquals(2, stats.get(0).getCount().intValue());
        assertEquals(new BigDecimal("0.6667"), stats.get(0).getRatio());
        assertEquals(1, stats.get(1).getCount().intValue());
        BigDecimal sum = stats.stream().map(ProjectTypeStatVo::getRatio).reduce(BigDecimal.ZERO, BigDecimal::add);
        assertEquals(0, sum.compareTo(BigDecimal.ONE));
        assertEquals("和美乡村建设项目", stats.get(0).getProjectTypeName());
    }

    @Test
    @DisplayName("DP-GTFY-UT-010 无项目固定三类")
    void ut010_emptyTypeStats()
    {
        List<ProjectTypeStatVo> stats = CommonProsperityScreenSupport.buildProjectTypeStats(Collections.emptyList());
        assertEquals(3, stats.size());
        for (ProjectTypeStatVo s : stats)
        {
            assertEquals(0, s.getCount().intValue());
            assertEquals(BigDecimal.ZERO, s.getRatio());
        }
    }

    @Test
    @DisplayName("DP-GTFY-UT-011 可选年份含当前年")
    void ut011_availableYearsIncludesCurrent()
    {
        int current = Calendar.getInstance().get(Calendar.YEAR);
        List<Integer> years = CommonProsperityScreenSupport.buildAvailableYears(
                Collections.singletonList("2025"), Collections.emptyList());
        assertTrue(years.contains(current));
        assertTrue(years.contains(2025));
        assertEquals(current, years.get(0).intValue());
    }

    @Test
    @DisplayName("DP-GTFY-UT-013 运营图 sort_order")
    void ut013_opImageSort()
    {
        BizCommonProsperityProjectOpImage a = new BizCommonProsperityProjectOpImage();
        a.setProjectId(1L);
        a.setSortOrder(2);
        a.setFileUrl("b");
        BizCommonProsperityProjectOpImage b = new BizCommonProsperityProjectOpImage();
        b.setProjectId(1L);
        b.setSortOrder(1);
        b.setFileUrl("a");
        Map<Long, List<BizCommonProsperityProjectOpImage>> map =
                CommonProsperityScreenSupport.groupImagesByProjectId(Arrays.asList(a, b));
        assertEquals("a", map.get(1L).get(0).getFileUrl());
    }

    private static BizCommonProsperityProject published(int implYear)
    {
        BizCommonProsperityProject p = new BizCommonProsperityProject();
        p.setPublishStatus(1);
        p.setImplYear(implYear);
        p.setProjectType(1);
        return p;
    }
}
