import { Decimal } from '@prisma/client/runtime/library';
import { FundsPostingService } from './funds-posting.service';

describe('FundsPostingService', () => {
  const wallet = {
    deposit: jest.fn(),
    freezeForBet: jest.fn(),
    settleBet: jest.fn(),
    applyResettleDelta: jest.fn(),
  };
  let service: FundsPostingService;

  beforeEach(() => {
    jest.clearAllMocks();
    service = new FundsPostingService(wallet as never);
  });

  it('freezes bet funds with a stable business key', async () => {
    await service.freezeBet({
      userId: 1n,
      stake: new Decimal(50),
      betNo: 'BET-001',
      tx: {} as never,
    });

    expect(wallet.freezeForBet).toHaveBeenCalledWith(
      1n,
      expect.any(Decimal),
      'BET-001',
      expect.anything(),
      'bet:BET-001:freeze',
    );
  });

  it('settles bets with batch and bet identity in the business key', async () => {
    await service.settleBet({
      userId: 2n,
      stake: new Decimal(100),
      payout: new Decimal(180),
      betNo: 'BET-002',
      batchNo: 'SETTLE-001',
      result: 'WIN',
      tx: {} as never,
    });

    expect(wallet.settleBet).toHaveBeenCalledWith(
      2n,
      expect.any(Decimal),
      expect.any(Decimal),
      'BET-002',
      'WIN',
      expect.anything(),
      'settle:SETTLE-001:BET-002',
    );
  });

  it('passes caller-provided business keys for transfer postings', async () => {
    await service.deposit({
      userId: 3n,
      amount: 25,
      operatorId: 9n,
      referenceId: 'REQ-1',
      transactionType: 'ADMIN_DEPOSIT',
      businessKey: 'admin-deposit:9:REQ-1',
    });

    expect(wallet.deposit).toHaveBeenCalledWith(
      3n,
      25,
      9n,
      undefined,
      'REQ-1',
      'ADMIN_DEPOSIT',
      undefined,
      'admin-deposit:9:REQ-1',
    );
  });
});
