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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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 com.ruoyi.web.modules.category.constant.CategoryConstants;
import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
import com.ruoyi.web.modules.category.facade.impl.CategoryFacadeImpl;
import com.ruoyi.web.modules.category.mapper.BizGoodsCategoryMapper;
import com.ruoyi.web.modules.category.service.IPlatformCategoryService;
import com.ruoyi.web.modules.category.vo.CategoryLevel2OptionVO;

/**
 * 分类 Facade 单元测试
 */
@ExtendWith(MockitoExtension.class)
class CategoryFacadeImplTest
{
    @Mock
    private BizGoodsCategoryMapper categoryMapper;

    @Mock
    private IPlatformCategoryService platformCategoryService;

    @InjectMocks
    private CategoryFacadeImpl categoryFacade;

    @Test
    void isCategoryVisible_whenParentHidden_false()
    {
        BizGoodsCategory l2 = level2(10L, 1L);
        BizGoodsCategory l1 = level1(1L);
        l1.setShowFlag(CategoryConstants.SHOW_NO);
        when(categoryMapper.selectByIdIgnoreShop(10L)).thenReturn(l2);
        when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(l1);

        assertFalse(categoryFacade.isCategoryVisible(10L));
    }

    @Test
    void isCategoryVisible_whenBothVisible_true()
    {
        BizGoodsCategory l2 = level2(10L, 1L);
        BizGoodsCategory l1 = level1(1L);
        when(categoryMapper.selectByIdIgnoreShop(10L)).thenReturn(l2);
        when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(l1);

        assertTrue(categoryFacade.isCategoryVisible(10L));
    }

    @Test
    void getCategoryPath_level2()
    {
        BizGoodsCategory l2 = level2(10L, 1L);
        l2.setCategoryName("复合肥");
        BizGoodsCategory l1 = level1(1L);
        l1.setCategoryName("肥料");
        when(categoryMapper.selectByIdIgnoreShop(10L)).thenReturn(l2);
        when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(l1);

        assertEquals("肥料 > 复合肥", categoryFacade.getCategoryPath(10L));
    }

    @Test
    void listPlatformLevel2Options_delegatesToPlatformService()
    {
        List<CategoryLevel2OptionVO> options = Collections.singletonList(
                new CategoryLevel2OptionVO(1002L, "肥料 > 复合肥", "复合肥"));
        when(platformCategoryService.selectPlatformLevel2Options()).thenReturn(options);

        List<CategoryLevel2OptionVO> result = categoryFacade.listPlatformLevel2Options();
        assertEquals(1, result.size());
        assertEquals("肥料 > 复合肥", result.get(0).getLabel());
        verify(platformCategoryService).selectPlatformLevel2Options();
    }

    @Test
    void listLevel2ByShopId_queriesCurrentShop()
    {
        BizGoodsCategory l1 = level1(1L);
        l1.setShopId(101L);
        l1.setCategoryName("肥料");
        BizGoodsCategory l2 = level2(10L, 1L);
        l2.setShopId(101L);
        l2.setCategoryName("复合肥");
        when(categoryMapper.selectList(argThat(q -> Long.valueOf(101L).equals(q.getShopId()))))
                .thenReturn(Arrays.asList(l1, l2));

        assertEquals(1, categoryFacade.listLevel2ByShopId(101L).size());
        assertEquals("肥料 > 复合肥", categoryFacade.listLevel2ByShopId(101L).get(0).getLabel());
    }

    private BizGoodsCategory level1(Long id)
    {
        BizGoodsCategory c = new BizGoodsCategory();
        c.setCategoryId(id);
        c.setCategoryLevel(CategoryConstants.LEVEL_ONE);
        c.setShowFlag(CategoryConstants.SHOW_YES);
        c.setCategoryName("一级");
        return c;
    }

    private BizGoodsCategory level2(Long id, Long parentId)
    {
        BizGoodsCategory c = new BizGoodsCategory();
        c.setCategoryId(id);
        c.setParentId(parentId);
        c.setCategoryLevel(CategoryConstants.LEVEL_TWO);
        c.setShowFlag(CategoryConstants.SHOW_YES);
        c.setCategoryName("二级");
        return c;
    }
}
