package com.ruoyi.web.modules.booking.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.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.booking.domain.AppMyAppointmentReviewBody;
import com.ruoyi.web.modules.booking.domain.vo.AppMyAppointmentListVo;
import com.ruoyi.web.modules.booking.service.IAppMyAppointmentService;
import com.ruoyi.web.modules.diagnosis.support.ServiceAppointmentRules;
import com.ruoyi.web.modules.farming.domain.BizServiceAppointmentReview;

@ExtendWith(MockitoExtension.class)
@DisplayName("移动端我的预约 HTTP MockMvc")
class BizAppMyAppointmentControllerApiTest
{
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Mock
    private IAppMyAppointmentService appMyAppointmentService;

    @InjectMocks
    private BizAppMyAppointmentController 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("GET /app/myAppointment/list")
    void list() throws Exception
    {
        AppMyAppointmentListVo vo = new AppMyAppointmentListVo();
        vo.setId(1L);
        vo.setProviderName("张兽医");
        vo.setStatus(ServiceAppointmentRules.STATUS_PENDING);
        when(appMyAppointmentService.selectMyAppointmentList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/app/myAppointment/list")
                .param("providerType", "1")
                .param("statusTab", "all"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].providerName").value("张兽医"));
    }

    @Test
    @DisplayName("POST /app/myAppointment/cancel/{id}")
    void cancel() throws Exception
    {
        when(appMyAppointmentService.cancelAppointment(eq(2L), eq("herder"), eq("herder"))).thenReturn(1);
        mockMvc.perform(post("/app/myAppointment/cancel/2"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    @DisplayName("POST /app/myAppointment/{id}/review")
    void submitReview() throws Exception
    {
        when(appMyAppointmentService.submitAppointmentReview(eq(3L), any(), eq("herder"), eq("扎西")))
                .thenReturn(88L);
        AppMyAppointmentReviewBody body = new AppMyAppointmentReviewBody();
        body.setPunctualityScore(5);
        body.setAttitudeScore(5);
        body.setGuidanceScore(5);
        body.setSummary("满意");
        mockMvc.perform(post("/app/myAppointment/3/review")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data").value(88));
    }

    @Test
    @DisplayName("GET /app/myAppointment/{id}/review")
    void getReview() throws Exception
    {
        BizServiceAppointmentReview review = new BizServiceAppointmentReview();
        review.setReviewerName("扎西");
        review.setSummary("很好");
        when(appMyAppointmentService.selectAppointmentReview(4L, "herder")).thenReturn(review);
        mockMvc.perform(get("/app/myAppointment/4/review"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.reviewerName").value("扎西"));
    }
}
