import type { MusicVibe } from './workout'

/**
 * Workout Program Types
 * Body Zone + Difficulty model
 * Program = Warmup → 3 Tabatas → Stretch
 * Tabata = 2 exercises × 8 rounds (20s work / 10s rest)
 */

// ─── Enums ──────────────────────────────────────────────────────

export type BodyZone = 'upper-body' | 'lower-body' | 'full-body'

export type ProgramLevel = 'Beginner' | 'Intermediate' | 'Advanced'

// ─── Exercise ───────────────────────────────────────────────────

export interface ProgramExercise {
  name: string
  nameEn: string
  tip?: string
  tipEn?: string
  modification?: string
  modificationEn?: string
  progression?: string
  progressionEn?: string
  videoUrl?: string | null
}

// ─── Timed Exercise (warmup / stretch) ──────────────────────────

export interface TimedExercise {
  name: string
  nameEn: string
  duration: number          // seconds
  videoUrl: string | null
  tip?: string
  tipEn?: string
}

export interface WarmupBlock {
  exercises: TimedExercise[]
  totalDuration: number     // seconds (sum of exercises.duration)
}

export interface StretchBlock {
  exercises: TimedExercise[]
  totalDuration: number
}

// ─── Tabata ─────────────────────────────────────────────────────

export interface WorkoutTabata {
  id: string
  position: 1 | 2 | 3
  exercise1: ProgramExercise
  exercise2: ProgramExercise
  rounds: number
  workTime: number
  restTime: number
}

// ─── Program ────────────────────────────────────────────────────

export interface WorkoutProgram {
  id: string
  title: string
  description: string | null
  bodyZone: BodyZone
  level: ProgramLevel
  isFree: boolean
  musicVibe: MusicVibe
  estimatedDuration: number
  estimatedCalories: number
  icon: string | null
  accentColor: string | null
  sortOrder: number
  warmup: WarmupBlock
  tabatas: WorkoutTabata[]
  stretch: StretchBlock
  createdAt: string
  updatedAt: string
}

// ─── Supabase Row Types ─────────────────────────────────────────

export interface WorkoutProgramRow {
  id: string
  title: string
  description: string | null
  body_zone: BodyZone
  level: ProgramLevel
  is_free: boolean
  music_vibe: MusicVibe
  estimated_duration: number
  estimated_calories: number
  icon: string | null
  accent_color: string | null
  sort_order: number
  created_at: string
  updated_at: string
}

export interface WorkoutTabataRow {
  id: string
  program_id: string
  position: 1 | 2 | 3
  exercise_1_name: string
  exercise_1_name_en: string | null
  exercise_1_tip: string | null
  exercise_1_tip_en: string | null
  exercise_1_modification: string | null
  exercise_1_modification_en: string | null
  exercise_1_progression: string | null
  exercise_1_progression_en: string | null
  exercise_1_video_url: string | null
  exercise_2_name: string
  exercise_2_name_en: string | null
  exercise_2_tip: string | null
  exercise_2_tip_en: string | null
  exercise_2_modification: string | null
  exercise_2_modification_en: string | null
  exercise_2_progression: string | null
  exercise_2_progression_en: string | null
  exercise_2_video_url: string | null
  rounds: number
  work_time: number
  rest_time: number
  created_at: string
}

export interface WorkoutTimedExerciseRow {
  id: string
  program_id: string
  position: number
  name: string
  name_en: string | null
  tip: string | null
  tip_en: string | null
  duration: number
  video_url: string | null
  created_at: string
}

// ─── Display Metadata ───────────────────────────────────────────

export const BODY_ZONE_META: Record<BodyZone, {
  label: string
  labelEn: string
  icon: string
  color: string
  descKey: string
}> = {
  'upper-body': {
    label: 'Haut du corps',
    labelEn: 'Upper Body',
    icon: 'figure.strengthtraining.traditional',
    color: '#4A90D9',
    descKey: 'screens:home.zoneDescUpper',
  },
  'lower-body': {
    label: 'Bas du corps',
    labelEn: 'Lower Body',
    icon: 'figure.run',
    color: '#9B59B6',
    descKey: 'screens:home.zoneDescLower',
  },
  'full-body': {
    label: 'Corps entier',
    labelEn: 'Full Body',
    icon: 'figure.cooldown',
    color: '#00C896',
    descKey: 'screens:home.zoneDescFull',
  },
}

export const LEVEL_META: Record<ProgramLevel, { label: string; labelEn: string; color: string }> = {
  Beginner: { label: 'Débutant', labelEn: 'Beginner', color: '#30D158' },
  Intermediate: { label: 'Intermédiaire', labelEn: 'Intermediate', color: '#FF9500' },
  Advanced: { label: 'Avancé', labelEn: 'Advanced', color: '#FF453A' },
}
