import { resolveCashbackRateForBet } from './cashback-rate.resolver';
import { Decimal } from '@prisma/client/runtime/library';

describe('resolveCashbackRateForBet', () => {
  const userId = BigInt(100);
  const agentId = BigInt(200);

  it('uses agent default when no rules', () => {
    const rate = resolveCashbackRateForBet({
      userId,
      agentId,
      marketTypes: ['FT_1X2'],
      agentDefaultRate: new Decimal('0.02'),
      rules: [],
    });
    expect(rate.toString()).toBe('0.02');
  });

  it('prefers USER rule over agent default', () => {
    const rate = resolveCashbackRateForBet({
      userId,
      agentId,
      marketTypes: ['FT_1X2'],
      agentDefaultRate: new Decimal('0.01'),
      rules: [
        {
          targetType: 'USER',
          targetId: userId,
          rate: new Decimal('0.03'),
          marketType: null,
        },
      ],
    });
    expect(rate.toString()).toBe('0.03');
  });

  it('applies market-specific rule when market matches', () => {
    const rate = resolveCashbackRateForBet({
      userId,
      agentId,
      marketTypes: ['FT_HANDICAP'],
      agentDefaultRate: new Decimal('0.01'),
      rules: [
        {
          targetType: 'GLOBAL',
          targetId: null,
          rate: new Decimal('0.005'),
          marketType: 'FT_HANDICAP',
        },
      ],
    });
    expect(rate.toString()).toBe('0.005');
  });

  it('skips market-specific rule when market does not match', () => {
    const rate = resolveCashbackRateForBet({
      userId,
      agentId,
      marketTypes: ['FT_1X2'],
      agentDefaultRate: new Decimal('0.01'),
      rules: [
        {
          targetType: 'GLOBAL',
          targetId: null,
          rate: new Decimal('0.005'),
          marketType: 'FT_HANDICAP',
        },
      ],
    });
    expect(rate.toString()).toBe('0.01');
  });
});
