import { evaluateHoLiquidityAlerts } from './omnl-ho-liquidity-snapshot';

jest.mock('./omnl-fineract-office-gl', () => ({
  fineractConfigured: jest.fn(() => true),
}));

describe('evaluateHoLiquidityAlerts', () => {
  const baseCash = {
    generatedAt: new Date().toISOString(),
    officeId: 1,
    cashInBankUsd: 1_000_000_000,
    externalNostroGlCode: '13010',
    nostroGlCodes: ['13010'],
    legs: [],
    fineractConfigured: true,
    audit: {
      status: 'breaks' as const,
      breaksCount: 0,
      supplyNostroMatch: 'break' as const,
      policyId: 'OMNL-NOSTRO-BACKED-NETTING-V1',
      policyRef: 'config/compliance/omnl-nostro-backed-netting.v1.json',
    },
  };

  const baseReserve = {
    status: 'sufficient' as const,
    multiplier: 1.2,
    headOfficeId: 1,
    reserveAssetGlCodes: ['1000', '1050'],
    reserveAssetsUsd: 1_150_000_000_000,
    issuedCStarUsd: 380_000_000_000,
    requiredReserveUsd: 456_000_000_000,
    coverageRatio: 2.52,
    formula: 'coverageRatio = reserveAssetsUsd / (1.2 × issuedCStarUsd)',
    policyId: 'OMNL-NOSTRO-BACKED-NETTING-V1',
    policyRef: 'config/compliance/omnl-nostro-backed-netting.v1.json',
    generatedAt: new Date().toISOString(),
  };

  it('flags nostro supply break as info while reserve sufficient', () => {
    const alerts = evaluateHoLiquidityAlerts({
      cashInBank: baseCash,
      reserveCoverage: baseReserve,
      generatedAt: new Date().toISOString(),
    });
    expect(alerts.some((a) => a.code === 'NOSTRO_SUPPLY_BREAK' && a.severity === 'info')).toBe(true);
    expect(alerts.some((a) => a.severity === 'critical')).toBe(false);
  });

  it('flags insufficient reserve coverage as critical', () => {
    const alerts = evaluateHoLiquidityAlerts({
      cashInBank: baseCash,
      reserveCoverage: { ...baseReserve, status: 'insufficient', coverageRatio: 0.8 },
      generatedAt: new Date().toISOString(),
    });
    expect(alerts.some((a) => a.code === 'RESERVE_COVERAGE_INSUFFICIENT')).toBe(true);
  });
});
