// apps/web/src/server/payments/mock/scenarios.ts
import { PaymentErrorCode } from '../provider.js';

export const MOCK_SCENARIOS = [
  'success',
  'decline',
  'insufficient_funds',
  'token_expired',
  'token_invalid',
  'duplicate',
  'hold_reject_debit',
  'partial_refund_failure',
  'reconcile_flip',
] as const;

export type MockScenario = (typeof MOCK_SCENARIOS)[number];

export function isMockScenario(value: string | null | undefined): value is MockScenario {
  return value != null && (MOCK_SCENARIOS as readonly string[]).includes(value);
}

/** Map a non-success scenario to the neutral error code its failing ops emit. */
export function scenarioErrorCode(scenario: MockScenario): PaymentErrorCode | null {
  switch (scenario) {
    case 'decline':
      return PaymentErrorCode.CARD_DECLINED;
    case 'insufficient_funds':
      return PaymentErrorCode.INSUFFICIENT_FUNDS;
    case 'token_expired':
      return PaymentErrorCode.TOKEN_EXPIRED;
    case 'token_invalid':
      return PaymentErrorCode.TOKEN_INVALID;
    case 'duplicate':
      return PaymentErrorCode.DUPLICATE;
    case 'hold_reject_debit':
      return PaymentErrorCode.HOLD_NOT_SUPPORTED;
    default:
      return null; // success / partial_refund_failure / reconcile_flip have op-specific handling
  }
}
