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

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.post;
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.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.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.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.entry.constant.MerchantEntryApplyConstants;
import com.ruoyi.web.modules.entry.dto.EntryApplySubmitDTO;
import com.ruoyi.web.modules.entry.service.IMerchantEntryApplyService;
import com.ruoyi.web.modules.merchant.constant.MerchantConstants;
import com.ruoyi.web.modules.merchant.domain.BizMerchant;

/**
 * C 端商户入驻申请接口测试（MEA-API）
 */
@ExtendWith(MockitoExtension.class)
class MerchantEntryApplyAppControllerTest
{
    @Mock
    private IMerchantEntryApplyService applyService;

    @InjectMocks
    private MerchantEntryApplyAppController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        MemberContext.setMemberId(100L);
    }

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

    @Test
    void apply_returnsApplyInfo() throws Exception
    {
        Map<String, Object> data = new HashMap<>();
        data.put("applyId", 1L);
        data.put("applyNo", "EA20260101001");
        data.put("applyStatus", MerchantEntryApplyConstants.STATUS_PENDING);
        when(applyService.submitApply(eq(100L), org.mockito.ArgumentMatchers.any())).thenReturn(data);

        EntryApplySubmitDTO dto = new EntryApplySubmitDTO();
        dto.setAgreementAccepted(true);
        BizMerchant subject = new BizMerchant();
        subject.setMerchantType(MerchantConstants.TYPE_PERSON);
        subject.setPersonName("张三");
        dto.setSubject(subject);

        mockMvc.perform(post("/api/merchant/entry/apply")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"agreementAccepted\":true,\"subject\":{\"merchantType\":\"1\",\"personName\":\"张三\"},"
                        + "\"biz\":{\"merchantName\":\"店\",\"contactName\":\"张三\",\"contactPhone\":\"13800138000\"},"
                        + "\"shop\":{\"shopName\":\"旗舰店\"}}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.applyStatus").value("0"));

        verify(applyService).submitApply(eq(100L), org.mockito.ArgumentMatchers.any());
    }

    @Test
    void my_returnsList() throws Exception
    {
        when(applyService.listMyApplies(100L)).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/api/merchant/entry/my"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));

        verify(applyService).listMyApplies(100L);
    }
}
