/**
 * `workflow convert` — Convert a .sop.md file to a YAML workflow definition.
 *
 * Reads the SOP markdown file, parses its structure, and outputs
 * a corresponding YAML workflow definition.
 *
 * @example
 *   workflow convert deploy.sop.md
 *   workflow convert deploy.sop.md --output workflows/deploy.yaml
 */

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

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

interface ConvertResult {
  inputFile: string;
  outputFile: string;
  workflowName: string;
  nodeCount: number;
}

async function convertSopToYaml(
  _inputPath: string,
  _outputPath?: string,
): Promise<ConvertResult> {
  throw new Error('not implemented yet: convertSopToYaml');
}

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

export async function convertCommand(
  args: string[],
  options: CliOptions,
): Promise<void> {
  if (args.length === 0) {
    throw new Error('Missing required argument: <file.sop.md>\n\nUsage: workflow convert <file.sop.md> [--output <path>]');
  }

  const inputPath = args[0]!;

  if (!inputPath.endsWith('.sop.md')) {
    throw new Error(`Input file must end with .sop.md, got: ${inputPath}`);
  }

  const result = await convertSopToYaml(inputPath, options.output);

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

  console.log(`Converted: ${result.inputFile}`);
  console.log(`  Output: ${result.outputFile}`);
  console.log(`  Workflow: ${result.workflowName}`);
  console.log(`  Nodes: ${result.nodeCount}`);
}