/**
 * Flock Freight RPA 探针（PRD §14.7）
 * 用法：RPA_MOCK_MODE=true npm run probe:flock-rpa
 *       或真实模式 FLOCK_RPA_ENABLED=true npm run probe:flock-rpa
 */
import { runFlockQuoteRpa } from "@/workers/rpa/flock/run-quote";
import { DEFAULT_FLOCK_DEMO } from "@/workers/rpa/flock/demo-input";
import type { FlockQuoteInput } from "@/workers/rpa/flock/types";

/** 与 flock-manual-20260713-152051.js 录制样例一致 */
const sample: FlockQuoteInput = {
  pickupDate: DEFAULT_FLOCK_DEMO.pickupDateDisplay,
  pickupZip: DEFAULT_FLOCK_DEMO.pickupZip,
  deliveryZip: DEFAULT_FLOCK_DEMO.deliveryZip,
  palletCount: DEFAULT_FLOCK_DEMO.palletCount,
  totalWeightLb: DEFAULT_FLOCK_DEMO.totalWeightLb,
  lengthIn: DEFAULT_FLOCK_DEMO.lengthIn,
  widthIn: DEFAULT_FLOCK_DEMO.widthIn,
  heightIn: DEFAULT_FLOCK_DEMO.heightIn,
};

async function main() {
  const rounds = Number(process.env.FLOCK_PROBE_ROUNDS ?? "1");
  let ok = 0;
  for (let i = 1; i <= rounds; i++) {
    console.log(`[probe-flock] round ${i}/${rounds} …`);
    console.log(
      `[probe-flock] sample ${sample.pickupZip}→${sample.deliveryZip} ` +
        `${sample.palletCount}pl ${sample.totalWeightLb}lb`,
    );
    const result = await runFlockQuoteRpa(sample);
    if (result.ok && result.quotes.length >= 2) {
      ok += 1;
      console.log(
        `[probe-flock] OK ref=${result.reference ?? "-"} prices=${result.quotes
          .map((q) => `${q.label}:$${q.totalUsd}`)
          .join(", ")}`,
      );
    } else {
      console.error(`[probe-flock] FAIL: ${result.errorMessage ?? "unknown"}`);
      process.exitCode = 1;
      return;
    }
  }
  console.log(`[probe-flock] passed ${ok}/${rounds}`);
}

main().catch((error) => {
  console.error("[probe-flock] fatal:", error);
  process.exit(1);
});
