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.MallServiceAgreementConstants;
import com.ruoyi.web.modules.content.service.IMallServiceAgreementService;
import com.ruoyi.web.modules.content.vo.MallServiceAgreementAppVO;
import com.ruoyi.web.modules.content.vo.MallServiceAgreementStatusVO;

/**
 * C 端商城服务协议接口测试（MSA-API）
 */
@ExtendWith(MockitoExtension.class)
class MallServiceAgreementAppControllerTest
{
    @Mock
    private IMallServiceAgreementService agreementService;

    @InjectMocks
    private MallServiceAgreementAppController controller;

    private MockMvc mockMvc;

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

    @Test
    void agreement_enabled_returns200() throws Exception
    {
        MallServiceAgreementAppVO vo = new MallServiceAgreementAppVO();
        vo.setEnabled(true);
        vo.setRegistrationOpen(true);
        vo.setRequireAgreementOnLogin(true);
        vo.setContent("<p>正文</p>");
        when(agreementService.getForApp()).thenReturn(vo);

        mockMvc.perform(get("/api/member/serviceAgreement"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.registrationOpen").value(true))
                .andExpect(jsonPath("$.data.requireAgreementOnLogin").value(true));
    }

    @Test
    void agreement_disabled_sp4() throws Exception
    {
        MallServiceAgreementAppVO vo = new MallServiceAgreementAppVO();
        vo.setEnabled(false);
        vo.setRegistrationOpen(false);
        vo.setRequireAgreementOnLogin(false);
        vo.setMessage(MallServiceAgreementConstants.MSG_REGISTRATION_CLOSED);
        when(agreementService.getForApp()).thenReturn(vo);

        mockMvc.perform(get("/api/member/serviceAgreement"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.requireAgreementOnLogin").value(false));
    }

    @Test
    void status_returns200() throws Exception
    {
        MallServiceAgreementStatusVO vo = new MallServiceAgreementStatusVO();
        vo.setRegistrationOpen(true);
        vo.setRequireAgreementOnLogin(true);
        when(agreementService.getStatus()).thenReturn(vo);

        mockMvc.perform(get("/api/member/serviceAgreement/status"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.registrationOpen").value(true));
    }
}
