/** Task status values. */
export type TaskStatus = 'pending' | 'running' | 'gated' | 'completed' | 'failed' | 'escalated';

/** Task type — determines dispatch strategy and gate requirements. */
export type TaskType =
  | 'coding'
  | 'deploy'
  | 'research'
  | 'review'
  | 'documentation'
  | 'infrastructure';

/** Execution backend. */
export type DispatchMode = 'yolo' | 'acp' | 'exec';

/** Dependency evaluation policy. */
export type DependsOnPolicy = 'all' | 'any' | 'all_terminal';

/** Quality gate type. */
export type GateType = 'mechanical' | 'ai-review' | 'ci-pipeline';

/** Gate fail_on mode. */
export type GateFailOn = 'blocker' | 'any';

/** Quality gate definition — either a bare command string or a structured object. */
export interface GateEntry {
  command: string;
  type?: GateType;
  fail_on?: GateFailOn;
  [key: string]: unknown;
}

/** MACP task. */
export interface Task {
  id: string;
  title: string;
  status: TaskStatus;
  description?: string;
  type?: TaskType;
  dispatch?: DispatchMode;
  runtime?: string;
  worktree?: string;
  branch?: string;
  brief_path?: string;
  result_path?: string;
  issue?: string;
  pr?: string | null;
  depends_on?: string[];
  depends_on_policy?: DependsOnPolicy;
  max_attempts?: number;
  attempts?: number;
  timeout_seconds?: number;
  command?: string;
  quality_gates?: (string | GateEntry)[];
  metadata?: Record<string, unknown>;
  [key: string]: unknown;
}

/** Event types emitted by the MACP protocol. */
export type EventType =
  | 'task.assigned'
  | 'task.started'
  | 'task.completed'
  | 'task.failed'
  | 'task.escalated'
  | 'task.gated'
  | 'task.retry.scheduled'
  | 'rail.check.started'
  | 'rail.check.passed'
  | 'rail.check.failed';

/** Structured event record. */
export interface MACPEvent {
  event_id: string;
  event_type: EventType | string;
  task_id: string;
  status: string;
  timestamp: string;
  source: string;
  message: string;
  metadata: Record<string, unknown>;
}

/** Result from running a single quality gate. */
export interface GateResult {
  command: string;
  exit_code: number;
  type: string;
  output: string;
  timed_out: boolean;
  passed: boolean;
  fail_on?: string;
  blockers?: number;
  findings?: number;
  parsed_output?: unknown;
  parse_error?: string;
}

/** Result from a completed task. */
export interface TaskResult {
  task_id: string;
  status: TaskStatus;
  completed_at: string;
  exit_code: number;
  gate_results: GateResult[];
  files_changed?: string[];
  [key: string]: unknown;
}

/** Provider registry entry. */
export interface ProviderMeta {
  credential_file: string;
  env_var: string;
  oc_env_key: string;
  oc_provider_path: string;
}

/** Provider registry mapping. */
export type ProviderRegistry = Record<string, ProviderMeta>;

/** Raised when required provider credentials cannot be resolved. */
export class CredentialError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'CredentialError';
  }
}
