import { BettingLimitsService } from './betting-limits.service';
import { Decimal } from '@prisma/client/runtime/library';
import { expectAppError } from '../../testing/prisma-mock';

describe('BettingLimitsService', () => {
  const prisma = {
    systemConfig: {
      findUnique: jest.fn(),
    },
    bet: {
      aggregate: jest.fn(),
    },
  };

  const service = new BettingLimitsService(prisma as never);

  beforeEach(() => {
    jest.clearAllMocks();
    prisma.systemConfig.findUnique.mockResolvedValue(null);
    prisma.bet.aggregate.mockResolvedValue({ _sum: { stake: new Decimal(0) } });
  });

  it('rejects stake below minimum', async () => {
    await expect(
      service.validateBet({
        userId: BigInt(1),
        betType: 'SINGLE',
        stake: 0.5,
        potentialReturn: new Decimal(1),
      }),
    ).rejects.toMatchObject(expectAppError('MIN_STAKE'));
  });

  it('rejects stake above single max', async () => {
    await expect(
      service.validateBet({
        userId: BigInt(1),
        betType: 'SINGLE',
        stake: 60000,
        potentialReturn: new Decimal(70000),
      }),
    ).rejects.toMatchObject(expectAppError('MAX_STAKE'));
  });

  it('rejects potential return above payout cap', async () => {
    await expect(
      service.validateBet({
        userId: BigInt(1),
        betType: 'SINGLE',
        stake: 100,
        potentialReturn: new Decimal(600000),
      }),
    ).rejects.toMatchObject(expectAppError('MAX_PAYOUT'));
  });
});
