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.AppBookingSubmitBody;
import com.ruoyi.web.modules.booking.domain.vo.AppBookingDateVo;
import com.ruoyi.web.modules.booking.domain.vo.AppointmentResourceVo;
import com.ruoyi.web.modules.booking.service.IAppBookingService;
import com.ruoyi.web.modules.booking.support.AppointmentResourceRules;
import com.ruoyi.web.modules.farming.domain.BizServiceAppointmentReview;

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

    @Mock
    private IAppBookingService appBookingService;

    @InjectMocks
    private BizAppBookingServiceController 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");
        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/bookingService/dates")
    void dates() throws Exception
    {
        AppBookingDateVo vo = new AppBookingDateVo();
        vo.setWeekdayName("周一");
        vo.setWeekday(1);
        vo.setDateMmDd("05-18");
        when(appBookingService.selectBookingDateList()).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/app/bookingService/dates"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0].weekday").value(1));
    }

    @Test
    @DisplayName("GET /app/bookingService/resource/list")
    void resourceList() throws Exception
    {
        AppointmentResourceVo vo = new AppointmentResourceVo();
        vo.setId(1L);
        vo.setResourceType(AppointmentResourceRules.TYPE_VET);
        vo.setResourceName("张兽医");
        when(appBookingService.selectAppointmentResourceList(any())).thenReturn(Collections.singletonList(vo));

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

    @Test
    @DisplayName("GET /app/bookingService/expert/{id}/reviews")
    void expertReviews() throws Exception
    {
        BizServiceAppointmentReview review = new BizServiceAppointmentReview();
        review.setReviewerName("牧民甲");
        review.setPunctualityScore(5);
        review.setAttitudeScore(5);
        review.setGuidanceScore(4);
        review.setSummary("很好");
        when(appBookingService.selectExpertReviewList(eq(9L))).thenReturn(Collections.singletonList(review));

        mockMvc.perform(get("/app/bookingService/expert/9/reviews"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].reviewerName").value("牧民甲"));
    }

    @Test
    @DisplayName("GET /app/bookingService/appointment/booked")
    void checkBooked() throws Exception
    {
        com.ruoyi.web.modules.booking.domain.vo.AppBookingDateBookedVo vo =
                new com.ruoyi.web.modules.booking.domain.vo.AppBookingDateBookedVo();
        vo.setBooked(true);
        when(appBookingService.checkBookedOnDate(eq(1), eq(5L), eq("2026-05-20"), eq("herder"))).thenReturn(vo);
        mockMvc.perform(get("/app/bookingService/appointment/booked")
                .param("providerType", "1")
                .param("providerId", "5")
                .param("appointDate", "2026-05-20"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.booked").value(true));
    }

    @Test
    @DisplayName("POST /app/bookingService/appointment")
    void submit() throws Exception
    {
        when(appBookingService.submitAppointment(any(), any())).thenReturn(500L);
        AppBookingSubmitBody body = new AppBookingSubmitBody();
        body.setResourceType(AppointmentResourceRules.TYPE_EXPERT);
        body.setResourceId(9L);
        body.setAppointDate("2026-05-22");
        body.setAppointeeName("扎西");
        body.setContactPhone("13800138000");
        body.setTimeSlot("08:00~10:00");
        body.setServiceAddress("县内");

        mockMvc.perform(post("/app/bookingService/appointment")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data").value(500));
    }
}
