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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
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.facade.ICategoryFacade;
import com.ruoyi.web.modules.goods.constant.GoodsConstants;
import com.ruoyi.web.modules.goods.domain.BizGoods;
import com.ruoyi.web.modules.goods.facade.impl.GoodsPurchaseFacadeImpl;
import com.ruoyi.web.modules.goods.mapper.BizGoodsMapper;
import com.ruoyi.web.modules.goods.vo.GoodsPurchaseVO;
import com.ruoyi.web.modules.store.facade.IAgriShopFacade;

@ExtendWith(MockitoExtension.class)
class GoodsPurchaseFacadeImplTest
{
    @Mock
    private BizGoodsMapper goodsMapper;

    @Mock
    private IAgriShopFacade shopFacade;

    @Mock
    private ICategoryFacade categoryFacade;

    @InjectMocks
    private GoodsPurchaseFacadeImpl purchaseFacade;

    @Test
    void canPurchase_success()
    {
        BizGoods goods = new BizGoods();
        goods.setGoodsStatus(GoodsConstants.STATUS_ON_SALE);
        goods.setStock(10);
        goods.setShopId(1L);
        goods.setCategoryId(2L);
        when(goodsMapper.selectById(1L)).thenReturn(goods);
        when(shopFacade.isShopOpenForOrder(1L)).thenReturn(true);
        when(categoryFacade.isCategoryVisible(2L)).thenReturn(true);

        GoodsPurchaseVO vo = purchaseFacade.canPurchase(1L);

        assertTrue(vo.isAllowed());
    }

    @Test
    void canPurchase_noStock_fails()
    {
        BizGoods goods = new BizGoods();
        goods.setGoodsStatus(GoodsConstants.STATUS_ON_SALE);
        goods.setStock(0);
        when(goodsMapper.selectById(1L)).thenReturn(goods);

        GoodsPurchaseVO vo = purchaseFacade.canPurchase(1L);

        assertFalse(vo.isAllowed());
    }

    @Test
    void canPurchase_shopClosed_fails()
    {
        BizGoods goods = new BizGoods();
        goods.setGoodsStatus(GoodsConstants.STATUS_ON_SALE);
        goods.setStock(5);
        goods.setShopId(1L);
        goods.setCategoryId(2L);
        when(goodsMapper.selectById(1L)).thenReturn(goods);
        when(shopFacade.isShopOpenForOrder(1L)).thenReturn(false);

        GoodsPurchaseVO vo = purchaseFacade.canPurchase(1L);

        assertFalse(vo.isAllowed());
    }
}
