#!/usr/bin/env node
import {
  assertCompilerPortableDependencies,
  consola,
  resolveProjectContext
} from "./chunk-JO5AMVZU.mjs";
import "./chunk-5NYBTZB4.mjs";
import "./chunk-776W3UGV.mjs";
import "./chunk-NAK77WXW.mjs";
import "./chunk-TAEQKBJB.mjs";
import "./chunk-IAYRNVUC.mjs";
import "./chunk-H76MT5UR.mjs";
import "./chunk-H6XDQJ3N.mjs";

// src/agent-verifier/agent-workspace-verifier.ts
import { readFile } from "fs/promises";
main().catch((error) => {
  console.error(error instanceof Error ? error.message : String(error));
  process.exit(1);
});
async function main() {
  const [command, ...args] = process.argv.slice(2);
  if (command === "--help" || command === "help") {
    printHelp();
    return;
  }
  if (command === "materialize-prepared-workspace") {
    if (args.includes("--help")) {
      printHelp();
      return;
    }
    await materializePreparedWorkspace(args);
    return;
  }
  await verifyAgentWorkspace(command ?? "verify", args);
}
function printHelp() {
  console.log(`Dreamboard agent workspace verifier

Usage:
  agent-workspace-verifier materialize-prepared-workspace --input <path>
  agent-workspace-verifier preflight [--env local|staging|prod]
  agent-workspace-verifier verify [--env local|staging|prod]
  agent-workspace-verifier fin [--env local|staging|prod]
`);
}
async function materializePreparedWorkspace(args) {
  const inputPath = readRequiredOption(args, "--input");
  const { materializeWorkspaceProject } = await import("./materialize-workspace-OZKOQCSQ.mjs");
  const input = JSON.parse(
    await readFile(inputPath, "utf8")
  );
  await materializeWorkspaceProject({
    ...input,
    agentManaged: true,
    workspacePrepared: true,
    allowCreateGame: false,
    installDependencies: false
  });
  consola.success(`Prepared workspace in ${input.targetDir}`);
}
async function verifyAgentWorkspace(rawMode, args) {
  const requestedMode = parseVerificationMode(rawMode);
  const parsedFlags = parseConfigArgs(args);
  const { projectRoot, projectConfig, config } = await resolveProjectContext(parsedFlags);
  await assertCompilerPortableDependencies({ projectRoot, projectConfig });
  if (requestedMode === "preflight") {
    consola.success("Agent workspace preflight passed.");
    return;
  }
  if (process.env.DREAMBOARD_AGENT_FINAL_SYNC_VERIFY === "1") {
    await runFullBackendConnectedVerification(parsedFlags);
    return;
  }
  if (requestedMode === "cloud-local" || requestedMode === "verify" || requestedMode === "fin") {
    await runCloudLocalVerification(projectRoot, projectConfig, config);
    return;
  }
  throw new Error(
    "Agent workspaces now run cloud-local verification directly. Use `fin` only through the generated wrapper."
  );
}
async function runFullBackendConnectedVerification(parsedFlags) {
  const [{ default: cmdSync }, { default: cmdCompile }, { default: cmdTest }] = await Promise.all([
    import("./sync-C6S3OGCD.mjs"),
    import("./compile-576O7TYP.mjs"),
    import("./test-Y5UGQV7J.mjs")
  ]);
  await runCommandDefinition(cmdSync, { ...parsedFlags, force: true });
  await runCommandDefinition(cmdCompile, parsedFlags);
  await runCommandDefinition(
    resolveTestSubCommand(cmdTest, "generate"),
    parsedFlags
  );
  await runCommandDefinition(resolveTestSubCommand(cmdTest, "run"), parsedFlags);
}
async function runCommandDefinition(command, args) {
  if (!command.run) {
    throw new Error("Verifier command is missing a runnable step.");
  }
  await command.run({ args, rawArgs: [], cmd: command });
}
function resolveTestSubCommand(cmdTest, name) {
  const subCommands = cmdTest.subCommands;
  const command = subCommands?.[name];
  if (!command) {
    throw new Error(`Verifier command is missing test ${name}.`);
  }
  return command;
}
function parseVerificationMode(value) {
  if (value === "preflight" || value === "verify" || value === "fin" || value === "cloud-local") {
    return value;
  }
  throw new Error(
    `Expected mode to be one of preflight, verify, fin, or cloud-local. Received: ${String(value)}`
  );
}
function parseConfigArgs(args) {
  const flags = {};
  for (let index = 0; index < args.length; index += 1) {
    const arg = args[index];
    if (arg === "--env") {
      const value = args[++index];
      if (value === "local" || value === "staging" || value === "prod") {
        flags.env = value;
        continue;
      }
      throw new Error(`Invalid --env value: ${String(value)}`);
    }
    if (arg === "--token") {
      flags.token = args[++index];
      continue;
    }
    if (arg === "--skip-install") {
      continue;
    }
    throw new Error(`Unknown verifier argument: ${arg}`);
  }
  return flags;
}
async function runCloudLocalVerification(projectRoot, projectConfig, config) {
  const [
    { scaffoldStaticWorkspace },
    { loadManifest },
    { applyWorkspaceCodegen },
    { reconcileWorkspaceDependencies },
    { assertReducerContractPreflight },
    { getProjectLocalMaintainerRegistry }
  ] = await Promise.all([
    import("./static-scaffold-JBUE3ROP.mjs"),
    import("./local-files-QVJ2H3MH.mjs"),
    import("./workspace-codegen-WPZHMATU.mjs"),
    import("./workspace-dependencies-B6A2ZX55.mjs"),
    import("./reducer-contract-preflight-COD2CO22.mjs"),
    import("./project-state-XKUSCFSV.mjs")
  ]);
  consola.start("Refreshing static scaffold...");
  await scaffoldStaticWorkspace(projectRoot, "update", {
    localMaintainerRegistry: getProjectLocalMaintainerRegistry(projectConfig)
  });
  consola.start("Applying workspace codegen...");
  const manifest = await loadManifest(projectRoot);
  await applyWorkspaceCodegen({ projectRoot, manifest });
  consola.start("Reconciling workspace dependencies...");
  await reconcileWorkspaceDependencies(projectRoot);
  consola.start("Validating reducer contract...");
  await assertReducerContractPreflight(projectRoot);
  const [{ runLocalTypecheck }, { assertReducerBundleSmoke }] = await Promise.all([
    import("./local-typecheck-2JWG5IGL.mjs"),
    import("./reducer-bundle-preflight-7NYZF5ZT.mjs")
  ]);
  consola.start("Running local typecheck...");
  const typecheckResult = await runLocalTypecheck(projectRoot);
  if (typecheckResult.skipped) {
    if (typecheckResult.output) consola.warn(typecheckResult.output);
  } else if (!typecheckResult.success) {
    if (typecheckResult.output) consola.error(typecheckResult.output);
    throw new Error(
      "Local typecheck failed. Fix the diagnostics before syncing."
    );
  }
  consola.start("Smoke-testing reducer bundle...");
  await assertReducerBundleSmoke({ projectRoot, manifest });
  const {
    generateReducerNativeArtifacts,
    isReducerNativeTestingWorkspace,
    runReducerNativeScenarios
  } = await import("./reducer-native-test-harness-QC7HZUK4.mjs");
  if (await isReducerNativeTestingWorkspace(projectRoot)) {
    const { bases } = await generateReducerNativeArtifacts({
      projectRoot,
      gameId: projectConfig.gameId,
      compiledResultId: projectConfig.compile?.latestSuccessful?.resultId
    });
    const summary = await runReducerNativeScenarios({
      projectRoot,
      projectConfig,
      resolvedConfig: config,
      runner: "reducer",
      gameId: projectConfig.gameId,
      compiledResultId: projectConfig.compile?.latestSuccessful?.resultId
    });
    if (summary.failed > 0) {
      const failures = summary.results.filter((result) => !result.success).map(
        (result) => result.error ? `FAIL ${result.id}: ${result.error}` : `FAIL ${result.id}`
      );
      throw new Error(
        [
          `Reducer-native verification failed: ${summary.failed} failed, ${summary.passed} passed.`,
          ...failures
        ].join("\n")
      );
    }
    consola.success(`Generated ${bases.length} reducer-native base state(s).`);
  }
  consola.success("Agent workspace cloud-local verification passed.");
}
function readRequiredOption(args, name) {
  const index = args.indexOf(name);
  const value = index >= 0 ? args[index + 1] : void 0;
  if (!value) {
    throw new Error(`${name} is required.`);
  }
  return value;
}
//# sourceMappingURL=agent-workspace-verifier.mjs.map