/**
 * `workflow status` — Show active (running + paused) workflow runs.
 *
 * @example
 *   workflow status
 *   workflow status --json
 */

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

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

interface ActiveRun {
  id: string;
  workflowName: string;
  status: string;
  duration: number; // ms
  currentNode?: string;
}

async function getActiveRuns(_cwd?: string): Promise<ActiveRun[]> {
  throw new Error('not implemented yet: getActiveRuns');
}

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

export async function statusCommand(
  _args: string[],
  options: CliOptions,
): Promise<void> {
  const runs = await getActiveRuns(options.cwd);

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

  if (runs.length === 0) {
    console.log('No active workflow runs.');
    return;
  }

  console.log('Active workflow runs:');
  console.log('');

  printTable(
    runs.map((r) => ({
      id: r.id,
      workflow: r.workflowName,
      status: r.status,
      duration: formatDuration(r.duration),
      currentNode: r.currentNode ?? '-',
    })),
    [
      { header: 'ID', field: 'id', minWidth: 26 },
      { header: 'Workflow', field: 'workflow', minWidth: 20 },
      { header: 'Status', field: 'status', minWidth: 10 },
      { header: 'Duration', field: 'duration', minWidth: 10 },
      { header: 'Current Node', field: 'currentNode', minWidth: 15 },
    ],
  );
}