// SPEC: PJ2026-01010305 71FREQ预装 draft-2026-06-26-71freq-v03-hwpod-preinstall.
// Responsibility: Top-level web-probe CLI adapter and legacy hwlab nodes web-probe migration guard.
import type { Config } from "./config";
import type { RenderedCliResult } from "./output";
import { hwlabNodeWebProbeHelp, hwlabNodeWebProbeScreenshotHelp } from "./hwlab-node-help";
import { parseNodeWebProbeOptions } from "./hwlab-node/web-probe";
import { runNodeWebProbe } from "./hwlab-node/web-probe-observe";
import { runWebProbeArtifactInspect } from "./web-probe-artifact";
import { runWebProbeExternalForm, webProbeExternalFormHelp } from "./web-probe-external-form";
import { compactWebProbeProductSmokeProjection, runWebProbeProductSmoke } from "./web-probe-product-smoke";

export function webProbeHelp(): Record<string, unknown> {
  return hwlabNodeWebProbeHelp();
}

export function webProbeScreenshotHelp(): Record<string, unknown> {
  return hwlabNodeWebProbeScreenshotHelp();
}

export async function runWebProbeCommand(_config: Config, args: string[]): Promise<Record<string, unknown> | RenderedCliResult> {
  if (args[0] === "screenshot" && (args.includes("--help") || args.includes("-h") || args[1] === "help")) return webProbeScreenshotHelp();
  if (args[0] === "external-form" && (args.includes("--help") || args.includes("-h") || args[1] === "help")) return webProbeExternalFormHelp();
  if (args.length === 0 || args.includes("--help") || args.includes("-h") || args[0] === "help") return webProbeHelp();
  const format = args.includes("--json") ? "json" : args.includes("--yaml") ? "yaml" : "text";
  const parsedArgs = args.filter((item) => item !== "--json" && item !== "--yaml");
  if (parsedArgs[0] === "artifact") return runWebProbeArtifactInspect(parsedArgs.slice(1));
  if (parsedArgs[0] === "external-form") return await runWebProbeExternalForm(parsedArgs.slice(1));
  if (parsedArgs[0] === "product-smoke") {
    const result = runWebProbeProductSmoke(parsedArgs.slice(1));
    if (format === "text") return result;
    const { renderedText: _renderedText, contentType: _contentType, ...projection } = result;
    if (format === "json") return args.includes("--full") || args.includes("--raw")
      ? projection
      : compactWebProbeProductSmokeProjection(projection);
    return { ...result, renderedText: Bun.YAML.stringify(projection), contentType: "application/yaml" };
  }
  const result = runNodeWebProbe(parseNodeWebProbeOptions(parsedArgs));
  if (format === "text" || !("projection" in result) || result.projection === undefined) return result;
  if (format === "json") return result.projection;
  return { ...result, renderedText: Bun.YAML.stringify(result.projection), contentType: "application/yaml" };
}

export function legacyHwlabNodeWebProbeUnsupported(args: string[]): Record<string, unknown> {
  const filtered = args.filter((item) => item !== "help" && item !== "--help" && item !== "-h");
  const suffix = filtered.length === 0 ? "" : ` ${filtered.join(" ")}`;
  return {
    ok: false,
    status: "unsupported",
    command: `hwlab nodes web-probe${args.length === 0 ? "" : ` ${args.join(" ")}`}`.trim(),
    message: "`hwlab nodes web-probe` has moved to top-level `web-probe`; the old path is no longer an executable alias.",
    migration: {
      canonicalCommand: `bun scripts/cli.ts web-probe${suffix}`,
      help: "bun scripts/cli.ts web-probe --help",
    },
    valuesPrinted: false,
  };
}
