#!/usr/bin/env bun
import { main } from "../../src/hwlab-cli-lib.ts";

const argv = process.argv.slice(2);
if (argv[0] === "tasktree") {
  const { runTaskTreeCli } = await import("../../src/tasktree-cli.ts");
  try {
    const tasktreeArgs = argv.slice(1);
    const stdin = tasktreeArgs.includes("--stdin") ? await Bun.stdin.text() : undefined;
    const result = await runTaskTreeCli(tasktreeArgs, process.env, stdin);
    console.log(JSON.stringify(result, null, 2));
    process.exitCode = result?.ok === false ? 1 : 0;
  } catch (error) {
    console.log(JSON.stringify({ ok: false, operation: "tasktree", error: { code: (error as any)?.code ?? "tasktree_cli_error", message: (error as any)?.message ?? String(error) } }, null, 2));
    process.exitCode = 1;
  }
} else if (argv[0] === "caserun") {
  const { caserunNativeServiceCommand } = await import("../../src/caserun-native-service.ts");
  try {
    const service = argv[1] === "service" ? argv[2] : "";
    const action = argv[1] === "service" ? argv[3] ?? "status" : "status";
    const result = await caserunNativeServiceCommand({ service, action, cwd: process.cwd(), env: process.env });
    console.log(JSON.stringify(result, null, 2));
    process.exitCode = result?.ok === false ? 1 : 0;
  } catch (error: any) {
    console.log(JSON.stringify({ ok: false, operation: "caserun", error: { code: error?.code ?? "caserun_cli_error", message: error?.message ?? String(error) } }, null, 2));
    process.exitCode = 1;
  }
} else if (argv[0] === "hwpod" && argv[1] === "service") {
  const { hwpodNativeServiceCommand, hwpodNativeServicesStatus } = await import("../../src/hwpod-native-service.ts");
  try {
    const result = argv[2] === "status" && argv[3] === undefined
      ? await hwpodNativeServicesStatus({ cwd: process.cwd(), env: process.env })
      : await hwpodNativeServiceCommand({ service: argv[2], action: argv[3] ?? "status", cwd: process.cwd(), env: process.env });
    console.log(JSON.stringify(result, null, 2));
    process.exitCode = result?.ok === false ? 1 : 0;
  } catch (error: any) {
    console.log(JSON.stringify({ ok: false, operation: "hwpod", error: { code: error?.code ?? "hwpod_cli_error", message: error?.message ?? String(error) } }, null, 2));
    process.exitCode = 1;
  }
} else if (argv[0] === "hwpod") {
  const { mainHwpodCli } = await import("../../src/hwpod-harness-lib.ts");
  await mainHwpodCli(argv.slice(1));
} else if (argv[0] === "workbench") {
  const { runWorkbenchCli } = await import("../../src/workbench-cli.ts");
  try {
    const result = await runWorkbenchCli(argv.slice(1));
    console.log(JSON.stringify(result, null, 2));
    process.exitCode = result?.ok === false ? 1 : 0;
  } catch (error: any) {
    console.log(JSON.stringify({ ok: false, operation: "workbench", error: { code: error?.code ?? "workbench_cli_error", message: error?.message ?? String(error) } }, null, 2));
    process.exitCode = 1;
  }
} else if (argv[0] === "kafka") {
  const { mainKafkaCli } = await import("../../src/hwlab-cli/kafka-regenerate.ts");
  await mainKafkaCli(argv.slice(1));
} else if (argv[0] === "case" && argv[1] === "audit") {
  const { mainCaseRunAuditCli } = await import("../../src/hwlab-caserun-audit.ts");
  await mainCaseRunAuditCli(argv.slice(2));
} else await main(argv);
