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.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 java.util.HashMap;
import java.util.Map;
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.AccountPasswordUpdateDTO;
import com.ruoyi.web.modules.account.dto.AccountProfileUpdateDTO;
import com.ruoyi.web.modules.account.service.IAccountManageService;
import com.ruoyi.web.modules.account.vo.AccountManageDetailVO;
import com.ruoyi.web.modules.account.vo.AccountManageListVO;

@ExtendWith(MockitoExtension.class)
class AccountManageControllerTest
{
    @Mock
    private IAccountManageService accountManageService;

    @Spy
    @InjectMocks
    private AccountManageController 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
    {
        AccountManageListVO vo = new AccountManageListVO();
        vo.setUserId(10L);
        vo.setAccountName("张老板");
        vo.setMobile("138****8000");
        vo.setShopCount(2);
        when(accountManageService.selectAccountList(any())).thenReturn(Collections.singletonList(vo));

        mockMvc.perform(get("/agri/accountManage/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].accountName").value("张老板"));
    }

    @Test
    void getInfo_returns200WithFullMobile() throws Exception
    {
        AccountManageDetailVO vo = new AccountManageDetailVO();
        vo.setUserId(10L);
        vo.setAccountName("张老板");
        vo.setMobile("13800138000");
        when(accountManageService.selectAccountDetail(10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/accountManage/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.mobile").value("13800138000"));
    }

    @Test
    void edit_needConfirm_returns200WithData() throws Exception
    {
        AccountProfileUpdateDTO dto = new AccountProfileUpdateDTO();
        dto.setUserId(10L);
        dto.setAccountName("张老板");
        dto.setMobile("13900139000");

        Map<String, Object> data = new HashMap<>();
        data.put("needConfirm", true);
        when(accountManageService.updateProfile(any(AccountProfileUpdateDTO.class), eq("admin"))).thenReturn(data);

        mockMvc.perform(put("/agri/accountManage")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.needConfirm").value(true));
    }

    @Test
    void resetPwd_returns200() throws Exception
    {
        AccountPasswordUpdateDTO dto = new AccountPasswordUpdateDTO();
        dto.setUserId(10L);
        dto.setPassword("Merchant@123");
        dto.setConfirmPassword("Merchant@123");
        doNothing().when(accountManageService).updatePassword(any(AccountPasswordUpdateDTO.class), eq("admin"));

        mockMvc.perform(put("/agri/accountManage/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(accountManageService).deleteAccounts(any(Long[].class), eq(1L), eq("admin"));

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