// ─── API base URL ─────────────────────────────────────────────────────────────
// Point this at your legal-service or the Next.js frontend proxy.
// Override via EXPO_PUBLIC_API_URL env var.

export const API_BASE = process.env.EXPO_PUBLIC_API_URL ?? 'http://192.168.42.4:3200'

export async function apiFetch(path: string, init?: RequestInit) {
  const res = await fetch(`${API_BASE}${path}`, {
    headers: { 'Content-Type': 'application/json', ...(init?.headers ?? {}) },
    ...init,
  })
  if (!res.ok) {
    let message = `HTTP ${res.status}`
    try { const d = await res.json(); message = d.message ?? message } catch {}
    throw new Error(message)
  }
  return res.json()
}
