package com.ruoyi.web.modules.screen.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityProject;
import com.ruoyi.web.modules.prosperity.domain.vo.AchievementAnnualReportVo;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityAchievementMapper;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectMapper;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityProjectOpImageMapper;
import com.ruoyi.web.modules.prosperity.service.IAchievementReportService;
import com.ruoyi.web.modules.prosperity.support.AchievementReportAggregator;
import com.ruoyi.web.modules.screen.domain.vo.CommonProsperityScreenDashboardVo;

/**
 * DP-GTFY-UT-014、015
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("CommonProsperityScreenServiceImpl")
class CommonProsperityScreenServiceImplTest
{

    @Mock
    private IAchievementReportService achievementReportService;

    @Mock
    private BizCommonProsperityAchievementMapper achievementMapper;

    @Mock
    private BizCommonProsperityProjectMapper projectMapper;

    @Mock
    private BizCommonProsperityProjectOpImageMapper opImageMapper;

    @InjectMocks
    private CommonProsperityScreenServiceImpl service;

    @Test
    @DisplayName("DP-GTFY-UT-014 仅项目无成果")
    void ut014_projectsOnly()
    {
        AchievementAnnualReportVo report = AchievementReportAggregator.buildReport(2026, Collections.emptyList());
        when(achievementReportService.getAnnualReport("2026")).thenReturn(report);
        BizCommonProsperityProject p = new BizCommonProsperityProject();
        p.setId(1L);
        p.setProjectType(1);
        p.setPublishStatus(1);
        when(projectMapper.selectPublishedForScreen(2026)).thenReturn(Collections.singletonList(p));
        when(achievementMapper.selectDistinctStatYears()).thenReturn(Collections.emptyList());
        when(projectMapper.selectDistinctStatYearsFromPublished()).thenReturn(Collections.singletonList("2026"));

        CommonProsperityScreenDashboardVo vo = service.loadDashboard("2026");
        assertFalse(vo.getAchievement().getHasData());
        assertNull(vo.getAchievement().getSummary());
        assertTrue(vo.getHasProjectData());
        assertEquals(3, vo.getProjectTypeStats().size());
    }

    @Test
    @DisplayName("DP-GTFY-UT-015 仅成果无项目")
    void ut015_achievementOnly()
    {
        AchievementAnnualReportVo report = AchievementReportAggregator.buildReport(2026,
                Collections.singletonList(sampleAchievementRow()));
        when(achievementReportService.getAnnualReport("2026")).thenReturn(report);
        when(projectMapper.selectPublishedForScreen(2026)).thenReturn(Collections.emptyList());
        when(achievementMapper.selectDistinctStatYears()).thenReturn(Collections.singletonList("2026"));
        when(projectMapper.selectDistinctStatYearsFromPublished()).thenReturn(Collections.emptyList());

        CommonProsperityScreenDashboardVo vo = service.loadDashboard("2026");
        assertTrue(vo.getAchievement().getHasData());
        assertFalse(vo.getHasProjectData());
        assertEquals(0, vo.getProjectTypeStats().get(0).getCount().intValue());
    }

    @Test
    @DisplayName("复用年度报告服务")
    void reusesAchievementReport()
    {
        when(achievementReportService.getAnnualReport("2026"))
                .thenReturn(AchievementReportAggregator.buildReport(2026, Collections.emptyList()));
        when(projectMapper.selectPublishedForScreen(2026)).thenReturn(Collections.emptyList());
        when(achievementMapper.selectDistinctStatYears()).thenReturn(Collections.emptyList());
        when(projectMapper.selectDistinctStatYearsFromPublished()).thenReturn(Collections.emptyList());

        service.loadDashboard("2026");
        verify(achievementReportService).getAnnualReport("2026");
        verify(projectMapper).selectPublishedForScreen(eq(2026));
    }

    @Test
    @DisplayName("列表含运营图查询")
    void listLoadsOpImages()
    {
        BizCommonProsperityProject p = new BizCommonProsperityProject();
        p.setId(9L);
        p.setProjectType(2);
        p.setPublishStatus(1);
        p.setProjectName("测试");
        when(projectMapper.selectPublishedForScreen(2026)).thenReturn(Collections.singletonList(p));
        when(opImageMapper.selectByProjectIds(any())).thenReturn(Collections.emptyList());

        assertNotNull(service.listPublishedProjects("2026"));
        verify(opImageMapper).selectByProjectIds(Arrays.asList(9L));
    }

    private static com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement sampleAchievementRow()
    {
        com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement r =
                new com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement();
        r.setStatPeriod("2026-03");
        r.setCollectiveEconomyIncome(new java.math.BigDecimal("10"));
        r.setEmploymentDrivenCount(1);
        r.setNewJobPositions(1);
        r.setEnvProjectCount(1);
        r.setNewGreenArea(new java.math.BigDecimal("1"));
        r.setCulturalHeritageCount(1);
        r.setEthnicIntegrationProjectCount(1);
        r.setEthnicUnityActivityCount(1);
        r.setDigitalPlatformUsageRate(new java.math.BigDecimal("0.1"));
        r.setRegionalPublicBrandCount(1);
        r.setBrandPremiumRate(new java.math.BigDecimal("0.1"));
        return r;
    }
}
