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.AppSupplierAmountTrendPointVo;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierHomeDashboardVo;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierSettlementSummaryVo;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierTradeSummaryVo;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierYakStatusVo;
import com.ruoyi.web.modules.trading.service.IAppSupplierHomeService;
import com.ruoyi.web.modules.trading.support.AppSupplierHomeRules;
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 BizAppSupplierHomeControllerApiTest
{
    @Mock
    private IAppSupplierHomeService appSupplierHomeService;

    @InjectMocks
    private BizAppSupplierHomeController controller;

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

        AppSupplierTradeSummaryVo trade = new AppSupplierTradeSummaryVo();
        trade.setOrderCount(2L);
        trade.setTotalAmount(new BigDecimal("74300"));
        trade.setTotalHeads(6L);
        trade.setPayingBuyerCount(2L);

        AppSupplierSettlementSummaryVo settlement = new AppSupplierSettlementSummaryVo();
        settlement.setPayableAmount(new BigDecimal("36500"));
        settlement.setServiceFeeAmount(new BigDecimal("600"));

        AppSupplierAmountTrendPointVo point = new AppSupplierAmountTrendPointVo();
        point.setBucketLabel("16:00");
        point.setAmount(new BigDecimal("74300"));

        AppSupplierYakStatusVo yakStatus = new AppSupplierYakStatusVo();
        yakStatus.setTradedHeads(129L);
        yakStatus.setIsolationEarTags(0L);
        yakStatus.setEnteredPenEarTags(30L);
        yakStatus.setPendingPayHeads(4L);

        AppSupplierHomeDashboardVo vo = new AppSupplierHomeDashboardVo();
        vo.setRange(AppSupplierHomeRules.RANGE_TODAY);
        vo.setRangeName("今日");
        vo.setTrendGranularity(AppSupplierHomeRules.TREND_GRANULARITY_HOUR);
        vo.setTradeSummary(trade);
        vo.setSettlementSummary(settlement);
        vo.setAmountTrend(Collections.singletonList(point));
        vo.setYakStatus(yakStatus);

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

        mockMvc.perform(get("/app/supplier/home/dashboard").param("range", "TODAY"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.range").value("TODAY"))
                .andExpect(jsonPath("$.data.tradeSummary.orderCount").value(2))
                .andExpect(jsonPath("$.data.settlementSummary.payableAmount").value(36500))
                .andExpect(jsonPath("$.data.yakStatus.tradedHeads").value(129))
                .andExpect(jsonPath("$.data.trendGranularity").value("HOUR"));
    }
}
