package com.ruoyi.web.modules.role.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.role.dto.MerchantRoleCreateDTO;
import com.ruoyi.web.modules.role.dto.MerchantRoleUpdateDTO;
import com.ruoyi.web.modules.role.service.IMerchantRoleService;
import com.ruoyi.web.modules.role.vo.MerchantRoleDetailVO;
import com.ruoyi.web.modules.role.vo.MerchantRoleListVO;

@ExtendWith(MockitoExtension.class)
class MerchantRoleControllerTest
{
    @Mock
    private IMerchantRoleService merchantRoleService;

    @Spy
    @InjectMocks
    private MerchantRoleController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

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

    @Test
    void list_returns200() throws Exception
    {
        MerchantRoleListVO vo = new MerchantRoleListVO();
        vo.setRoleId(401L);
        vo.setRoleName("店员");
        vo.setStatus("0");
        doNothing().when(merchantRoleService).fillListScope(any(), eq(10L));
        when(merchantRoleService.selectRoleList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/agri/seller/role/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].roleName").value("店员"));
    }

    @Test
    void getInfo_returns200() throws Exception
    {
        MerchantRoleDetailVO vo = new MerchantRoleDetailVO();
        vo.setRoleId(401L);
        vo.setRoleName("店员");
        vo.setMenuIds(new Long[] { 6001L });
        when(merchantRoleService.selectRoleDetail(401L, 10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/role/401"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.roleName").value("店员"));
    }

    @Test
    void add_returns200() throws Exception
    {
        MerchantRoleCreateDTO dto = new MerchantRoleCreateDTO();
        dto.setRoleName("店员");
        dto.setStatus("0");
        dto.setMenuIds(new Long[] { 6001L });
        when(merchantRoleService.createRole(any(MerchantRoleCreateDTO.class), eq(10L), eq("merchantAdmin")))
                .thenReturn(401L);

        mockMvc.perform(post("/agri/seller/role")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.roleId").value(401));
    }

    @Test
    void edit_returns200() throws Exception
    {
        MerchantRoleUpdateDTO dto = new MerchantRoleUpdateDTO();
        dto.setRoleId(401L);
        dto.setRoleName("店员");
        dto.setStatus("0");
        dto.setMenuIds(new Long[] { 6001L });
        doNothing().when(merchantRoleService).updateRole(any(MerchantRoleUpdateDTO.class), eq(10L),
                eq("merchantAdmin"));

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

    @Test
    void remove_returns200() throws Exception
    {
        doNothing().when(merchantRoleService).deleteRoles(any(Long[].class), eq(10L));

        mockMvc.perform(delete("/agri/seller/role/401,402"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
