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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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 java.util.HashMap;
import java.util.Map;
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 com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.modules.entry.constant.MerchantEntryApplyConstants;
import com.ruoyi.web.modules.entry.dto.EntryApplyRejectDTO;
import com.ruoyi.web.modules.entry.service.IMerchantEntryApplyService;
import com.ruoyi.web.modules.entry.vo.EntryApplyDetailVO;

/**
 * 平台商户入驻审核接口测试（MEA-API）
 */
@ExtendWith(MockitoExtension.class)
class MerchantEntryApplyControllerTest
{
    @Mock
    private IMerchantEntryApplyService applyService;

    @Spy
    @InjectMocks
    private MerchantEntryApplyController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp() throws Exception
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        org.mockito.Mockito.lenient().doReturn("admin").when(controller).getUsername();
    }

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

        mockMvc.perform(get("/agri/merchantEntryApply/pendingCount"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.pendingCount").value(3));
    }

    @Test
    void getInfo_pending_canApprove() throws Exception
    {
        EntryApplyDetailVO vo = new EntryApplyDetailVO();
        vo.setApplyId(1L);
        vo.setApplyStatus(MerchantEntryApplyConstants.STATUS_PENDING);
        vo.setCanApprove(true);
        vo.setCanReject(true);
        when(applyService.selectDetail(1L)).thenReturn(vo);

        mockMvc.perform(get("/agri/merchantEntryApply/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.canApprove").value(true))
                .andExpect(jsonPath("$.data.canReject").value(true));
    }

    @Test
    void approve_returnsMerchantAndShop() throws Exception
    {
        Map<String, Object> data = new HashMap<>();
        data.put("merchantId", 10L);
        data.put("shopId", 20L);
        when(applyService.approve(eq(1L), any(), eq("admin"))).thenReturn(data);

        mockMvc.perform(put("/agri/merchantEntryApply/approve/1")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.merchantId").value(10))
                .andExpect(jsonPath("$.data.shopId").value(20));
    }

    @Test
    void reject_returns200() throws Exception
    {
        EntryApplyRejectDTO dto = new EntryApplyRejectDTO();
        dto.setApplyId(1L);
        dto.setRejectReason("资料不全");

        mockMvc.perform(put("/agri/merchantEntryApply/reject")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));

        verify(applyService).reject(any(EntryApplyRejectDTO.class), eq("admin"));
    }

    @Test
    void list_returns200() throws Exception
    {
        when(applyService.selectList(any())).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/agri/merchantEntryApply/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
