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.IShopGoodsCategoryService;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.category.vo.CategoryRowVO;

@ExtendWith(MockitoExtension.class)
class SellerShopGoodsCategoryControllerTest
{
    @Mock
    private IShopGoodsCategoryService shopGoodsCategoryService;

    @Spy
    @InjectMocks
    private SellerShopGoodsCategoryController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

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

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

    @Test
    void list_returns200() throws Exception
    {
        CategoryRowVO row = new CategoryRowVO();
        row.setCategoryId(1L);
        row.setCategoryName("肥料");
        row.setCategoryLevel("1");
        when(shopGoodsCategoryService.selectCategoryRowList(null)).thenReturn(Collections.singletonList(row));

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

    @Test
    void add_returns200() throws Exception
    {
        when(shopGoodsCategoryService.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/shopCategory")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

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

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

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

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