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

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;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.web.modules.content.constant.MerchantEntryAgreementConstants;
import com.ruoyi.web.modules.content.service.IMerchantEntryAgreementService;
import com.ruoyi.web.modules.content.vo.MerchantEntryAgreementAppVO;
import com.ruoyi.web.modules.content.vo.MerchantEntryStatusVO;

/**
 * C 端入驻协议接口测试（MEA-API）
 */
@ExtendWith(MockitoExtension.class)
class MerchantEntryAgreementAppControllerTest
{
    @Mock
    private IMerchantEntryAgreementService agreementService;

    @InjectMocks
    private MerchantEntryAgreementAppController controller;

    private MockMvc mockMvc;

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

    @Test
    void agreement_enabled_returns200() throws Exception
    {
        MerchantEntryAgreementAppVO vo = new MerchantEntryAgreementAppVO();
        vo.setEnabled(true);
        vo.setAgreementTitle("农资商城商家入驻协议");
        vo.setContent("<p>正文</p>");
        vo.setCheckboxLabel("我已阅读并同意《农资商城商家入驻协议》（v1.0）");
        when(agreementService.getForApp()).thenReturn(vo);

        mockMvc.perform(get("/api/merchant/entry/agreement"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.enabled").value(true))
                .andExpect(jsonPath("$.data.content").value("<p>正文</p>"));
    }

    @Test
    void agreement_disabled_returns200() throws Exception
    {
        MerchantEntryAgreementAppVO vo = new MerchantEntryAgreementAppVO();
        vo.setEnabled(false);
        vo.setMessage(MerchantEntryAgreementConstants.MSG_ENTRY_CLOSED);
        when(agreementService.getForApp()).thenReturn(vo);

        mockMvc.perform(get("/api/merchant/entry/agreement"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.enabled").value(false))
                .andExpect(jsonPath("$.data.message").value(MerchantEntryAgreementConstants.MSG_ENTRY_CLOSED));
    }

    @Test
    void status_returns200() throws Exception
    {
        MerchantEntryStatusVO vo = new MerchantEntryStatusVO();
        vo.setEntryOpen(true);
        when(agreementService.getEntryStatus()).thenReturn(vo);

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