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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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 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 com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
import com.ruoyi.web.modules.category.exception.CategoryBatchDeleteException;
import com.ruoyi.web.modules.category.service.ICategoryService;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.category.vo.CategoryListVO;
import com.ruoyi.web.modules.category.vo.CategoryRowVO;

/**
 * 商家端分类接口测试（CAT-API）
 */
@ExtendWith(MockitoExtension.class)
class SellerCategoryControllerTest
{
    @Mock
    private ICategoryService categoryService;

    @Spy
    @InjectMocks
    private SellerCategoryController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

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

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

    @Test
    void list_returns200() throws Exception
    {
        CategoryListVO vo = new CategoryListVO();
        vo.setShopId(1L);
        vo.setShopName("测试店");
        CategoryRowVO row = new CategoryRowVO();
        row.setCategoryId(1L);
        row.setCategoryName("肥料");
        vo.setRows(Collections.singletonList(row));
        when(categoryService.selectCategoryList(null)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/category/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.shopId").value(1))
                .andExpect(jsonPath("$.data.rows[0].categoryName").value("肥料"));
    }

    @Test
    void add_returns200() throws Exception
    {
        when(categoryService.insertCategory(any())).thenReturn(1);

        BizGoodsCategory body = new BizGoodsCategory();
        body.setCategoryLevel("1");
        body.setCategoryName("肥料");
        body.setCategoryPic("/profile/upload/c.jpg");
        body.setSortNo(0);
        body.setShowFlag("1");
        body.setHotFlag("0");

        mockMvc.perform(post("/agri/seller/category")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    void remove_batchFail_returnsReasons() throws Exception
    {
        doThrow(new CategoryBatchDeleteException("部分分类不满足删除条件，整批操作已取消",
                Collections.singletonList("分类「复合肥」下存在商品，请先处理商品")))
                .when(categoryService).deleteCategoryByIds(any(), any());

        mockMvc.perform(delete("/agri/seller/category/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.data.reasons[0]").exists());
    }

    @Test
    void level2Options_returns200() throws Exception
    {
        when(categoryService.selectLevel2Options()).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/agri/seller/category/level2Options"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
