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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
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.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays;
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.goods.constant.GoodsConstants;
import com.ruoyi.web.modules.goods.dto.GoodsAuditDTO;
import com.ruoyi.web.modules.goods.dto.GoodsOffShelfDTO;
import com.ruoyi.web.modules.goods.exception.GoodsBatchOperationException;
import com.ruoyi.web.modules.goods.service.IGoodsService;
import com.ruoyi.web.modules.goods.vo.GoodsDetailVO;

@ExtendWith(MockitoExtension.class)
class GoodsControllerTest
{
    @Mock
    private IGoodsService goodsService;

    @Mock
    private com.ruoyi.web.modules.category.facade.ICategoryFacade categoryFacade;

    @Spy
    @InjectMocks
    private GoodsController 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
    {
        GoodsDetailVO vo = new GoodsDetailVO();
        vo.setGoodsId(1L);
        vo.setGoodsName("复合肥");
        when(goodsService.selectPlatformDetail(1L)).thenReturn(vo);

        mockMvc.perform(get("/agri/goods/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.goodsName").value("复合肥"));
    }

    @Test
    void pendingCount_returns200() throws Exception
    {
        when(goodsService.countPlatformPending()).thenReturn(3);

        mockMvc.perform(get("/agri/goods/pendingCount"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.pendingCount").value(3));
    }

    @Test
    void audit_returns200() throws Exception
    {
        GoodsAuditDTO dto = new GoodsAuditDTO();
        dto.setGoodsIds(Collections.singletonList(1L));
        dto.setPass(true);

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

        verify(goodsService).auditGoods(any(GoodsAuditDTO.class), eq("admin"));
    }

    @Test
    void audit_batchFail_returnsReasons() throws Exception
    {
        GoodsAuditDTO dto = new GoodsAuditDTO();
        dto.setGoodsIds(Arrays.asList(1L, 2L));
        dto.setPass(true);
        doThrow(new GoodsBatchOperationException(GoodsConstants.MSG_BATCH_FAIL,
                Collections.singletonList("商品非待审核状态：G001")))
                .when(goodsService).auditGoods(any(), eq("admin"));

        mockMvc.perform(put("/agri/goods/audit")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.data.reasons[0]").exists());
    }

    @Test
    void offShelf_returns200() throws Exception
    {
        GoodsOffShelfDTO dto = new GoodsOffShelfDTO();
        dto.setGoodsIds(Collections.singletonList(5L));

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

        verify(goodsService).offShelfPlatform(any(GoodsOffShelfDTO.class), eq("admin"));
    }
}
