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.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 org.junit.jupiter.api.AfterEach;
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.category.support.SellerShopContext;
import com.ruoyi.web.modules.pay.dto.ShopWechatPaySaveDTO;
import com.ruoyi.web.modules.pay.service.IShopWechatPayService;
import com.ruoyi.web.modules.pay.vo.ShopWechatPayVO;
import com.ruoyi.web.modules.store.domain.BizShop;
import com.ruoyi.web.modules.store.mapper.BizShopMapper;

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

    @Mock
    private BizShopMapper shopMapper;

    @Spy
    @InjectMocks
    private SellerShopWechatPayController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp()
    {
        SellerShopContext.setShopId(101L);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        lenient().doReturn("seller01").when(controller).getUsername();
    }

    @AfterEach
    void tearDown()
    {
        SellerShopContext.clear();
    }

    @Test
    void getApply_withoutShopId_rejected()
    {
        SellerShopContext.clear();
        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(get("/agri/seller/wechat-pay")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(shopWechatPayService, never()).getByShopId(any());
    }

    @Test
    void getApply_returns200() throws Exception
    {
        ShopWechatPayVO vo = new ShopWechatPayVO();
        vo.setShopId(101L);
        vo.setCompanyName("测试商户");
        when(shopWechatPayService.getByShopId(101L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/wechat-pay"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.companyName").value("测试商户"));
    }

    @Test
    void submitApply_delegatesToService() throws Exception
    {
        BizShop shop = new BizShop();
        shop.setShopId(101L);
        shop.setMerchantId(501L);
        when(shopMapper.selectById(101L)).thenReturn(shop);
        ShopWechatPaySaveDTO dto = new ShopWechatPaySaveDTO();
        dto.setPayMode("1");
        dto.setCompanyName("测试公司");
        dto.setLegalPerson("张三");
        dto.setLicenseNo("91540000MA6T123456");
        dto.setContactName("李四");
        dto.setContactMobile("13800138000");

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

        verify(shopWechatPayService).submitApply(eq(101L), eq(501L), any(), eq("seller01"));
    }
}
