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.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.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.VetOnlineConsultTestSamples;
import com.ruoyi.web.modules.diagnosis.domain.ConsultOpenBody;
import com.ruoyi.web.modules.diagnosis.domain.vo.AppOnlineConsultVetListVo;
import com.ruoyi.web.modules.diagnosis.service.IAppOnlineConsultService;
import com.ruoyi.web.modules.diagnosis.service.IBizVetOnlineConsultService;

@ExtendWith(MockitoExtension.class)
@DisplayName("移动端在线问诊 HTTP MockMvc")
class BizAppOnlineConsultControllerApiTest
{
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Mock
    private IAppOnlineConsultService appOnlineConsultService;

    @Mock
    private IBizVetOnlineConsultService bizVetOnlineConsultService;

    @InjectMocks
    private BizAppOnlineConsultController controller;

    private MockMvc mockMvc;

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

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

    @Test
    @DisplayName("GET /app/onlineConsult/vet/list")
    void vetList() throws Exception
    {
        AppOnlineConsultVetListVo vo = new AppOnlineConsultVetListVo();
        vo.setId(101L);
        vo.setResourceName("张兽医");
        when(appOnlineConsultService.selectOnlineConsultVetList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/app/onlineConsult/vet/list").param("resourceName", "张"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].resourceName").value("张兽医"));
    }

    @Test
    @DisplayName("GET /app/onlineConsult/session/list")
    void sessionList() throws Exception
    {
        when(bizVetOnlineConsultService.selectAskerSessionList(any()))
                .thenReturn(Collections.singletonList(VetOnlineConsultTestSamples.activeSession()));
        mockMvc.perform(get("/app/onlineConsult/session/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].id").value(5001));
    }

    @Test
    @DisplayName("POST /app/onlineConsult/session/open")
    void openSession() throws Exception
    {
        when(bizVetOnlineConsultService.openOrGetVetSession(eq(101L), eq(50L), eq("扎西")))
                .thenReturn(VetOnlineConsultTestSamples.activeSession());
        ConsultOpenBody body = new ConsultOpenBody();
        body.setVetResourceId(101L);
        mockMvc.perform(post("/app/onlineConsult/session/open")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.id").value(5001));
    }
}
