import { WorkoutSession, WorkoutSet, ExerciseType } from '../types'; import { api } from './api'; // Define the shape of session coming from API (Prisma include) interface ApiSession extends Omit { startTime: string | number; // JSON dates are strings endTime?: string | number; sets: (Omit & { exercise?: { name: string; type: ExerciseType; } })[]; } export const getSessions = async (userId: string): Promise => { try { const sessions = await api.get('/sessions'); // Convert ISO date strings to timestamps return sessions.map((session) => ({ ...session, startTime: new Date(session.startTime).getTime(), endTime: session.endTime ? new Date(session.endTime).getTime() : undefined, sets: session.sets.map((set) => ({ ...set, exerciseName: set.exercise?.name || 'Unknown', type: set.exercise?.type || ExerciseType.STRENGTH })) as WorkoutSet[] })); } catch { return []; } }; export const saveSession = async (userId: string, session: WorkoutSession): Promise => { await api.post('/sessions', session); }; export const getActiveSession = async (userId: string): Promise => { try { const response = await api.get<{ success: boolean; session?: ApiSession }>('/sessions/active'); if (!response.success || !response.session) { return null; } const session = response.session; // Convert ISO date strings to timestamps return { ...session, startTime: new Date(session.startTime).getTime(), endTime: session.endTime ? new Date(session.endTime).getTime() : undefined, sets: session.sets.map((set) => ({ ...set, exerciseName: set.exercise?.name || 'Unknown', type: set.exercise?.type || ExerciseType.STRENGTH })) as WorkoutSet[] }; } catch { return null; } }; export const updateActiveSession = async (userId: string, session: WorkoutSession): Promise => { await api.put('/sessions/active', session); }; export const deleteSetFromActiveSession = async (userId: string, setId: string): Promise => { await api.delete(`/sessions/active/set/${setId}`); }; export const updateSetInActiveSession = async (userId: string, setId: string, setData: Partial): Promise => { const response = await api.put<{ success: boolean; updatedSet: WorkoutSet }>(`/sessions/active/set/${setId}`, setData); return response.updatedSet; }; export const deleteActiveSession = async (userId: string): Promise => { await api.delete('/sessions/active'); }; export const deleteSession = async (userId: string, id: string): Promise => { await api.delete(`/sessions/${id}`); }; export const addSetToActiveSession = async (userId: string, setData: any): Promise => { return await api.post('/sessions/active/log-set', setData); }; export const deleteAllUserData = (userId: string) => { // Not implemented in frontend };