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

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.test.core.ut.BaseWebUnitTest;
import com.cf.imes.module.system.controller.admin.funds.organamount.vo.TotalSalesAmountRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.SalesDetailRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.SalesDetailsPageReqVO;
import com.cf.imes.module.system.service.funds.purchase.PurchaseService;
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.List;

import static org.mockito.ArgumentMatchers.any;
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 9:53
 */
@WebMvcTest(controllers = SalesDetailsController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class SalesDetailsControllerTest extends BaseWebUnitTest {
    @Autowired
    private MockMvc mockMvc;

    @MockitoBean
    private PurchaseService purchaseService;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 获取销售明细分页
     */
    @Test
    void testGetManageSalesDetailsPage() throws Exception {
        // 构造请求参数
        SalesDetailsPageReqVO reqVO = new SalesDetailsPageReqVO();
        reqVO.setId(123L);
        reqVO.setProductPrice(new BigDecimal(100));

        // mock 返回数据
        SalesDetailRespVO respVO = new SalesDetailRespVO();
        respVO.setId(1L);
        respVO.setOrganName("测试组织");
        respVO.setPaymentMethod("支付宝");

        PageResult<SalesDetailRespVO> mockPage = new PageResult<>();
        mockPage.setTotal(1L);
        mockPage.setList(List.of(respVO));

        when(purchaseService.getManageSalesDetailsPage(any()))
                .thenReturn(mockPage);

        // 发起请求 & 断言
        mockMvc.perform(get("/system/sales/page")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(reqVO)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.total").value(1))
                .andExpect(jsonPath("$.data.list[0].id").value(1L))
                .andExpect(jsonPath("$.data.list[0].paymentMethod").value("支付宝"));

        // 验证 service 调用
        verify(purchaseService, times(1))
                .getManageSalesDetailsPage(any());
    }

    /**
     * 导出销售明细
     */
    @Test
    void testExportSalesDetailsPage() throws Exception {
        // 构造请求参数
        SalesDetailsPageReqVO reqVO = new SalesDetailsPageReqVO();
        reqVO.setId(123L);
        reqVO.setProductPrice(new BigDecimal(100));

        // mock 返回数据
        SalesDetailRespVO respVO = new SalesDetailRespVO();
        respVO.setId(1L);
        respVO.setOrganName("测试组织");
        respVO.setPaymentMethod("支付宝");

        PageResult<SalesDetailRespVO> mockPage = new PageResult<>();
        mockPage.setTotal(1L);
        mockPage.setList(List.of(respVO));

        when(purchaseService.getManageSalesDetailsPage(any()))
                .thenReturn(mockPage);

        // 发起请求
        mockMvc.perform(get("/system/sales/export")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(reqVO)))
                .andExpect(status().isOk());

        // 验证 service 调用
        verify(purchaseService, times(1))
                .getManageSalesDetailsPage(any());
    }

    /**
     * 获取管理端销售总额信息
     */
    @Test
    void testGetManageTotalSalesInfo() throws Exception {
        // mock 返回
        TotalSalesAmountRespVO respVO = new TotalSalesAmountRespVO(new BigDecimal("1000.00"));

        when(purchaseService.getTotalSales())
                .thenReturn(respVO);

        // 发起请求 & 断言
        mockMvc.perform(get("/system/sales/total")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.amount").value(1000.00));

        // 验证 service 调用
        verify(purchaseService, times(1))
                .getTotalSales();
    }
}
