/**
 * Is the game server up? Server-side probe of `/health` with a short timeout, so the demo page can
 * show a friendly fallback instead of two broken iframes. Any failure (refused, slow, non-2xx,
 * unexpected body) counts as down.
 */
export async function gameServerHealthy(
  baseUrl: string = process.env.NEXT_PUBLIC_GAME_SERVER_URL || 'http://localhost:4000',
  timeoutMs = 1500,
): Promise<boolean> {
  try {
    const res = await fetch(new URL('/health', baseUrl), { cache: 'no-store', signal: AbortSignal.timeout(timeoutMs) });
    if (!res.ok) return false;
    const body = (await res.json().catch(() => null)) as { ok?: unknown } | null;
    return body?.ok === true;
  } catch {
    return false;
  }
}
