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 java.util.HashSet;
import java.util.Set;
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.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.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
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.OrgMyAppointmentTestSamples;
import com.ruoyi.web.modules.diagnosis.domain.BizServiceAppointment;
import com.ruoyi.web.modules.diagnosis.service.IBizOrgMyAppointmentService;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("我的预约（机构）HTTP 接口 MockMvc")
class BizOrgMyAppointmentControllerApiTest
{
    @Mock
    private IBizOrgMyAppointmentService bizOrgMyAppointmentService;

    @InjectMocks
    private BizOrgMyAppointmentController controller;

    private MockMvc mockMvc;

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

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

    private static void loginAsAllPermissions()
    {
        SysUser u = new SysUser();
        u.setUserId(OrgMyAppointmentTestSamples.SYS_USER_ID);
        u.setUserName("orgTester");
        u.setPassword("x");
        Set<String> perms = new HashSet<>();
        perms.add(Constants.ALL_PERMISSION);
        LoginUser lu = new LoginUser(OrgMyAppointmentTestSamples.SYS_USER_ID, 102L, u, perms);
        UsernamePasswordAuthenticationToken auth =
                new UsernamePasswordAuthenticationToken(lu, "", lu.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(auth);
    }

    @Test
    @DisplayName("GET /list")
    void list() throws Exception
    {
        BizServiceAppointment row = OrgMyAppointmentTestSamples.bookedAppointment();
        row.setStatusLabel("已预约");
        when(bizOrgMyAppointmentService.selectOrgAppointmentList(any(), eq(OrgMyAppointmentTestSamples.SYS_USER_ID)))
                .thenReturn(Collections.singletonList(row));
        mockMvc.perform(get("/diseaseTreatment/myAppointment/org/list")
                .param("appointeeName", "才")
                .param("startDate", "2026-05-01")
                .param("endDate", "2026-05-31")
                .param("status", "5"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    @DisplayName("GET /{id}")
    void getInfo() throws Exception
    {
        BizServiceAppointment row = OrgMyAppointmentTestSamples.bookedAppointment();
        row.setStatusLabel("已预约");
        when(bizOrgMyAppointmentService.selectOrgAppointmentById(2000L, OrgMyAppointmentTestSamples.SYS_USER_ID))
                .thenReturn(row);
        mockMvc.perform(get("/diseaseTreatment/myAppointment/org/2000"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.appointeeName").value("才让"))
                .andExpect(jsonPath("$.data.statusLabel").value("已预约"));
    }

    @Test
    @DisplayName("POST /complete/{id}")
    void complete() throws Exception
    {
        when(bizOrgMyAppointmentService.complete(eq(2000L), eq(OrgMyAppointmentTestSamples.SYS_USER_ID), eq("orgTester")))
                .thenReturn(1);
        mockMvc.perform(post("/diseaseTreatment/myAppointment/org/complete/2000"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    @DisplayName("无确认接口")
    void noConfirmEndpoint() throws Exception
    {
        mockMvc.perform(post("/diseaseTreatment/myAppointment/org/confirm/2000"))
                .andExpect(status().isNotFound());
    }

    @Test
    @DisplayName("无拒绝接口")
    void noRejectEndpoint() throws Exception
    {
        mockMvc.perform(post("/diseaseTreatment/myAppointment/org/reject/2000"))
                .andExpect(status().isNotFound());
    }
}
