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.constant.StockConstants;
import com.ruoyi.web.modules.stock.service.IStockLogService;
import com.ruoyi.web.modules.stock.vo.StockLogListRowVO;

@ExtendWith(MockitoExtension.class)
class SellerStockLogControllerTest
{
    @Mock
    private IStockLogService stockLogService;

    @Spy
    @InjectMocks
    private SellerStockLogController 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/log/list")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(stockLogService, never()).selectList(any(), any());
    }

    @Test
    void list_returns200() throws Exception
    {
        StockLogListRowVO row = new StockLogListRowVO();
        row.setLogId(1L);
        row.setGoodsName("测试商品");
        row.setChangeReasonText("下单扣减库存");
        row.setDirection(StockConstants.DIRECTION_OUT);
        when(stockLogService.selectList(eq(101L), any())).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/agri/seller/stock/log/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].changeReasonText").value("下单扣减库存"));
    }

    @Test
    void list_withDirectionAndKeyword() throws Exception
    {
        when(stockLogService.selectList(eq(101L), any())).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/agri/seller/stock/log/list")
                .param("direction", StockConstants.DIRECTION_IN)
                .param("keyword", "复合肥"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));

        verify(stockLogService).selectList(eq(101L), any());
    }
}
