/**
 * Integration test setup — programmatically starts ClawRouter proxy.
 *
 * Shared across all integration test files via beforeAll/afterAll.
 * Starts the proxy on a worker-scoped port, waits for /health to return 200,
 * and caches the handle so test files in the same worker share one instance.
 */

import { startProxy } from "../../src/proxy.js";
import { resolveOrGenerateWalletKey } from "../../src/auth.js";
import type { ProxyHandle } from "../../src/proxy.js";

const HEALTH_POLL_INTERVAL_MS = 200;
const HEALTH_TIMEOUT_MS = 5_000;

let proxyHandle: ProxyHandle | undefined;

function getTestPort(): number {
  // Keep worker 1 on the historical default (8402), then offset others.
  const workerRaw = process.env.VITEST_POOL_ID ?? process.env.VITEST_WORKER_ID ?? "1";
  const workerId = Number.parseInt(workerRaw, 10);
  if (Number.isInteger(workerId) && workerId >= 1) {
    return 8401 + workerId;
  }
  return 8402;
}

/**
 * Start the test proxy on a worker-scoped port.
 * Polls /health until it returns 200 (up to 5s), then returns the handle.
 * Reuses an existing handle if already started.
 */
export async function startTestProxy(): Promise<ProxyHandle> {
  if (proxyHandle) return proxyHandle;

  const wallet = await resolveOrGenerateWalletKey();
  const testPort = getTestPort();

  proxyHandle = await startProxy({
    wallet,
    port: testPort,
    skipBalanceCheck: true,
  });

  // Wait for /health to return 200
  const deadline = Date.now() + HEALTH_TIMEOUT_MS;
  while (Date.now() < deadline) {
    try {
      const res = await fetch(`${proxyHandle.baseUrl}/health`);
      if (res.ok) return proxyHandle;
    } catch {
      // proxy not ready yet
    }
    await new Promise((r) => setTimeout(r, HEALTH_POLL_INTERVAL_MS));
  }

  throw new Error(`Test proxy did not become healthy within ${HEALTH_TIMEOUT_MS}ms`);
}

/**
 * Stop the test proxy and clear the cached handle.
 */
export async function stopTestProxy(): Promise<void> {
  if (!proxyHandle) return;
  await proxyHandle.close();
  proxyHandle = undefined;
}

/**
 * Get the base URL of the running test proxy (e.g. http://127.0.0.1:8402).
 * Throws if the proxy has not been started.
 */
export function getTestProxyUrl(): string {
  if (!proxyHandle) throw new Error("Test proxy not started — call startTestProxy() first");
  return proxyHandle.baseUrl;
}
