export interface HealthResponse {
  status: 'ok'
  version: string
}

export interface ReadinessComponent {
  status: 'ok' | 'unavailable' | 'not_configured'
  required: boolean
  latency_ms: number
  error?: string
}

export interface ReadinessResponse {
  ready: boolean
  status: 'ok' | 'degraded' | 'unavailable'
  components: Record<string, ReadinessComponent>
}

export class GatewaySystemClient {
  constructor(private readonly baseURL = '') {}

  health(signal?: AbortSignal): Promise<HealthResponse> {
    return this.get<HealthResponse>('/healthz', signal)
  }

  readiness(signal?: AbortSignal): Promise<ReadinessResponse> {
    return this.get<ReadinessResponse>('/readyz', signal)
  }

  private async get<T>(path: string, signal?: AbortSignal): Promise<T> {
    const response = await fetch(`${this.baseURL}${path}`, {
      headers: { Accept: 'application/json' },
      signal
    })
    if (!response.ok) {
      throw new Error(`gateway request failed: ${response.status}`)
    }
    return (await response.json()) as T
  }
}

