import { api } from '@/services/api';

export type ExerciseCategoryDto = {
  key: string;
  name: string;
  sortOrder: number;
};

export type ExerciseDto = {
  key: string;
  name: string;
  description: string;
  categoryKey: string;
  categoryName: string;
  sortOrder: number;
};

export type ExerciseConfigResponse = {
  categories: ExerciseCategoryDto[];
  exercises: ExerciseDto[];
};

export type ExerciseLibraryItem = {
  key: string;
  name: string;
  description: string;
  category: string; // display name
};

export async function fetchExerciseConfig(): Promise<ExerciseConfigResponse> {
  return await api.get<ExerciseConfigResponse>('/exercises/config');
}

export function normalizeToLibraryItems(resp: ExerciseConfigResponse | null | undefined): ExerciseLibraryItem[] {
  if (!resp || !Array.isArray(resp.exercises)) return [];
  return resp.exercises.map((e) => ({
    key: e.key,
    name: e.name,
    description: e.description,
    category: e.categoryName || e.categoryKey,
  }));
}


