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

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.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.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.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.architecture.domain.AppMedicationWithdrawalCalculateVo;
import com.ruoyi.web.modules.architecture.service.IAppMedicationDrugService;
import com.ruoyi.web.modules.architecture.service.IAppMedicationWithdrawalService;

@ExtendWith(MockitoExtension.class)
@DisplayName("计算休药期移动端 HTTP MockMvc")
class BizAppMedicationWithdrawalControllerApiTest
{
    @Mock
    private IAppMedicationDrugService appMedicationDrugService;

    @Mock
    private IAppMedicationWithdrawalService appMedicationWithdrawalService;

    @InjectMocks
    private BizAppMedicationWithdrawalController controller;

    private MockMvc mockMvc;

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

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

    @Test
    @DisplayName("GET /app/medication/withdrawal/calculate")
    void calculate() throws Exception
    {
        AppMedicationWithdrawalCalculateVo vo = new AppMedicationWithdrawalCalculateVo();
        vo.setDrugName("青霉素");
        vo.setStopDate("2025-06-01");
        vo.setWithdrawalDays(7);
        vo.setEndDate("2025-06-08");
        when(appMedicationWithdrawalService.calculate(eq("青霉素"), eq("2025-06-01"))).thenReturn(vo);

        mockMvc.perform(get("/app/medication/withdrawal/calculate")
                .param("drugName", "青霉素")
                .param("stopDate", "2025-06-01"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.endDate").value("2025-06-08"));
    }
}
