import type { Command } from "commander";
export type { GatewayRpcOpts } from "./gateway-rpc.types.js";
import type { GatewayRpcOpts } from "./gateway-rpc.types.js";

type GatewayRpcRuntimeModule = typeof import("./gateway-rpc.runtime.js");

let gatewayRpcRuntimePromise: Promise<GatewayRpcRuntimeModule> | undefined;

async function loadGatewayRpcRuntime(): Promise<GatewayRpcRuntimeModule> {
  gatewayRpcRuntimePromise ??= import("./gateway-rpc.runtime.js");
  return gatewayRpcRuntimePromise;
}

export function addGatewayClientOptions(cmd: Command) {
  return cmd
    .option("--url <url>", "Gateway WebSocket URL (defaults to gateway.remote.url when configured)")
    .option("--token <token>", "Gateway token (if required)")
    .option("--timeout <ms>", "Timeout in ms", "30000")
    .option("--expect-final", "Wait for final response (agent)", false);
}

export async function callGatewayFromCli(
  method: string,
  opts: GatewayRpcOpts,
  params?: unknown,
  extra?: { expectFinal?: boolean; progress?: boolean },
) {
  const runtime = await loadGatewayRpcRuntime();
  return await runtime.callGatewayFromCliRuntime(method, opts, params, extra);
}
