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.request.MockMvcRequestBuilders.post;
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 java.util.HashMap;
import java.util.Map;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.util.NestedServletException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.dto.StockOutboundCreateDTO;
import com.ruoyi.web.modules.stock.dto.StockOutboundCreateItemDTO;
import com.ruoyi.web.modules.stock.service.IStockOutboundService;
import com.ruoyi.web.modules.stock.vo.StockOutboundDetailVO;
import com.ruoyi.web.modules.stock.vo.StockOutboundListRowVO;

@ExtendWith(MockitoExtension.class)
class SellerStockOutboundControllerTest
{
    @Mock
    private IStockOutboundService stockOutboundService;

    @Spy
    @InjectMocks
    private SellerStockOutboundController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

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

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

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

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

    @Test
    void list_returns200() throws Exception
    {
        StockOutboundListRowVO row = new StockOutboundListRowVO();
        row.setOutboundId(1L);
        row.setOutboundNo("OUT20260526000001");
        row.setOutboundTypeText("订单出库");
        when(stockOutboundService.selectList(eq(101L), any())).thenReturn(Collections.singletonList(row));

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

    @Test
    void detail_returns200() throws Exception
    {
        StockOutboundDetailVO vo = new StockOutboundDetailVO();
        vo.setOutboundId(10L);
        vo.setOutboundNo("OUT20260526000001");
        when(stockOutboundService.getDetail(101L, 10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/stock/outbound/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.outboundNo").value("OUT20260526000001"));
    }

    @Test
    void create_delegatesToService() throws Exception
    {
        StockOutboundCreateDTO dto = new StockOutboundCreateDTO();
        dto.setOutboundType(StockConstants.OUTBOUND_TYPE_MANUAL_ORDER);
        StockOutboundCreateItemDTO item = new StockOutboundCreateItemDTO();
        item.setGoodsId(1001L);
        item.setQuantity(5);
        dto.setItems(Collections.singletonList(item));

        Map<String, Object> result = new HashMap<>();
        result.put("outboundId", 20L);
        result.put("outboundNo", "OUT20260526000002");
        when(stockOutboundService.createManual(eq(101L), any(), eq(1L), eq("seller01"))).thenReturn(result);

        mockMvc.perform(post("/agri/seller/stock/outbound")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.outboundNo").value("OUT20260526000002"));
    }
}
