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

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
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.math.BigDecimal;
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.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.goods.constant.GoodsConstants;
import com.ruoyi.web.modules.goods.vo.GoodsPurchaseVO;
import com.ruoyi.web.modules.home.service.IGoodsAppService;
import com.ruoyi.web.modules.home.vo.GoodsDetailAppVO;
import com.ruoyi.web.modules.home.vo.GoodsReviewAppVO;
import com.ruoyi.web.modules.home.vo.GoodsShopBriefAppVO;

@ExtendWith(MockitoExtension.class)
class GoodsAppControllerTest
{
    @Mock
    private IGoodsAppService goodsAppService;

    @InjectMocks
    private GoodsAppController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    void detail_returns200WithPicsGallery() throws Exception
    {
        when(goodsAppService.getDetail(1L)).thenReturn(sampleDetail());

        mockMvc.perform(get("/api/goods/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.goodsId").value(1))
                .andExpect(jsonPath("$.data.goodsName").value("阿莫西林"))
                .andExpect(jsonPath("$.data.pics.length()").value(3))
                .andExpect(jsonPath("$.data.pics[0]").value("/main.jpg"))
                .andExpect(jsonPath("$.data.pics[1]").value("/sub1.jpg"))
                .andExpect(jsonPath("$.data.purchase.allowed").value(true))
                .andExpect(jsonPath("$.data.shop.shopId").value(101));
    }

    @Test
    void detail_notFoundRejected()
    {
        when(goodsAppService.getDetail(99L))
                .thenThrow(new ServiceException(GoodsConstants.MSG_GOODS_NOT_FOUND));

        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(get("/api/goods/99")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
    }

    @Test
    void canPurchase_returnsFacadeResult() throws Exception
    {
        GoodsPurchaseVO purchase = new GoodsPurchaseVO();
        purchase.setAllowed(false);
        purchase.setReason("商品非出售中");
        when(goodsAppService.canPurchase(1L)).thenReturn(purchase);

        mockMvc.perform(get("/api/goods/1/can-purchase"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.allowed").value(false))
                .andExpect(jsonPath("$.data.reason").value("商品非出售中"));
    }

    @Test
    void reviews_returnsTableRows() throws Exception
    {
        GoodsReviewAppVO row = new GoodsReviewAppVO();
        row.setReviewId(1L);
        row.setContent("很好");
        when(goodsAppService.listReviews(1L)).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/api/goods/1/reviews"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows.length()").value(1))
                .andExpect(jsonPath("$.rows[0].reviewId").value(1))
                .andExpect(jsonPath("$.rows[0].content").value("很好"));

        verify(goodsAppService).listReviews(1L);
    }

    private static GoodsDetailAppVO sampleDetail()
    {
        GoodsDetailAppVO vo = new GoodsDetailAppVO();
        vo.setGoodsId(1L);
        vo.setGoodsSn("G001");
        vo.setGoodsName("阿莫西林");
        vo.setPics(Arrays.asList("/main.jpg", "/sub1.jpg", "/sub2.jpg"));
        vo.setSalePrice(new BigDecimal("88.00"));
        vo.setStock(100);
        vo.setSalesCount(50);
        vo.setGoodsStatus("2");
        vo.setGoodsStatusLabel("出售中");
        GoodsPurchaseVO purchase = new GoodsPurchaseVO();
        purchase.setAllowed(true);
        vo.setPurchase(purchase);
        GoodsShopBriefAppVO shop = new GoodsShopBriefAppVO();
        shop.setShopId(101L);
        shop.setShopName("测试店铺");
        vo.setShop(shop);
        return vo;
    }
}
