/**
 * TabataFit Data Layer
 * New 3-Program System + Legacy Support
 */

import { PROGRAMS, ALL_PROGRAM_WORKOUTS, ASSESSMENT_WORKOUT } from './programs'
import { TRAINERS } from './trainers'
import { ACHIEVEMENTS } from './achievements'
import type { ProgramId } from '../types'

// Re-export new program system
export {
  PROGRAMS,
  ALL_PROGRAM_WORKOUTS,
  ASSESSMENT_WORKOUT,
  TRAINERS,
  ACHIEVEMENTS,
}

// ═══════════════════════════════════════════════════════════════════════════
// PROGRAM LOOKUPS
// ═══════════════════════════════════════════════════════════════════════════

export function getProgramById(id: ProgramId) {
  return PROGRAMS[id]
}

export function getAllPrograms() {
  return Object.values(PROGRAMS)
}

export function getProgramWorkouts(programId: ProgramId) {
  const program = PROGRAMS[programId]
  if (!program) return []
  return program.weeks.flatMap((week) => week.workouts)
}

export function getWorkoutById(id: string) {
  return ALL_PROGRAM_WORKOUTS.find((w) => w.id === id)
}

export function getWorkoutProgramId(workoutId: string): ProgramId | null {
  for (const [programId, program] of Object.entries(PROGRAMS)) {
    for (const week of program.weeks) {
      if (week.workouts.some((w) => w.id === workoutId)) {
        return programId as ProgramId
      }
    }
  }
  return null
}

// ═══════════════════════════════════════════════════════════════════════════
// TRAINER LOOKUPS
// ═══════════════════════════════════════════════════════════════════════════

export function getTrainerById(id: string) {
  return TRAINERS.find((t) => t.id === id)
}

export function getTrainerByName(name: string) {
  return TRAINERS.find((t) => t.name.toLowerCase() === name.toLowerCase())
}

// ═══════════════════════════════════════════════════════════════════════════
// CATEGORY METADATA
// ═══════════════════════════════════════════════════════════════════════════

export const CATEGORIES: { id: ProgramId | 'all'; label: string }[] = [
  { id: 'all', label: 'All' },
  { id: 'upper-body', label: 'Upper Body' },
  { id: 'lower-body', label: 'Lower Body' },
  { id: 'full-body', label: 'Full Body' },
]

// Legacy exports for backward compatibility (to be removed)
export { WORKOUTS } from './workouts'
export { COLLECTIONS, FEATURED_COLLECTION_ID } from './collections'
