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

// 与后端保持一致的数据结构定义
export interface ExerciseLibraryItem {
  key: string;
  name: string;
  description?: string;
  category: string; // 中文分类名（显示用）
  targetMuscleGroups: string;
  equipmentName?: string;
  beginnerReps?: number;
  beginnerSets?: number;
  breathingCycles?: number;
  holdDuration?: number;
  specialInstructions?: string;
}

export interface ExerciseCategoryDto {
  key: string; // 英文 key
  name: string; // 中文名
  type: 'mat_pilates' | 'equipment_pilates';
  equipmentName?: string;
  sortOrder?: number;
}

export interface ExerciseDto {
  key: string;
  name: string;
  description?: string;
  categoryKey: string;
  categoryName: string;
  targetMuscleGroups: string;
  equipmentName?: string;
  beginnerReps?: number;
  beginnerSets?: number;
  breathingCycles?: number;
  holdDuration?: number;
  specialInstructions?: string;
  sortOrder?: number;
}

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

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,
    targetMuscleGroups: e.targetMuscleGroups,
    equipmentName: e.equipmentName,
    beginnerReps: e.beginnerReps,
    beginnerSets: e.beginnerSets,
    breathingCycles: e.breathingCycles,
    holdDuration: e.holdDuration,
    specialInstructions: e.specialInstructions,
  }));
}

