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.MerchantEmployeeCreateDTO;
import com.ruoyi.web.modules.account.dto.MerchantEmployeePasswordDTO;
import com.ruoyi.web.modules.account.dto.MerchantEmployeeProfileDTO;
import com.ruoyi.web.modules.account.dto.MerchantEmployeeUpdateDTO;
import com.ruoyi.web.modules.account.service.IMerchantEmployeeService;
import com.ruoyi.web.modules.account.vo.MerchantEmployeeDetailVO;
import com.ruoyi.web.modules.account.vo.MerchantEmployeeListVO;
import com.ruoyi.web.modules.account.vo.MerchantEmployeeQuotaVO;

@ExtendWith(MockitoExtension.class)
class MerchantEmployeeControllerTest
{
    @Mock
    private IMerchantEmployeeService merchantEmployeeService;

    @Spy
    @InjectMocks
    private MerchantEmployeeController 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("merchantAdmin").when(controller).getUsername();
        org.mockito.Mockito.lenient().doReturn(10L).when(controller).getUserId();
    }

    @Test
    void list_returns200() throws Exception
    {
        MerchantEmployeeListVO vo = new MerchantEmployeeListVO();
        vo.setUserId(20L);
        vo.setEmployeeName("王小二");
        vo.setLoginAccount("13900002001");
        vo.setAccountType("EMPLOYEE");
        doNothing().when(merchantEmployeeService).fillListScope(any(), eq(10L));
        when(merchantEmployeeService.selectEmployeeList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/agri/seller/employee/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].employeeName").value("王小二"));
    }

    @Test
    void quota_returns200() throws Exception
    {
        MerchantEmployeeQuotaVO vo = new MerchantEmployeeQuotaVO();
        vo.setUsedCount(2);
        vo.setMaxCount(5);
        vo.setMerchantName("测试商户");
        when(merchantEmployeeService.selectQuota(10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/employee/quota"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.usedCount").value(2))
                .andExpect(jsonPath("$.data.maxCount").value(5));
    }

    @Test
    void getInfo_returns200() throws Exception
    {
        MerchantEmployeeDetailVO vo = new MerchantEmployeeDetailVO();
        vo.setUserId(20L);
        vo.setMobile("13900002001");
        when(merchantEmployeeService.selectEmployeeDetail(20L, 10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/employee/20"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.mobile").value("13900002001"));
    }

    @Test
    void add_returns200() throws Exception
    {
        MerchantEmployeeCreateDTO dto = new MerchantEmployeeCreateDTO();
        dto.setEmployeeName("王小二");
        dto.setMobile("13900002001");
        dto.setRoleIds(new Long[] { 201L });
        dto.setStatus("0");
        when(merchantEmployeeService.createEmployee(any(MerchantEmployeeCreateDTO.class), eq(10L), eq("merchantAdmin")))
                .thenReturn(20L);

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

    @Test
    void edit_returns200() throws Exception
    {
        MerchantEmployeeUpdateDTO dto = new MerchantEmployeeUpdateDTO();
        dto.setUserId(20L);
        dto.setEmployeeName("王小二");
        dto.setMobile("13900002001");
        dto.setStatus("0");
        dto.setRoleIds(new Long[] { 201L });
        doNothing().when(merchantEmployeeService).updateEmployee(any(MerchantEmployeeUpdateDTO.class), eq(10L),
                eq("merchantAdmin"));

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

    @Test
    void editProfile_returns200() throws Exception
    {
        MerchantEmployeeProfileDTO dto = new MerchantEmployeeProfileDTO();
        dto.setUserId(20L);
        dto.setEmployeeName("王小二");
        dto.setMobile("13900002001");
        doNothing().when(merchantEmployeeService).updateProfile(any(MerchantEmployeeProfileDTO.class), eq(10L),
                eq("merchantAdmin"));

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

    @Test
    void resetPwd_returns200() throws Exception
    {
        MerchantEmployeePasswordDTO dto = new MerchantEmployeePasswordDTO();
        dto.setUserId(20L);
        dto.setPassword("Employee@123");
        dto.setConfirmPassword("Employee@123");
        doNothing().when(merchantEmployeeService).updatePassword(any(MerchantEmployeePasswordDTO.class), eq(10L),
                eq("merchantAdmin"));

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

    @Test
    void remove_returns200() throws Exception
    {
        doNothing().when(merchantEmployeeService).deleteEmployees(any(Long[].class), eq(10L), eq("merchantAdmin"));

        mockMvc.perform(delete("/agri/seller/employee/20,21"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
