/**
 * `workflow approve` — Approve a paused workflow run.
 *
 * @example
 *   workflow approve abc123
 *   workflow approve abc123 "Looks good" --json
 */

import { printJson, type CliOptions } from "../utils.js";

// ---------------------------------------------------------------------------
// Stub: engine integration (not implemented yet)
// ---------------------------------------------------------------------------

interface ApproveResult {
  runId: string;
  approved: boolean;
  comment?: string;
  message: string;
}

async function approveWorkflowRun(
  _runId: string,
  _comment?: string,
): Promise<ApproveResult> {
  throw new Error('not implemented yet: approveWorkflowRun');
}

// ---------------------------------------------------------------------------
// Command handler
// ---------------------------------------------------------------------------

export async function approveCommand(
  args: string[],
  options: CliOptions,
): Promise<void> {
  if (args.length === 0) {
    throw new Error('Missing required argument: <run-id>\n\nUsage: workflow approve <run-id> [comment] [--json]');
  }

  const runId = args[0]!;
  const comment = args.length > 1 ? args.slice(1).join(' ') : undefined;

  const result = await approveWorkflowRun(runId, comment);

  if (options.json) {
    printJson(result);
    return;
  }

  if (result.approved) {
    console.log(`✓ Run ${result.runId} approved.`);
    if (result.comment) {
      console.log(`  Comment: ${result.comment}`);
    }
  } else {
    console.log(`✗ Failed to approve run ${result.runId}: ${result.message}`);
  }
}