package com.ruoyi.web.modules.screen.controller;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.AfterEach;
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.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.web.modules.screen.domain.vo.TradeSalesDashboardVo;
import com.ruoyi.web.modules.screen.domain.vo.TradeSalesMonthlyTrendVo;
import com.ruoyi.web.modules.screen.domain.vo.TradeSalesOriginQuoteVo;
import com.ruoyi.web.modules.screen.domain.vo.TradeSalesOverviewVo;
import com.ruoyi.web.modules.screen.domain.vo.TradeSalesQualityGradeVo;
import com.ruoyi.web.modules.screen.service.ITradeSalesScreenService;
import com.ruoyi.web.modules.screen.support.TradeSalesScreenSupport;

/**
 * DP-JY-API-001～016
 */
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("大屏交易销售 HTTP 接口 MockMvc")
class TradeSalesScreenControllerApiTest
{
    @Mock
    private ITradeSalesScreenService tradeSalesScreenService;

    @InjectMocks
    private TradeSalesScreenController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        loginAsAllPermissions();
    }

    @AfterEach
    void tearDown()
    {
        SecurityContextHolder.clearContext();
    }

    @Test
    @DisplayName("DP-JY-API-001 GET dashboard 有数据")
    void api001_dashboard() throws Exception
    {
        when(tradeSalesScreenService.loadDashboard("2026")).thenReturn(sampleDashboard());
        mockMvc.perform(get("/bigScreen/tradeSales/dashboard").param("statYear", "2026"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.statYear").value(2026))
                .andExpect(jsonPath("$.data.tradeOverview.orderCount").value(12))
                .andExpect(jsonPath("$.data.tradeMonthlyTrend.length()").value(12))
                .andExpect(jsonPath("$.data.salesDestination.length()").value(3));
    }

    @Test
    @DisplayName("DP-JY-API-016 statYear 回显")
    void api016_statYearEcho() throws Exception
    {
        TradeSalesDashboardVo vo = sampleDashboard();
        vo.setStatYear(2025);
        when(tradeSalesScreenService.loadDashboard("2025")).thenReturn(vo);
        mockMvc.perform(get("/bigScreen/tradeSales/dashboard").param("statYear", "2025"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.statYear").value(2025));
    }

    private static TradeSalesDashboardVo sampleDashboard()
    {
        TradeSalesDashboardVo vo = new TradeSalesDashboardVo();
        vo.setStatYear(2026);
        vo.setStatDate("2026-05-20");
        vo.setAvailableYears(Arrays.asList(2026, 2025));

        TradeSalesOverviewVo overview = new TradeSalesOverviewVo();
        overview.setOrderCount(12);
        overview.setTradeHeads(48);
        overview.setTradeAmount(new BigDecimal("960000.00"));
        overview.setSupplierCount(5);
        vo.setTradeOverview(overview);

        TradeSalesOriginQuoteVo quote = new TradeSalesOriginQuoteVo();
        quote.setHasQuoteData(true);
        quote.setAvgPrice7d(new BigDecimal("32.50"));
        quote.setDailyTrend(Collections.emptyList());
        vo.setOriginQuote(quote);

        TradeSalesMonthlyTrendVo month = new TradeSalesMonthlyTrendVo();
        month.setMonth(1);
        month.setTradeHeads(10);
        month.setTradeAmount(new BigDecimal("200000"));
        vo.setTradeMonthlyTrend(Collections.nCopies(12, month));

        vo.setSalesDestination(TradeSalesScreenSupport.buildSalesDestination(Collections.emptyList(), 48));

        TradeSalesQualityGradeVo grade = new TradeSalesQualityGradeVo();
        grade.setHasGradeData(true);
        grade.setTotalHeads(48);
        grade.setItems(Collections.emptyList());
        vo.setQualityGrade(grade);
        return vo;
    }

    private static void loginAsAllPermissions()
    {
        SysUser u = new SysUser();
        u.setUserId(1L);
        u.setUserName("tester");
        u.setPassword("x");
        Set<String> perms = new HashSet<>();
        perms.add(Constants.ALL_PERMISSION);
        LoginUser lu = new LoginUser(1L, 100L, u, perms);
        UsernamePasswordAuthenticationToken auth =
                new UsernamePasswordAuthenticationToken(lu, "", lu.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(auth);
    }
}
