package com.ruoyi.web.modules.store.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 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.store.dto.ShopCreateDTO;
import com.ruoyi.web.modules.store.dto.ShopUpdateDTO;
import com.ruoyi.web.modules.store.service.IShopService;
import com.ruoyi.web.modules.store.vo.ShopDetailVO;

@ExtendWith(MockitoExtension.class)
class ShopControllerTest
{
    @Mock
    private IShopService shopService;

    @Spy
    @InjectMocks
    private ShopController 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 getInfo_returns200() throws Exception
    {
        ShopDetailVO vo = new ShopDetailVO();
        vo.setShopId(1L);
        vo.setShopName("测试店");
        when(shopService.selectShopDetail(1L)).thenReturn(vo);

        mockMvc.perform(get("/agri/shop/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.shopName").value("测试店"));
    }

    @Test
    void add_returnsShopId() throws Exception
    {
        when(shopService.createShop(any(ShopCreateDTO.class), eq("admin"))).thenReturn(5L);

        ShopCreateDTO dto = new ShopCreateDTO();
        dto.setShopName("新店");
        dto.setMerchantId(1L);

        mockMvc.perform(post("/agri/shop")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.shopId").value(5));
    }

    @Test
    void edit_needConfirm_returnsData() throws Exception
    {
        Map<String, Object> data = new HashMap<>();
        data.put("needConfirm", true);
        data.put("confirmMessage", "停业后用户端将无法下单，是否继续？");
        when(shopService.updateShop(any(ShopUpdateDTO.class), eq("admin"))).thenReturn(data);

        ShopUpdateDTO dto = new ShopUpdateDTO();
        dto.setShopId(1L);
        dto.setShopStatus("1");
        dto.setConfirm(false);

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