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.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
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 org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.test.util.ReflectionTestUtils;
import com.ruoyi.web.modules.screen.domain.dto.TradeSalesOverviewDto;
import com.ruoyi.web.modules.screen.domain.vo.MallOrderTrendItemVo;
import com.ruoyi.web.modules.screen.domain.vo.MallOrderTrendVo;
import com.ruoyi.web.modules.screen.domain.vo.MallStatsOverviewVo;
import com.ruoyi.web.modules.screen.domain.vo.TradeSalesDashboardVo;
import com.ruoyi.web.modules.screen.mapper.HomeScreenStatsMapper;
import com.ruoyi.web.modules.screen.mapper.TradeSalesScreenMapper;
import com.ruoyi.web.modules.screen.mallstats.MallStatsOpenApiClient;
import com.ruoyi.web.modules.trading.domain.BizGradeWeightConfig;
import com.ruoyi.web.modules.trading.mapper.BizGradeWeightConfigMapper;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("TradeSalesScreenServiceImpl")
class TradeSalesScreenServiceImplTest
{
    @Mock
    private TradeSalesScreenMapper tradeSalesScreenMapper;

    @Mock
    private HomeScreenStatsMapper homeScreenStatsMapper;

    @Mock
    private BizGradeWeightConfigMapper bizGradeWeightConfigMapper;

    @Mock
    private MallStatsOpenApiClient mallStatsOpenApiClient;

    @InjectMocks
    private TradeSalesScreenServiceImpl service;

    @BeforeEach
    void stubMappers()
    {
        ReflectionTestUtils.setField(service, "defaultMarketName", "巴青牦牛交易市场");
        TradeSalesOverviewDto overview = new TradeSalesOverviewDto();
        overview.setOrderCount(2L);
        overview.setTradeHeads(8L);
        overview.setTradeAmount(new BigDecimal("160000"));
        when(tradeSalesScreenMapper.selectTradeOverview(any(), any())).thenReturn(overview);
        when(homeScreenStatsMapper.countSupplierForScreen()).thenReturn(5);
        when(tradeSalesScreenMapper.selectMonthlyTradeByYear(any(), any()))
                .thenReturn(Collections.emptyList());
        when(tradeSalesScreenMapper.selectSalesDestinationByYear(any(), any()))
                .thenReturn(Collections.emptyList());
        when(tradeSalesScreenMapper.selectQualityGradeByYear(any(), any()))
                .thenReturn(Collections.emptyList());
        when(tradeSalesScreenMapper.countGradedLinesInYear(any(), any())).thenReturn(0L);
        when(bizGradeWeightConfigMapper.selectAllOrderBySort()).thenReturn(Collections.emptyList());
        when(tradeSalesScreenMapper.selectTradeMarketIdByName("巴青牦牛交易市场")).thenReturn(null);
        when(tradeSalesScreenMapper.selectDistinctFinishYears()).thenReturn(Arrays.asList(2025, 2026));
        when(mallStatsOpenApiClient.fetchOverview(org.mockito.ArgumentMatchers.anyInt()))
                .thenReturn(null);
    }

    @Test
    @DisplayName("DP-JY-UT-009 供应商数来自 HomeScreenStatsMapper")
    void ut009_supplierFromHomeMapper()
    {
        TradeSalesDashboardVo vo = service.loadDashboard("2026");
        assertEquals(5, vo.getTradeOverview().getSupplierCount());
        assertEquals(2, vo.getTradeOverview().getOrderCount());
    }

    @Test
    @DisplayName("DP-JY-UT-019 无市场行情空态")
    void ut019_noMarketQuoteEmpty()
    {
        TradeSalesDashboardVo vo = service.loadDashboard("2026");
        assertFalse(vo.getOriginQuote().isHasQuoteData());
    }

    @Test
    @DisplayName("DP-JY-UT-020 商城未接入时 mallStatsAvailable=false")
    void ut020_mallUnavailable()
    {
        TradeSalesDashboardVo vo = service.loadDashboard("2026");
        assertFalse(vo.isMallStatsAvailable());
    }

    @Test
    @DisplayName("DP-JY-UT-021 商城 overview 合并农资订单量")
    void ut021_mallOverviewMerge()
    {
        MallStatsOverviewVo mall = new MallStatsOverviewVo();
        MallOrderTrendVo trend = new MallOrderTrendVo();
        MallOrderTrendItemVo jan = new MallOrderTrendItemVo();
        jan.setMonth(1);
        jan.setOrderCount(10L);
        MallOrderTrendItemVo feb = new MallOrderTrendItemVo();
        feb.setMonth(2);
        feb.setOrderCount(5L);
        trend.setItems(Arrays.asList(jan, feb));
        mall.setMallOrderTrend(trend);
        when(mallStatsOpenApiClient.fetchOverview(org.mockito.ArgumentMatchers.eq(2026)))
                .thenReturn(mall);

        TradeSalesDashboardVo vo = service.loadDashboard("2026");
        assertTrue(vo.isMallStatsAvailable());
        assertEquals(15L, vo.getTradeOverview().getAgriOrderCount().longValue());
    }
}
