package com.cf.imes.module.system.controller.admin.funds.organamount;

import com.cf.imes.framework.common.enums.StatisticsUnit;
import com.cf.imes.framework.test.core.ut.BaseWebUnitTest;
import com.cf.imes.module.system.controller.admin.funds.organamount.vo.ManageAmountOverviewRespVO;
import com.cf.imes.module.system.service.funds.organamount.OrganAmountService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
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;

/**
 * 组织资金 controller 单元测试
 *
 * @author Gqr
 * @since 2026/1/7 10:26
 */
@WebMvcTest(
        controllers = OrganAmountController.class,
        excludeAutoConfiguration = SecurityAutoConfiguration.class
)
class OrganAmountControllerTest extends BaseWebUnitTest {
    @Autowired
    private MockMvc mockMvc;

    @MockitoBean
    private OrganAmountService organAmountService;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 获取管理端资金总览
     */
    @Test
    void testGetManageTotalAmount() throws Exception {
        // mock 返回
        ManageAmountOverviewRespVO respVO = new ManageAmountOverviewRespVO();
        respVO.setToExamineAmount(new BigDecimal("10000.00"));
        respVO.setTotalSalesAmount(new BigDecimal("8000.00"));

        when(organAmountService.getOrganAmountAll())
                .thenReturn(respVO);

        mockMvc.perform(get("/system/amount/manage/total")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.toExamineAmount").value(10000.00))
                .andExpect(jsonPath("$.data.totalSalesAmount").value(8000.00));

        verify(organAmountService, times(1))
                .getOrganAmountAll();
    }

    /**
     * 获取组织消费趋势（非管理端）
     */
    @Test
    void testGetOrganConsumptionTrend() throws Exception {
        // 构造返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("dateList", List.of("2025-01", "2025-02"));
        result.put("series", List.of(100, 200));

        when(organAmountService.getConsumptionTrend(any(), eq(true)))
                .thenReturn(result);

        mockMvc.perform(get("/system/amount/statistics/consumptionTrend")
                        .contentType(MediaType.APPLICATION_JSON)
                        .param("createTime", "2025-01-01")
                        .param("createTime", "2025-02-28")
                        .param("unit", StatisticsUnit.MONTH.getValue().toString()))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.dateList[0]").value("2025-01"))
                .andExpect(jsonPath("$.data.series[1]").value(200));

        verify(organAmountService, times(1))
                .getConsumptionTrend(any(), eq(true));
    }

    /**
     * 获取管理端消费趋势
     */
    @Test
    void testGetManageConsumptionTrend() throws Exception {
        Map<String, Object> result = new HashMap<>();
        result.put("dateList", List.of("2025-01"));
        result.put("series", List.of(999));

        when(organAmountService.getConsumptionTrend(any(), eq(false)))
                .thenReturn(result);

        mockMvc.perform(get("/system/amount/manage/statistics/consumptionTrend")
                        .contentType(MediaType.APPLICATION_JSON)
                        .param("createTime", "2025-01-01")
                        .param("createTime", "2025-02-28")
                        .param("unit", StatisticsUnit.MONTH.getValue().toString()))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.dateList[0]").value("2025-01"))
                .andExpect(jsonPath("$.data.series[0]").value(999));

        verify(organAmountService, times(1))
                .getConsumptionTrend(any(), eq(false));
    }
}
