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

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.util.NestedServletException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.pay.dto.ShopWechatPayAuditRejectDTO;
import com.ruoyi.web.modules.pay.service.IShopWechatPayService;
import com.ruoyi.web.modules.pay.vo.ShopWechatPayPlatformVO;

@ExtendWith(MockitoExtension.class)
class PlatformShopWechatPayControllerTest
{
    @Mock
    private IShopWechatPayService shopWechatPayService;

    @Spy
    @InjectMocks
    private PlatformShopWechatPayController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        lenient().doReturn("platform01").when(controller).getUsername();
    }

    @Test
    void list_returns200() throws Exception
    {
        ShopWechatPayPlatformVO vo = new ShopWechatPayPlatformVO();
        vo.setId(1L);
        vo.setShopId(101L);
        vo.setCompanyName("测试公司");
        when(shopWechatPayService.selectPlatformList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/agri/shop/wechat-pay/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.rows[0].companyName").value("测试公司"));
    }

    @Test
    void pendingCount_returns200() throws Exception
    {
        when(shopWechatPayService.countPlatformPending()).thenReturn(3);

        mockMvc.perform(get("/agri/shop/wechat-pay/pendingCount"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.pendingCount").value(3));
    }

    @Test
    void getInfo_returns200() throws Exception
    {
        ShopWechatPayPlatformVO vo = new ShopWechatPayPlatformVO();
        vo.setId(1L);
        vo.setShopName("测试店铺");
        when(shopWechatPayService.selectPlatformDetail(1L)).thenReturn(vo);

        mockMvc.perform(get("/agri/shop/wechat-pay/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.shopName").value("测试店铺"));
    }

    @Test
    void approve_delegatesToService() throws Exception
    {
        mockMvc.perform(put("/agri/shop/wechat-pay/approve/101"))
                .andExpect(status().isOk());

        verify(shopWechatPayService).auditApprove(eq(101L), any());
    }

    @Test
    void reject_delegatesToService() throws Exception
    {
        ShopWechatPayAuditRejectDTO dto = new ShopWechatPayAuditRejectDTO();
        dto.setShopId(101L);
        dto.setAuditRemark("资料不全");

        mockMvc.perform(put("/agri/shop/wechat-pay/reject")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk());

        verify(shopWechatPayService).auditReject(any(), any());
    }

    @Test
    void approve_notPending_propagatesException()
    {
        doThrow(new ServiceException("当前申请不在审核中"))
                .when(shopWechatPayService).auditApprove(eq(101L), any());

        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(put("/agri/shop/wechat-pay/approve/101")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
    }

    @Test
    void reject_withoutRemark_propagatesException()
    {
        ShopWechatPayAuditRejectDTO dto = new ShopWechatPayAuditRejectDTO();
        dto.setShopId(101L);
        doThrow(new ServiceException("请填写驳回原因"))
                .when(shopWechatPayService).auditReject(any(), any());

        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(put("/agri/shop/wechat-pay/reject")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto)))
                        .andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(shopWechatPayService).auditReject(any(), any());
    }
}
