import type { ExternalTransferRequest } from '@dbis/settlement-core';
import { loadConfig, loadOfficeProfile, settlementOfficeId } from '../config';

/** Bind every settlement request to Office 24 (same profile as OMNL terminal). */
export function bindOffice24(req: Partial<ExternalTransferRequest>): ExternalTransferRequest {
  const cfg = loadConfig();
  const profile = loadOfficeProfile();
  const officeId = settlementOfficeId(req.officeId);

  if (!req.idempotencyKey || !req.creditorIban || !req.amount) {
    throw new Error('idempotencyKey, creditorIban, amount required');
  }

  return {
    officeId,
    valueDate: req.valueDate ?? new Date().toISOString().slice(0, 10),
    currency: req.currency ?? profile.settlement.defaultCurrency ?? cfg.defaultCurrency,
    moneyLayers: (req.moneyLayers ?? profile.settlement.moneyLayers) as ExternalTransferRequest['moneyLayers'],
    rail: req.rail ?? 'SWIFT',
    remittanceInfo: req.remittanceInfo ?? `OMNL Office 24 settlement — ${profile.externalId}`,
    beneficiaryName: req.beneficiaryName ?? 'Beneficiary',
    idempotencyKey: req.idempotencyKey,
    creditorIban: req.creditorIban,
    amount: req.amount,
    debtorIban: req.debtorIban,
    creditorBic: req.creditorBic,
    orderingName: req.orderingName ?? profile.omnlLegalName,
    swiftMessageKind: req.swiftMessageKind,
    tradeFinance: req.tradeFinance,
    rwaVerbiage: req.rwaVerbiage,
    convertToCrypto: req.convertToCrypto,
  };
}
