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.Date;
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_intersectsStatYear()
    {
        assertTrue(CommonProsperityScreenSupport.intersectsStatYear(published(2025, 1, 1, 2027, 12, 31), 2026));
        assertFalse(CommonProsperityScreenSupport.intersectsStatYear(published(2024, 1, 1, 2024, 12, 31), 2026));
        assertTrue(CommonProsperityScreenSupport.intersectsStatYear(published(2026, 1, 1, 2026, 12, 31), 2026));
    }

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

    @Test
    @DisplayName("DP-GTFY-UT-008 边界日期含端点")
    void ut008_boundaryDates()
    {
        assertTrue(CommonProsperityScreenSupport.intersectsStatYear(
                published(2026, 12, 31, 2027, 6, 1), 2026));
        assertTrue(CommonProsperityScreenSupport.intersectsStatYear(
                published(2025, 6, 1, 2026, 1, 1), 2026));
    }

    @Test
    @DisplayName("DP-GTFY-UT-009 分类占比")
    void ut009_typeStatsRatio()
    {
        List<BizCommonProsperityProject> projects = Arrays.asList(
                published(2026, 1, 1, 2026, 12, 31),
                published(2026, 1, 1, 2026, 12, 31),
                published(2026, 1, 1, 2026, 12, 31));
        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));
    }

    @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 y1, int m1, int d1, int y2, int m2, int d2)
    {
        BizCommonProsperityProject p = new BizCommonProsperityProject();
        p.setPublishStatus(1);
        p.setImplStartDate(date(y1, m1, d1));
        p.setImplEndDate(date(y2, m2, d2));
        p.setProjectType(1);
        return p;
    }

    private static Date date(int y, int m, int d)
    {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(y, m - 1, d, 0, 0, 0);
        return c.getTime();
    }
}
