package com.starry.admin.modules.blindbox.service;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.blindbox.mapper.BlindBoxPoolMapper;
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;

@ExtendWith(MockitoExtension.class)
class BlindBoxInventoryServiceTest {

    @Mock
    private BlindBoxPoolMapper blindBoxPoolMapper;

    @InjectMocks
    private BlindBoxInventoryService blindBoxInventoryService;

    @Test
    void shouldReserveStockWhenDraw() {
        when(blindBoxPoolMapper.consumeRewardStock(eq("tenant-1"), eq(5L), eq("gift-1"))).thenReturn(1);

        blindBoxInventoryService.reserveRewardStock("tenant-1", 5L, "gift-1");

        verify(blindBoxPoolMapper).consumeRewardStock("tenant-1", 5L, "gift-1");
    }

    @Test
    void shouldThrowWhenStockInsufficient() {
        when(blindBoxPoolMapper.consumeRewardStock(eq("tenant-1"), eq(6L), eq("gift-1"))).thenReturn(0);

        CustomException ex = assertThrows(CustomException.class,
                () -> blindBoxInventoryService.reserveRewardStock("tenant-1", 6L, "gift-1"));
        assertTrue(ex.getMessage().contains("库存不足"));
    }
}
