#!/usr/bin/env tsx
/**
 * FTL canonical 路径 — 人机协作录制
 * 自动填 Step1 + 拖车类型；Additional Services 及后续交人工并记录至报价
 *
 *   npm run record:priority1-ftl-path
 *   $env:PRIORITY1_PATH_ID="F01-ftl-dry-van-full"; npm run record:priority1-ftl-path
 */
import { join } from "node:path";
import {
  formatPriority1InputSummary,
  PRIORITY1_CANONICAL_PATHS,
} from "@/workers/rpa/priority1/demo-input";
import { runPriority1FtlPathRecording } from "./visual-priority1-full-chain";

async function main(): Promise<void> {
  const pathId = process.env.PRIORITY1_PATH_ID?.trim() ?? "F01-ftl-dry-van-full";
  const canonical = PRIORITY1_CANONICAL_PATHS.find(
    (p) => p.id === pathId || p.id.startsWith(pathId),
  );
  if (!canonical) {
    console.error(`[record-ftl] 未找到路径: ${pathId}`);
    console.error("可用 F 路径: F01 … F11");
    process.exit(1);
  }
  if (!canonical.id.startsWith("F")) {
    console.error(`[record-ftl] 非 FTL 路径: ${canonical.id}`);
    process.exit(1);
  }

  const outDir = join(
    process.cwd(),
    ".rpa",
    "recordings",
    `${canonical.id}-human-session`,
  );

  console.log("\n════════ FTL 人机协作录制 ════════");
  console.log(`路径: ${canonical.id} — ${canonical.title}`);
  console.log(formatPriority1InputSummary(canonical.input));
  console.log(`\n[record-ftl] 输出目录: ${outDir}`);
  console.log("[record-ftl] 浏览器打开后，在 handoff 清单处人工补操作");
  console.log("[record-ftl] 出现报价自动保存；或 Ctrl+C 保存已录步骤\n");

  const result = await runPriority1FtlPathRecording({
    demo: canonical.input,
    caseId: canonical.id,
    outDir,
    keepBrowserOpen: true,
  });

  process.exit(result.ok ? 0 : 1);
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});
