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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.request.MockMvcRequestBuilders.put;
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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.modules.account.dto.PlatformAdminCreateDTO;
import com.ruoyi.web.modules.account.dto.PlatformAdminUpdateDTO;
import com.ruoyi.web.modules.account.service.IPlatformAdminService;
import com.ruoyi.web.modules.account.vo.PlatformAdminDetailVO;
import com.ruoyi.web.modules.account.vo.PlatformAdminListVO;
import com.ruoyi.web.modules.account.vo.PlatformAdminRoleOptionVO;

@ExtendWith(MockitoExtension.class)
class PlatformAdminControllerTest
{
    @Mock
    private IPlatformAdminService platformAdminService;

    @Spy
    @InjectMocks
    private PlatformAdminController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp() throws Exception
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        org.mockito.Mockito.lenient().doReturn("admin").when(controller).getUsername();
        org.mockito.Mockito.lenient().doReturn(1L).when(controller).getUserId();
    }

    @Test
    void list_returns200() throws Exception
    {
        PlatformAdminListVO vo = new PlatformAdminListVO();
        vo.setUserId(10L);
        vo.setAdminName("运营甲");
        when(platformAdminService.selectAdminList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/agri/platformAdmin/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].adminName").value("运营甲"));
    }

    @Test
    void roleOptions_returns200() throws Exception
    {
        PlatformAdminRoleOptionVO role = new PlatformAdminRoleOptionVO();
        role.setRoleId(2L);
        role.setRoleName("商户经营");
        role.setRoleKey("merchant");
        when(platformAdminService.selectRoleOptions()).thenReturn(Collections.singletonList(role));

        mockMvc.perform(get("/agri/platformAdmin/roleOptions"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0].roleKey").value("merchant"));
    }

    @Test
    void getInfo_returns200() throws Exception
    {
        PlatformAdminDetailVO vo = new PlatformAdminDetailVO();
        vo.setUserId(10L);
        vo.setAdminName("运营甲");
        when(platformAdminService.selectAdminDetail(10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/platformAdmin/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.adminName").value("运营甲"));
    }

    @Test
    void add_returns200() throws Exception
    {
        PlatformAdminCreateDTO dto = new PlatformAdminCreateDTO();
        dto.setAdminName("运营甲");
        dto.setUserName("pad01");
        dto.setPhonenumber("17700001111");
        dto.setPassword("Admin@123");
        dto.setRoleIds(new Long[] { 2L });
        when(platformAdminService.createAdmin(any(PlatformAdminCreateDTO.class), eq("admin"))).thenReturn(100L);

        mockMvc.perform(post("/agri/platformAdmin")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data").value(100));
    }

    @Test
    void edit_returns200() throws Exception
    {
        PlatformAdminUpdateDTO dto = new PlatformAdminUpdateDTO();
        dto.setUserId(10L);
        dto.setAdminName("新名称");
        dto.setPhonenumber("17700003333");
        dto.setRoleIds(new Long[] { 2L });
        doNothing().when(platformAdminService).updateAdmin(any(PlatformAdminUpdateDTO.class), eq(1L), eq("admin"));

        mockMvc.perform(put("/agri/platformAdmin")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    void remove_returns200() throws Exception
    {
        doNothing().when(platformAdminService).deleteAdmin(any(Long[].class), eq(1L));

        mockMvc.perform(delete("/agri/platformAdmin/10,11"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
