import type { Exercise, Routine, RecordRoutine, User, Equipment, MuscleGroup, Set, SuperSet, RecordExercise, WorkoutStats } from '../types/models'; // Base API URL - should be configurable via environment variables in a real app const API_BASE_URL = '/api'; // Generic fetch with error handling async function fetchApi( endpoint: string, options: RequestInit = {} ): Promise { const url = `${API_BASE_URL}${endpoint}`; const response = await fetch(url, { headers: { 'Content-Type': 'application/json', ...options.headers, }, ...options, }); if (!response.ok) { const error = await response.text(); throw new Error(error || `API request failed with status ${response.status}`); } return response.json(); } // Equipment API services export const EquipmentService = { getAll: () => fetchApi('/equipment'), getById: (id: number) => fetchApi(`/equipment/${id}`), create: (equipment: Equipment) => fetchApi('/equipment', { method: 'POST', body: JSON.stringify(equipment), }), update: (id: number, equipment: Equipment) => fetchApi(`/equipment/${id}`, { method: 'PUT', body: JSON.stringify(equipment), }), delete: (id: number) => fetchApi(`/equipment/${id}`, { method: 'DELETE', }), }; // MuscleGroup API services export const MuscleGroupService = { getAll: () => fetchApi('/musclegroups'), getById: (id: number) => fetchApi(`/musclegroups/${id}`), create: (muscleGroup: MuscleGroup) => fetchApi('/musclegroups', { method: 'POST', body: JSON.stringify(muscleGroup), }), update: (id: number, muscleGroup: MuscleGroup) => fetchApi(`/musclegroups/${id}`, { method: 'PUT', body: JSON.stringify(muscleGroup), }), delete: (id: number) => fetchApi(`/musclegroups/${id}`, { method: 'DELETE', }), }; // Exercise API services export const ExerciseService = { getAll: () => fetchApi('/exercises'), getById: (id: number) => fetchApi(`/exercises/${id}`), create: (exercise: Exercise) => fetchApi('/exercises', { method: 'POST', body: JSON.stringify(exercise), }), update: (id: number, exercise: Exercise) => fetchApi(`/exercises/${id}`, { method: 'PUT', body: JSON.stringify(exercise), }), delete: (id: number) => fetchApi(`/exercises/${id}`, { method: 'DELETE', }), }; // Set API services export const SetService = { getAll: (exerciseId: number) => fetchApi(`/exercises/${exerciseId}/sets`), getById: (id: number) => fetchApi(`/sets/${id}`), create: (set: Set) => fetchApi('/sets', { method: 'POST', body: JSON.stringify(set), }), update: (id: number, set: Set) => fetchApi(`/sets/${id}`, { method: 'PUT', body: JSON.stringify(set), }), delete: (id: number) => fetchApi(`/sets/${id}`, { method: 'DELETE', }), }; // SuperSet API services export const SuperSetService = { getAll: () => fetchApi('/supersets'), getById: (id: number) => fetchApi(`/supersets/${id}`), create: (superSet: SuperSet) => fetchApi('/supersets', { method: 'POST', body: JSON.stringify(superSet), }), update: (id: number, superSet: SuperSet) => fetchApi(`/supersets/${id}`, { method: 'PUT', body: JSON.stringify(superSet), }), delete: (id: number) => fetchApi(`/supersets/${id}`, { method: 'DELETE', }), }; // Routine API services export const RoutineService = { getAll: () => fetchApi('/routines'), getById: (id: number) => fetchApi(`/routines/${id}`), create: (routine: Routine) => fetchApi('/routines', { method: 'POST', body: JSON.stringify(routine), }), update: (id: number, routine: Routine) => fetchApi(`/routines/${id}`, { method: 'PUT', body: JSON.stringify(routine), }), delete: (id: number) => fetchApi(`/routines/${id}`, { method: 'DELETE', }), }; // RecordRoutine (Workout) API services export const WorkoutService = { getAll: () => fetchApi('/recordroutines'), getById: (id: number) => fetchApi(`/recordroutines/${id}`), create: (workout: RecordRoutine) => fetchApi('/recordroutines', { method: 'POST', body: JSON.stringify(workout), }), update: (id: number, workout: RecordRoutine) => fetchApi(`/recordroutines/${id}`, { method: 'PUT', body: JSON.stringify(workout), }), delete: (id: number) => fetchApi(`/recordroutines/${id}`, { method: 'DELETE', }), // Additional method to get workout statistics for the home page getStats: () => fetchApi('/stats'), }; // RecordExercise API services export const RecordExerciseService = { getAll: (recordRoutineId: number) => fetchApi(`/recordroutines/${recordRoutineId}/exercises`), getById: (id: number) => fetchApi(`/recordexercises/${id}`), create: (recordExercise: RecordExercise) => fetchApi('/recordexercises', { method: 'POST', body: JSON.stringify(recordExercise), }), update: (id: number, recordExercise: RecordExercise) => fetchApi(`/recordexercises/${id}`, { method: 'PUT', body: JSON.stringify(recordExercise), }), delete: (id: number) => fetchApi(`/recordexercises/${id}`, { method: 'DELETE', }), }; // User profile service export const ProfileService = { get: async () => { const user = await fetchApi('/users/1'); user.birthDate = new Date(user.birthDate).toISOString(); return user; }, update: async (profile: User) => { profile.birthDate = new Date(profile.birthDate).toISOString(); profile.isFemale = profile.isFemale === 'true'; profile.weight = +profile.weight; profile.height = +profile.height; return await fetchApi('/users/1', { method: 'PUT', body: JSON.stringify(profile), })}, };