File size: 3,368 Bytes
3c48b32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const BASE = '/api'

function getToken(): string | null {
  return localStorage.getItem('token')
}

async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
  const token = getToken()
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
    ...(options.headers as Record<string, string>),
  }
  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<Token>('/auth/register', { method: 'POST', body: JSON.stringify({ email, password, coach_persona }) }),
  login: (email: string, password: string) =>
    request<Token>('/auth/login', { method: 'POST', body: JSON.stringify({ email, password }) }),
  me: () => request<UserOut>('/auth/me'),
  updatePersona: (coach_persona: string) =>
    request<UserOut>('/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<Exercise, 'id' | 'workout_id'>[] }

export const workouts = {
  list: () => request<Workout[]>('/workouts'),
  create: (data: WorkoutCreate) => request<Workout>('/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<HealthMetric[]>('/health-metrics'),
  create: (data: HealthMetricCreate) => request<HealthMetric>('/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 }) }),
}