package com.ruoyi.web.modules.goodsservice.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.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
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.goodsservice.constant.GoodsServiceConstants;
import com.ruoyi.web.modules.goodsservice.domain.BizGoodsService;
import com.ruoyi.web.modules.goodsservice.facade.impl.GoodsServiceFacadeImpl;
import com.ruoyi.web.modules.goodsservice.mapper.BizGoodsServiceMapper;

/**
 * 商品服务 Facade 单元测试（GSV-UT-010～012）
 */
@ExtendWith(MockitoExtension.class)
class GoodsServiceFacadeImplTest
{
    @Mock
    private BizGoodsServiceMapper goodsServiceMapper;

    @Mock
    private IGoodsSnapshotFacade goodsSnapshotFacade;

    @InjectMocks
    private GoodsServiceFacadeImpl goodsServiceFacade;

    @Test
    void listAllEnabled_returnsOnlyActive()
    {
        BizGoodsService a = row(1L, "A", "1", 0);
        BizGoodsService b = row(2L, "B", "0", 1);
        when(goodsServiceMapper.selectAllEnabled()).thenReturn(Arrays.asList(a, b));

        assertEquals(2, goodsServiceFacade.listAllEnabled().size());
    }

    @Test
    void listDefaultShow_filtersFlag()
    {
        BizGoodsService a = row(1L, "A", GoodsServiceConstants.DEFAULT_SHOW_YES, 0);
        when(goodsServiceMapper.selectDefaultShowEnabled()).thenReturn(Collections.singletonList(a));

        assertEquals(1, goodsServiceFacade.listDefaultShow().size());
    }

    @Test
    void isServiceEnabled_deleted_returnsFalse()
    {
        when(goodsServiceMapper.selectById(99L)).thenReturn(null);

        assertFalse(goodsServiceFacade.isServiceEnabled(99L));
    }

    @Test
    void isServiceEnabled_active_returnsTrue()
    {
        when(goodsServiceMapper.selectById(1L)).thenReturn(row(1L, "A", "1", 0));

        assertTrue(goodsServiceFacade.isServiceEnabled(1L));
    }

    @Test
    void existsGoodsRef_delegatesToSnapshotFacade()
    {
        when(goodsSnapshotFacade.existsGoodsRef(1L)).thenReturn(true);

        assertTrue(goodsServiceFacade.existsGoodsRef(1L));
    }

    private BizGoodsService row(Long id, String name, String defaultShow, int sort)
    {
        BizGoodsService row = new BizGoodsService();
        row.setServiceId(id);
        row.setServiceName(name);
        row.setServiceIntro("intro");
        row.setServiceIcon("/icon.png");
        row.setDefaultShowFlag(defaultShow);
        row.setSortNo(sort);
        return row;
    }
}
