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

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
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.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Collections;
import org.junit.jupiter.api.AfterEach;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.util.NestedServletException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.stock.service.IStockQueryService;
import com.ruoyi.web.modules.stock.vo.StockQueryListRowVO;
import com.ruoyi.web.modules.stock.vo.StockQueryLogRowVO;

@ExtendWith(MockitoExtension.class)
class SellerStockQueryControllerTest
{
    @Mock
    private IStockQueryService stockQueryService;

    @Spy
    @InjectMocks
    private SellerStockQueryController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        SellerShopContext.setShopId(101L);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        lenient().doReturn("seller01").when(controller).getUsername();
    }

    @AfterEach
    void tearDown()
    {
        SellerShopContext.clear();
    }

    @Test
    void list_withoutShopId_rejected()
    {
        SellerShopContext.clear();

        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(get("/agri/seller/stock/query/list")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(stockQueryService, never()).selectList(any(), any());
    }

    @Test
    void list_returns200() throws Exception
    {
        StockQueryListRowVO row = new StockQueryListRowVO();
        row.setGoodsId(1001L);
        row.setGoodsName("测试商品");
        row.setStock(50);
        when(stockQueryService.selectList(eq(101L), any())).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/agri/seller/stock/query/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].stock").value(50));
    }

    @Test
    void logs_returns200() throws Exception
    {
        StockQueryLogRowVO row = new StockQueryLogRowVO();
        row.setIoTypeText("采购入库");
        row.setIoQty(10);
        when(stockQueryService.listLogs(101L, 1001L)).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/agri/seller/stock/query/1001/logs"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data[0].ioQty").value(10))
                .andExpect(jsonPath("$.data[0].ioTypeText").value("采购入库"));
    }
}
