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

import static org.mockito.ArgumentMatchers.eq;
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.Collections;

import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.trading.domain.vo.AppDistributorAmountTrendPointVo;
import com.ruoyi.web.modules.trading.domain.vo.AppDistributorHomeDashboardVo;
import com.ruoyi.web.modules.trading.domain.vo.AppDistributorTradeSummaryVo;
import com.ruoyi.web.modules.trading.service.IAppDistributorHomeService;
import com.ruoyi.web.modules.trading.support.AppDistributorHomeRules;
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.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@ExtendWith(MockitoExtension.class)
@DisplayName("承销商首页移动端 HTTP MockMvc")
class BizAppDistributorHomeControllerApiTest
{
    @Mock
    private IAppDistributorHomeService appDistributorHomeService;

    @InjectMocks
    private BizAppDistributorHomeController controller;

    @Test
    @DisplayName("GET /app/distributor/home/dashboard")
    void dashboard() throws Exception
    {
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();

        AppDistributorTradeSummaryVo summary = new AppDistributorTradeSummaryVo();
        summary.setOrderCount(2);
        summary.setTotalAmount(new BigDecimal("36800"));
        summary.setTotalHeads(3);
        summary.setCompletionRate(new BigDecimal("50.0"));
        summary.setAvgUnitPrice(new BigDecimal("12266.67"));

        AppDistributorAmountTrendPointVo point = new AppDistributorAmountTrendPointVo();
        point.setBucketLabel("13:00");
        point.setAmount(new BigDecimal("36800"));

        AppDistributorHomeDashboardVo vo = new AppDistributorHomeDashboardVo();
        vo.setRange(AppDistributorHomeRules.RANGE_TODAY);
        vo.setRangeName("今日");
        vo.setTrendGranularity(AppDistributorHomeRules.TREND_GRANULARITY_HOUR);
        vo.setSummary(summary);
        vo.setAmountTrend(Collections.singletonList(point));

        when(appDistributorHomeService.loadDashboard(eq("TODAY"))).thenReturn(vo);

        mockMvc.perform(get("/app/distributor/home/dashboard").param("range", "TODAY"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.range").value("TODAY"))
                .andExpect(jsonPath("$.data.summary.orderCount").value(2))
                .andExpect(jsonPath("$.data.trendGranularity").value("HOUR"));
    }
}
