const BASE = '/api' function getToken(): string | null { return localStorage.getItem('token') } async function request(path: string, options: RequestInit = {}): Promise { const token = getToken() const headers: Record = { 'Content-Type': 'application/json', ...(options.headers as Record), } if (token) headers['Authorization'] = `Bearer ${token}` const res = await fetch(`${BASE}${path}`, { ...options, headers }) if (!res.ok) { const err = await res.json().catch(() => ({ detail: res.statusText })) throw new Error(err.detail || 'Request failed') } return res.json() } // ── Auth ────────────────────────────────────────────── export interface Token { access_token: string; token_type: string } export interface UserOut { id: string; email: string; coach_persona: string } export const auth = { register: (email: string, password: string, coach_persona = 'encouraging') => request('/auth/register', { method: 'POST', body: JSON.stringify({ email, password, coach_persona }) }), login: (email: string, password: string) => request('/auth/login', { method: 'POST', body: JSON.stringify({ email, password }) }), me: () => request('/auth/me'), updatePersona: (coach_persona: string) => request('/auth/me/persona', { method: 'PATCH', body: JSON.stringify({ coach_persona }) }), } // ── Workouts ────────────────────────────────────────── export interface Exercise { id: number; workout_id: number; name: string; sets?: number; reps?: number; weight_kg?: number; distance_km?: number } export interface Workout { id: number; user_id: string; date: string; type: string; duration_minutes: number; notes?: string; exercises: Exercise[] } export interface WorkoutCreate { type: string; duration_minutes: number; notes?: string; exercises?: Omit[] } export const workouts = { list: () => request('/workouts'), create: (data: WorkoutCreate) => request('/workouts', { method: 'POST', body: JSON.stringify(data) }), delete: (id: number) => request<{ ok: boolean }>(`/workouts/${id}`, { method: 'DELETE' }), } // ── Health Metrics ──────────────────────────────────── export interface HealthMetric { id: number; user_id: string; date: string; weight_kg?: number; sleep_hours?: number; energy_level?: number } export interface HealthMetricCreate { weight_kg?: number; sleep_hours?: number; energy_level?: number } export const health = { list: () => request('/health-metrics'), create: (data: HealthMetricCreate) => request('/health-metrics', { method: 'POST', body: JSON.stringify(data) }), delete: (id: number) => request<{ ok: boolean }>(`/health-metrics/${id}`, { method: 'DELETE' }), } // ── Chat ────────────────────────────────────────────── export const chat = { send: (message: string) => request<{ reply: string }>('/chat', { method: 'POST', body: JSON.stringify({ message }) }), }