import type { RecommendResponse, TipFeedback, IntegrationsResponse, UserProfile } from '@oo/shared-types'; const API = '/api'; async function apiFetch(path: string, init?: RequestInit): Promise { const res = await fetch(`${API}${path}`, { credentials: 'include', ...init, headers: { 'Content-Type': 'application/json', ...init?.headers, }, }); if (!res.ok) { const err = await res.json().catch(() => ({ error: res.statusText })); throw Object.assign(new Error(err.error ?? 'API error'), { status: res.status }); } if (res.status === 204) return undefined as T; return res.json() as T; } export async function getSession() { return apiFetch<{ user: { id: string; email: string; name?: string; image?: string } | null }>('/auth/session'); } export async function getRecommendation(): Promise { try { return await apiFetch('/recommend', { method: 'POST' }); } catch (e: any) { if (e.status === 204 || e.status === 422) return null; throw e; } } export async function sendFeedback(tipId: string, feedback: TipFeedback) { return apiFetch<{ ok: boolean }>(`/tip/${encodeURIComponent(tipId)}/feedback`, { method: 'POST', body: JSON.stringify(feedback), }); } export async function getIntegrations(): Promise { return apiFetch('/integrations'); } export async function disconnectIntegration(provider: string) { return apiFetch<{ ok: boolean }>(`/integrations/${provider}`, { method: 'DELETE' }); } export async function getProfile(): Promise { return apiFetch('/user/me'); } export async function giveConsent() { return apiFetch<{ ok: boolean }>('/user/consent', { method: 'POST' }); } export async function deleteAccount() { return apiFetch<{ ok: boolean }>('/user/me', { method: 'DELETE' }); } export async function logout() { return apiFetch<{ ok: boolean }>('/auth/logout', { method: 'POST' }); }