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.StockInboundCreateDTO;
import com.ruoyi.web.modules.stock.dto.StockInboundCreateItemDTO;
import com.ruoyi.web.modules.stock.service.IStockInboundService;
import com.ruoyi.web.modules.stock.vo.StockInboundDetailVO;
import com.ruoyi.web.modules.stock.vo.StockInboundListRowVO;

@ExtendWith(MockitoExtension.class)
class SellerStockInboundControllerTest
{
    @Mock
    private IStockInboundService stockInboundService;

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

    @Test
    void list_returns200() throws Exception
    {
        StockInboundListRowVO row = new StockInboundListRowVO();
        row.setInboundId(1L);
        row.setInboundNo("IN20260526000001");
        row.setInboundTypeText("采购入库");
        when(stockInboundService.selectList(eq(101L), any())).thenReturn(Collections.singletonList(row));

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

    @Test
    void detail_returns200() throws Exception
    {
        StockInboundDetailVO vo = new StockInboundDetailVO();
        vo.setInboundId(10L);
        vo.setInboundNo("IN20260526000001");
        when(stockInboundService.getDetail(101L, 10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/stock/inbound/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.inboundNo").value("IN20260526000001"));
    }

    @Test
    void create_delegatesToService() throws Exception
    {
        StockInboundCreateDTO dto = new StockInboundCreateDTO();
        dto.setInboundType(StockConstants.INBOUND_TYPE_PURCHASE);
        StockInboundCreateItemDTO item = new StockInboundCreateItemDTO();
        item.setGoodsId(1001L);
        item.setQuantity(5);
        dto.setItems(Collections.singletonList(item));

        Map<String, Object> result = new HashMap<>();
        result.put("inboundId", 20L);
        result.put("inboundNo", "IN20260526000002");
        when(stockInboundService.createManual(eq(101L), any(), any(), eq("seller01"))).thenReturn(result);

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