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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
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.screen.domain.dto.LivestockResourceOverviewDto;
import com.ruoyi.web.modules.screen.domain.dto.LivestockResourceUserStructureDto;
import com.ruoyi.web.modules.screen.domain.vo.LivestockResourceDashboardVo;
import com.ruoyi.web.modules.screen.mapper.LivestockResourceScreenMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("LivestockResourceScreenServiceImpl")
class LivestockResourceScreenServiceImplTest
{
    @Mock
    private LivestockResourceScreenMapper livestockResourceScreenMapper;

    @InjectMocks
    private LivestockResourceScreenServiceImpl service;

    @Test
    @DisplayName("DP-XM-UT-007 加载看板结构")
    void loadDashboardStructure()
    {
        LivestockResourceOverviewDto year = new LivestockResourceOverviewDto();
        year.setTotalUsers(5L);
        year.setTotalSessions(10L);
        year.setTotalQuestionCount(20L);
        year.setAvgCostTimeMs(3000.0);
        when(livestockResourceScreenMapper.selectYearOverviewMetrics(any(), any())).thenReturn(year);
        when(livestockResourceScreenMapper.countTodayActiveUsers(any(), any())).thenReturn(2L);
        when(livestockResourceScreenMapper.countTodayNewUsers(any(), any())).thenReturn(1L);
        when(livestockResourceScreenMapper.countTodaySessions(any(), any())).thenReturn(1L);
        when(livestockResourceScreenMapper.countTodayQuestionCount(any(), any())).thenReturn(2L);
        when(livestockResourceScreenMapper.selectDailySessionCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectDailyAiReplyCounts(any(), any())).thenReturn(Collections.emptyList());
        LivestockResourceUserStructureDto user = new LivestockResourceUserStructureDto();
        user.setNewUserCount(1L);
        user.setOldUserCount(1L);
        when(livestockResourceScreenMapper.selectUserStructure()).thenReturn(user);
        when(livestockResourceScreenMapper.countAskerMessagesInYear(any(), any())).thenReturn(0L);
        when(livestockResourceScreenMapper.selectAskerCategoryCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectHourlySessionCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectHourlyAiReplyCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.countYearSessions(any(), any())).thenReturn(10L);
        when(livestockResourceScreenMapper.selectSessionReplyBuckets(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.countAiRepliesOk(any(), any())).thenReturn(15L);
        when(livestockResourceScreenMapper.countAiRepliesFail(any(), any())).thenReturn(5L);
        when(livestockResourceScreenMapper.selectTextAskerContents(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectDistinctSessionYears()).thenReturn(Arrays.asList(2026, 2025));

        int currentYear = java.time.LocalDate.now().getYear();
        LivestockResourceDashboardVo vo = service.loadDashboard(String.valueOf(currentYear));
        assertEquals(currentYear, vo.getStatYear());
        assertNotNull(vo.getStatDate());
        assertNotNull(vo.getOverview());
        assertEquals(7, vo.getActivityTrend().getDailySeries().size());
        assertEquals(24, vo.getHourlyHeat().getHourlySeries().size());
        assertEquals(5, vo.getCategoryShare().getItems().size());
        assertEquals(4, vo.getSessionReplyDistribution().size());
    }

    @Test
    @DisplayName("DP-XM-UT-008 历史年今日指标为 0")
    void historicalYearTodayZero()
    {
        LivestockResourceOverviewDto year = new LivestockResourceOverviewDto();
        year.setTotalUsers(1L);
        year.setTotalSessions(1L);
        year.setTotalQuestionCount(1L);
        when(livestockResourceScreenMapper.selectYearOverviewMetrics(any(), any())).thenReturn(year);
        when(livestockResourceScreenMapper.selectDailySessionCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectDailyAiReplyCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectUserStructure()).thenReturn(new LivestockResourceUserStructureDto());
        when(livestockResourceScreenMapper.countAskerMessagesInYear(any(), any())).thenReturn(0L);
        when(livestockResourceScreenMapper.selectAskerCategoryCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectHourlySessionCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectHourlyAiReplyCounts(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.countYearSessions(any(), any())).thenReturn(0L);
        when(livestockResourceScreenMapper.selectSessionReplyBuckets(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.countAiRepliesOk(any(), any())).thenReturn(0L);
        when(livestockResourceScreenMapper.countAiRepliesFail(any(), any())).thenReturn(0L);
        when(livestockResourceScreenMapper.selectTextAskerContents(any(), any())).thenReturn(Collections.emptyList());
        when(livestockResourceScreenMapper.selectDistinctSessionYears()).thenReturn(Collections.singletonList(2020));

        LivestockResourceDashboardVo vo = service.loadDashboard("2020");
        assertEquals(0, vo.getOverview().getTodayActiveUsers());
        assertEquals(0, vo.getOverview().getTodaySessions());
        assertEquals("2020-12-31", vo.getStatDate());
    }
}
