// Scratch driver for issue 33766: mints a full-access MCP client token (same call `t3 thread` makes)
// for the dev server whose base dir is argv[2]. Prints JSON {origin, token}.
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as References from "effect/References";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
const W = process.env.WT!;
const Net = await import(`${W}/packages/shared/src/Net.ts`);
const EnvironmentAuth = await import(`${W}/apps/server/src/auth/EnvironmentAuth.ts`);
const ServerConfig = await import(`${W}/apps/server/src/config.ts`);
const { resolveCliAuthConfig } = await import(`${W}/apps/server/src/cli/config.ts`);
const { readPersistedServerRuntimeState } = await import(`${W}/apps/server/src/serverRuntimeState.ts`);
const program = Effect.gen(function* () {
  const config = yield* resolveCliAuthConfig({ baseDir: Option.some(process.argv[2]) }, Option.none(), { quietLogs: true });
  return yield* Effect.gen(function* () {
    const state = yield* readPersistedServerRuntimeState(config.serverRuntimeStatePath);
    const auth = yield* EnvironmentAuth.EnvironmentAuth;
    const issued = yield* auth.issueMcpClientSession({ label: "issue-33766 driver", access: "full-access", peerLink: false, client: { deviceType: "bot" } });
    return { origin: Option.getOrNull(state)?.origin, token: issued.token };
  }).pipe(Effect.provide(EnvironmentAuth.layerRuntime.pipe(Layer.provide(ServerConfig.layer(config)), Layer.provide(Layer.succeed(References.MinimumLogLevel, "Error")))));
});
const out = await Effect.runPromise(program.pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, Net.layer))) as any);
console.log(JSON.stringify(out));
process.exit(0);
