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

import static org.mockito.ArgumentMatchers.any;
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.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 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 java.util.Collections;
import com.ruoyi.web.modules.account.dto.MemberCreateDTO;
import com.ruoyi.web.modules.account.dto.MemberStatusDTO;
import com.ruoyi.web.modules.account.service.IMemberService;
import com.ruoyi.web.modules.account.vo.MemberDetailVO;
import com.ruoyi.web.modules.account.vo.MemberLevelVO;

@ExtendWith(MockitoExtension.class)
class MemberControllerTest
{
    @Mock
    private IMemberService memberService;

    @Spy
    @InjectMocks
    private MemberController 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();
    }

    @Test
    void levels_returns200() throws Exception
    {
        MemberLevelVO level = new MemberLevelVO();
        level.setLevelNo(0);
        level.setLevelName("普通会员");
        when(memberService.selectMemberLevelList()).thenReturn(Collections.singletonList(level));

        mockMvc.perform(get("/agri/member/levels"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data[0].levelName").value("普通会员"));
    }

    @Test
    void getInfo_returns200() throws Exception
    {
        MemberDetailVO vo = new MemberDetailVO();
        vo.setMemberId(1L);
        vo.setNickName("用户A");
        vo.setMobile("17712349136");
        when(memberService.selectMemberDetail(1L)).thenReturn(vo);

        mockMvc.perform(get("/agri/member/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.nickName").value("用户A"));
    }

    @Test
    void add_returns200() throws Exception
    {
        MemberCreateDTO dto = new MemberCreateDTO();
        dto.setMobile("17700001111");
        dto.setPassword("Pass1234");
        when(memberService.createMember(any(MemberCreateDTO.class), eq("admin"))).thenReturn(100L);

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

    @Test
    void updateStatus_returns200() throws Exception
    {
        MemberStatusDTO dto = new MemberStatusDTO();
        dto.setStatus("1");

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