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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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 org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
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.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.exception.GlobalExceptionHandler;
import com.ruoyi.web.modules.diagnosis.AiOnlineConsultTestSamples;
import com.ruoyi.web.modules.diagnosis.domain.AiConsultOpenBody;
import com.ruoyi.web.modules.diagnosis.domain.AiConsultSessionView;
import com.ruoyi.web.modules.diagnosis.service.IBizAiOnlineConsultService;
import com.ruoyi.web.modules.diagnosis.support.AiConsultConstants;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("AI诊断移动端 HTTP MockMvc")
class BizAppAiConsultControllerApiTest
{
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Mock
    private IBizAiOnlineConsultService bizAiOnlineConsultService;

    @InjectMocks
    private BizAppAiConsultController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setControllerAdvice(new GlobalExceptionHandler())
                .build();
        SysUser u = new SysUser();
        u.setUserId(50L);
        u.setUserName("herder");
        u.setNickName("牧民");
        LoginUser lu = new LoginUser(50L, 200L, u, Collections.emptySet());
        SecurityContextHolder.getContext().setAuthentication(
                new UsernamePasswordAuthenticationToken(lu, "", lu.getAuthorities()));
    }

    @AfterEach
    void tearDown()
    {
        SecurityContextHolder.clearContext();
        PageHelper.clearPage();
    }

    @Test
    @DisplayName("POST /session 含模板")
    void createWithCategory() throws Exception
    {
        AiConsultSessionView view = new AiConsultSessionView();
        view.setId(6001L);
        view.setPromptTemplate(AiConsultConstants.resolvePromptTemplate(2));
        when(bizAiOnlineConsultService.createAiSession(eq(50L), any(), any())).thenReturn(view);
        AiConsultOpenBody body = new AiConsultOpenBody();
        body.setCategory(2);
        mockMvc.perform(post("/app/consult/ai/session")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.promptTemplate").isNotEmpty());
    }

    @Test
    @DisplayName("GET /session/list")
    void list() throws Exception
    {
        when(bizAiOnlineConsultService.selectAiSessionList(any(), eq(50L)))
                .thenReturn(Collections.singletonList(AiOnlineConsultTestSamples.activeAiSession()));
        mockMvc.perform(get("/app/consult/ai/session/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.disclaimer").value(AiConsultConstants.DISCLAIMER));
    }

    @Test
    @DisplayName("POST /session/{id}/hide")
    void hide() throws Exception
    {
        when(bizAiOnlineConsultService.hideSession(AiOnlineConsultTestSamples.SESSION_ID, 50L)).thenReturn(1);
        mockMvc.perform(post("/app/consult/ai/session/" + AiOnlineConsultTestSamples.SESSION_ID + "/hide"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
