import { ChargeCaptureService } from '@/modules/billing/charge-capture.service';
import { D } from '@/common/util';

/** Pure pricing rules of the charge engine (no database). */
describe('ChargeCaptureService pricing', () => {
  const svc = Object.create(ChargeCaptureService.prototype) as ChargeCaptureService;
  const tariff = (over: Record<string, unknown>) => ({ rate: D('1'), tiers: null, conditions: {}, ...over }) as never;

  it('flat rate when no tiers', () => {
    expect(svc.rateFor(tariff({ rate: D('2.5') }), D(10)).toString()).toBe('2.5');
  });

  it('tiered rate picks the band covering the quantity (upTo inclusive, null = open ended)', () => {
    const t = tariff({ tiers: [{ upTo: 100, rate: 0.25 }, { upTo: 500, rate: 0.2 }, { upTo: null, rate: 0.15 }] });
    expect(svc.rateFor(t, D(100)).toString()).toBe('0.25');
    expect(svc.rateFor(t, D(101)).toString()).toBe('0.2');
    expect(svc.rateFor(t, D(5000)).toString()).toBe('0.15');
  });

  it('tier order in the array does not matter', () => {
    const t = tariff({ tiers: [{ upTo: null, rate: 0.15 }, { upTo: 100, rate: 0.25 }] });
    expect(svc.rateFor(t, D(50)).toString()).toBe('0.25');
  });

  it('conditions must all match the facts', () => {
    expect(svc.conditionsMatch({ containerSize: '40' }, { containerSize: '40' })).toBe(true);
    expect(svc.conditionsMatch({ containerSize: '40' }, { containerSize: '20' })).toBe(false);
    expect(svc.conditionsMatch({}, {})).toBe(true);
    expect(svc.conditionsMatch({ asnType: 'RETURN', warehouseId: 'w1' }, { asnType: 'RETURN', warehouseId: 'w2' })).toBe(false);
  });
});
