#!/usr/bin/env tsx
/**
 * 有头可视化：依次跑 FTL → Expedited，便于肉眼核对 Shipment Type 卡片与 Step 2 字段
 *
 *   npm run visual:priority1-inspect-types
 */
process.env.RPA_HEADLESS = "false";
process.env.RPA_KEEP_BROWSER_OPEN = "true";
process.env.RPA_SLOW_MO_MS = "200";
process.env.RPA_DWELL_MS = "3500";
process.env.RPA_QUOTE_WAIT_MS = "120000";

import { mkdirSync } from "node:fs";
import { join } from "node:path";
import {
  DEFAULT_EXPEDITED_DEMO,
  DEFAULT_FTL_DEMO,
  formatPriority1InputSummary,
} from "@/workers/rpa/priority1/demo-input";
import { getShipmentConfig } from "@/workers/rpa/priority1/shipment-types";

const outRoot = join(process.cwd(), ".rpa", "visual-priority1-inspect");
mkdirSync(outRoot, { recursive: true });

const RUNS = [
  { demo: DEFAULT_FTL_DEMO, caseId: "ftl-inspect" },
  { demo: DEFAULT_EXPEDITED_DEMO, caseId: "expedited-inspect" },
] as const;

async function main(): Promise<void> {
  const { runPriority1VisualChain } = await import("./visual-priority1-full-chain");

  console.log("\n[inspect] 有头可视化 — 先 FTL，再 Expedited");
  console.log("[inspect] 每种类型 Step 1/2 填写后会停留 3.5s；结束后 Ctrl+C 继续下一种\n");

  for (let i = 0; i < RUNS.length; i += 1) {
    const run = RUNS[i]!;
    const cfg = getShipmentConfig(run.demo.shipmentType);
    console.log(`\n${"═".repeat(60)}`);
    console.log(`[inspect] ▶ ${cfg.displayName}`);
    console.log("─".repeat(60));
    console.log(formatPriority1InputSummary(run.demo));
    console.log("─".repeat(60));

    const result = await runPriority1VisualChain({
      demo: run.demo,
      caseId: run.caseId,
      outDir: join(outRoot, run.caseId),
      keepBrowserOpen: true,
    });

    if (!result.ok) {
      console.error(`[inspect] ${run.caseId} 失败: ${result.error}`);
      process.exit(1);
    }

    if (i < RUNS.length - 1) {
      console.log(
        `\n[inspect] ${cfg.displayName} 已完成。请核对 Shipment Type 黑底卡片与 Step 2。`,
      );
      console.log("[inspect] 终端按 Ctrl+C 关闭浏览器并继续 Expedited…\n");
    }
  }

  console.log("\n[inspect] 两种类型均已跑完。");
  console.log(`[inspect] 截图: ${outRoot}`);
}

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