/**
 * `workflow runs` — List recent workflow runs with filters.
 *
 * @example
 *   workflow runs
 *   workflow runs --status failed --limit 10 --json
 *   workflow runs --all
 */

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

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

interface RunRecord {
  id: string;
  workflowName: string;
  status: string;
  startedAt: string;
  duration?: number; // ms, absent if still running
  currentNode?: string;
}

async function listWorkflowRuns(_filters: {
  status?: string;
  limit?: number;
  all?: boolean;
  cwd?: string;
}): Promise<RunRecord[]> {
  throw new Error('not implemented yet: listWorkflowRuns');
}

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

export async function runsCommand(
  args: string[],
  options: CliOptions,
): Promise<void> {
  // Parse --status, --limit, --all from args/options.
  // These are already extracted by parseArgs into options.
  const status = typeof (options as Record<string, unknown>).status === 'string'
    ? (options as Record<string, unknown>).status as string
    : undefined;
  const limit = typeof (options as Record<string, unknown>).limit === 'string'
    ? parseInt((options as Record<string, unknown>).limit as string, 10)
    : 50;
  const all = (options as Record<string, unknown>).all === true;

  const runs = await listWorkflowRuns({
    status,
    limit,
    all,
    cwd: options.cwd,
  });

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

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

  console.log(`Showing ${runs.length} run(s):`);
  console.log('');

  printTable(
    runs.map((r) => ({
      id: r.id,
      workflow: r.workflowName,
      status: r.status,
      started: formatTimestamp(new Date(r.startedAt)),
      duration: r.duration != null ? 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: 'Started', field: 'started', minWidth: 19 },
      { header: 'Duration', field: 'duration', minWidth: 10 },
      { header: 'Node', field: 'currentNode', minWidth: 15 },
    ],
  );
}