/**
 * Typed fail-closed capability errors (SDLC-D-035).
 *
 * A Forge run must fail closed when a required capability (executor, reviewer
 * provider, CI pipeline, authority sign-off) is missing. These typed errors
 * name the missing capability so callers can distinguish "not wired" from
 * ordinary execution failures.
 */

/** Closed set of typed Forge capability error codes. */
export const FORGE_ERROR_CODES = [
  'FORGE_NO_EXECUTOR',
  'FORGE_NO_REVIEWER',
  'FORGE_NO_CI_PIPELINE',
  'FORGE_NO_PROVIDER',
  'FORGE_AUTHORITY_REQUIRED',
] as const;

export type ForgeErrorCode = (typeof FORGE_ERROR_CODES)[number];

/** Raised when a required capability is missing and the pipeline must fail closed. */
export class ForgeCapabilityError extends Error {
  /** Typed error code from the closed FORGE_ERROR_CODES set. */
  readonly code: ForgeErrorCode;
  /** The missing capability, e.g. `task-executor`, `reviewer`, `board-approval`. */
  readonly capability: string;

  constructor(code: ForgeErrorCode, capability: string, message: string) {
    super(message);
    this.name = 'ForgeCapabilityError';
    this.code = code;
    this.capability = capability;
  }
}

/** Map a provider gate capability to its typed error code. */
export function providerErrorCode(capability: string): ForgeErrorCode {
  switch (capability) {
    case 'reviewer':
      return 'FORGE_NO_REVIEWER';
    case 'ci-pipeline':
      return 'FORGE_NO_CI_PIPELINE';
    default:
      return 'FORGE_NO_PROVIDER';
  }
}
