/**
 * `workflow abandon` — Cancel a non-terminal workflow run.
 *
 * Marks the run as cancelled. Only works on runs that are not
 * already in a terminal state (completed, failed, cancelled).
 *
 * @example
 *   workflow abandon abc123
 *   workflow abandon abc123 --json
 */

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

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

interface AbandonResult {
  runId: string;
  abandoned: boolean;
  message: string;
}

async function abandonWorkflowRun(_runId: string): Promise<AbandonResult> {
  throw new Error('not implemented yet: abandonWorkflowRun');
}

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

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

  const runId = args[0]!;

  const result = await abandonWorkflowRun(runId);

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

  if (result.abandoned) {
    console.log(`⊘ Run ${result.runId} abandoned (cancelled).`);
  } else {
    console.log(`Failed to abandon run ${result.runId}: ${result.message}`);
  }
}