import {
  buildProofOfFunds,
  buildDebitNote,
  buildRemittanceAdvice,
  buildTokenizationSanitized,
  buildConversionSwiftCopy,
  renderTerminalScreen,
  type SettlementTerminalContext,
} from './omnl-settlement-terminal';

jest.mock('./omnl-alchemy-client', () => ({
  alchemyConfigured: jest.fn(() => false),
}));

jest.mock('./omnl-ipsas-gl', () => ({
  fetchFineractGlAccounts: jest.fn(),
}));

function sampleCtx(): SettlementTerminalContext {
  return {
    generatedAtUtc: '2026-06-07T12:00:00.000Z',
    settlementRef: 'HYBX-BATCH-001',
    valueDate: '2026-03-17',
    currency: 'USD',
    officeId: 1,
    officeName: 'OMNL Head Office (DBIS) – Central Bank',
    beneficiary: 'Bank Kanaya (Indonesia)',
    beneficiaryOfficeId: 22,
    beneficiaryExternalId: 'BANK-KANAYA-ID',
    amountUsd: '1000000000.00',
    omnlLegalName: 'ORGANISATION MONDIALE DU NUMERIQUE L.P.B.C.',
    omnlLei: '98450070C57395F6B906',
    fineract: {
      configured: true,
      pofPrimaryGlCode: '2100',
      pofPrimaryAmountUsd: '50000000000.00',
      glBalances: [{ glCode: '2100', name: 'M1', side: 'Cr', amount: '50000000000.00' }],
    },
    alchemy: {
      configured: false,
      mainnetNetwork: 'eth-mainnet',
      chain138Network: 'defi-oracle-meta-mainnet',
    },
    documentSha256: 'abc123',
  };
}

describe('omnl-settlement-terminal', () => {
  it('builds proof of funds with primary GL attestation', () => {
    const pof = buildProofOfFunds(sampleCtx());
    expect(pof.documentType).toBe('proof-of-funds');
    expect(pof.attestation).toMatchObject({ primaryGlCode: '2100' });
  });

  it('builds debit note with accounting pair', () => {
    const dn = buildDebitNote(sampleCtx());
    expect(dn.documentType).toBe('debit-note');
    expect(dn.accounting).toMatchObject({ debitGlCode: '2100', creditGlCode: '1410' });
  });

  it('builds remittance advice with instruction id', () => {
    const ra = buildRemittanceAdvice(sampleCtx());
    expect(ra.documentType).toBe('remittance-advice');
    expect(String(ra.instructionId)).toContain('RA-HYBX-BATCH-001');
  });

  it('sanitizes tokenization copy fields', () => {
    const tok = buildTokenizationSanitized(sampleCtx());
    expect(tok.documentType).toBe('tokenization-sanitized');
    expect(String(tok.accountNumber)).toMatch(/\*/);
    expect(String(tok.beneficiaryName)).toMatch(/\*/);
  });

  it('builds conversion swift copy with MT field tags', () => {
    const sw = buildConversionSwiftCopy(sampleCtx());
    expect(sw.documentType).toBe('conversion-swift-copy');
    expect((sw.fields as Record<string, string>)[':20:']).toBe('HYBX-BATCH-001');
  });

  it('renders black and color terminal screens', () => {
    const ctx = sampleCtx();
    const black = renderTerminalScreen(ctx, 'black', 'package');
    const color = renderTerminalScreen(ctx, 'color', 'proof-of-funds');
    expect(black).toContain('BLACK / GREEN');
    expect(color).toContain('COLOR / GRAY');
    expect(black).toContain('HYBX-BATCH-001');
    expect(color).toContain('POF GL 2100');
  });
});
