import { api } from './api';
import { SporadicSet } from '../types';

export async function getSporadicSets(): Promise<SporadicSet[]> {
    try {
        const response = await api.get('/sporadic-sets');
        if (response.success) {
            return response.sporadicSets || [];
        }
        return [];
    } catch (error) {
        console.error('Failed to fetch sporadic sets:', error);
        return [];
    }
}

export async function logSporadicSet(setData: {
    exerciseId: string;
    weight?: number;
    reps?: number;
    durationSeconds?: number;
    distanceMeters?: number;
    height?: number;
    bodyWeightPercentage?: number;
    note?: string;
    side?: 'LEFT' | 'RIGHT';
}): Promise<SporadicSet | null> {
    try {
        const response = await api.post('/sporadic-sets', setData);
        if (response.success) {
            return response.sporadicSet;
        }
        return null;
    } catch (error) {
        console.error('Failed to log sporadic set:', error);
        return null;
    }
}

export async function updateSporadicSet(id: string, setData: {
    weight?: number;
    reps?: number;
    durationSeconds?: number;
    distanceMeters?: number;
    height?: number;
    bodyWeightPercentage?: number;
    note?: string;
}): Promise<SporadicSet | null> {
    try {
        const response = await api.put(`/sporadic-sets/${id}`, setData);
        if (response.success) {
            return response.sporadicSet;
        }
        return null;
    } catch (error) {
        console.error('Failed to update sporadic set:', error);
        return null;
    }
}

export async function deleteSporadicSet(id: string): Promise<boolean> {
    try {
        const response = await api.delete(`/sporadic-sets/${id}`);
        return response.success || false;
    } catch (error) {
        console.error('Failed to delete sporadic set:', error);
        return false;
    }
}
