/**
 * TabataFit Data Layer
 * Single source of truth + helper lookups
 */

import { WORKOUTS } from './workouts'
import { TRAINERS } from './trainers'
import { COLLECTIONS, PROGRAMS, FEATURED_COLLECTION_ID } from './collections'
import { ACHIEVEMENTS } from './achievements'
import type { WorkoutCategory, WorkoutLevel, WorkoutDuration } from '../types'

// Re-export raw data
export { WORKOUTS, TRAINERS, COLLECTIONS, PROGRAMS, ACHIEVEMENTS, FEATURED_COLLECTION_ID }

// ═══════════════════════════════════════════════════════════════════════════
// WORKOUT LOOKUPS
// ═══════════════════════════════════════════════════════════════════════════

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

export function getWorkoutsByCategory(category: WorkoutCategory) {
  return WORKOUTS.filter(w => w.category === category)
}

export function getWorkoutsByTrainer(trainerId: string) {
  return WORKOUTS.filter(w => w.trainerId === trainerId)
}

export function getWorkoutsByLevel(level: WorkoutLevel) {
  return WORKOUTS.filter(w => w.level === level)
}

export function getWorkoutsByDuration(duration: WorkoutDuration) {
  return WORKOUTS.filter(w => w.duration === duration)
}

export function getFeaturedWorkouts() {
  return WORKOUTS.filter(w => w.isFeatured)
}

export function getPopularWorkouts(count = 8) {
  // Simulate popularity — pick a diverse spread
  return WORKOUTS.filter(w => ['1', '11', '21', '31', '41', '42', '2', '32'].includes(w.id)).slice(0, count)
}

// ═══════════════════════════════════════════════════════════════════════════
// 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())
}

// ═══════════════════════════════════════════════════════════════════════════
// COLLECTION LOOKUPS
// ═══════════════════════════════════════════════════════════════════════════

export function getCollectionById(id: string) {
  return COLLECTIONS.find(c => c.id === id)
}

export function getCollectionWorkouts(collectionId: string) {
  const collection = getCollectionById(collectionId)
  if (!collection) return []
  return collection.workoutIds.map(id => getWorkoutById(id)).filter(Boolean)
}

export function getFeaturedCollection() {
  return getCollectionById(FEATURED_COLLECTION_ID)
}

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

export function getProgramById(id: string) {
  return PROGRAMS.find(p => p.id === id)
}

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

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

// SF Symbol icon map for collections
export const COLLECTION_ICONS: Record<string, string> = {
  'morning-energizer': 'sunrise.fill',
  'no-equipment': 'figure.strengthtraining.traditional',
  '7-day-burn': 'flame.fill',
  'quick-intense': 'bolt.fill',
  'core-focus': 'target',
  'leg-day': 'figure.walk',
}

// Collection color map
export const COLLECTION_COLORS: Record<string, string> = {
  'morning-energizer': '#FFD60A',
  'no-equipment': '#30D158',
  '7-day-burn': '#FF3B30',
  'quick-intense': '#FF3B30',
  'core-focus': '#5AC8FA',
  'leg-day': '#BF5AF2',
}
