async function apiFetch(url: string, options?: RequestInit) {
  const res = await fetch(url, { credentials: 'include', ...options });
  const json = await res.json().catch(() => ({}));
  if (!res.ok) throw new Error(json.error || `Request failed: ${res.status}`);
  return json;
}

export async function getAIConfig() {
  return apiFetch('/api/ai-config');
}

export async function saveAIConfig(body: Record<string, unknown>) {
  return apiFetch('/api/ai-config', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
}
