import type { MusicVibe } from './workout'

/**
 * Tabata Program Types
 * Physiotherapist-designed 4-program progressive system
 *
 * Model: Programme → Semaine → Séance → Bloc → Exercice
 * Each block alternates between odd/even exercises on tabata rounds.
 */

// ─── Tabata Types ─────────────────────────────────────────────

export type TabataProgramId = 'debutant' | 'intermediaire' | 'avance' | 'bureau'

export type ProgramTier = 'free' | 'premium'

/** A single exercise with tabata metadata */
export interface TabataExercise {
  name: string
  nameEn: string
  /** Physiotherapist tip (the 📋 text from the guide) */
  conseil: string
  conseilEn: string
  /** Easier alternative */
  modification?: string
  modificationEn?: string
  /** Harder alternative */
  progression?: string
  progressionEn?: string
}

/** A tabata block alternates two exercises on odd/even rounds */
export interface TabataBlock {
  id: string
  /** Rounds 1, 3, 5, 7 */
  oddExercise: TabataExercise
  /** Rounds 2, 4, 6, 8 */
  evenExercise: TabataExercise
  /** Standard tabata: 8 rounds per block */
  rounds: number
  /** Work interval in seconds (typically 20) */
  workTime: number
  /** Rest interval in seconds (typically 10) */
  restTime: number
}

/** A timed movement for warmup or cooldown */
export interface TimedMovement {
  name: string
  nameEn: string
  /** Duration in seconds */
  duration: number
}

export interface WarmupPhase {
  movements: TimedMovement[]
  totalDuration: number
}

export interface CooldownPhase {
  movements: TimedMovement[]
  totalDuration: number
}

/** A session = warmup + blocks + cooldown */
export interface TabataSession {
  id: string // e.g. 'deb-w1-s1'
  week: number
  /** Order within the week (1-based) */
  order: number
  title: string
  titleEn: string
  description: string
  descriptionEn: string
  focus: string[]
  focusEn: string[]
  warmup: WarmupPhase
  blocks: TabataBlock[]
  cooldown: CooldownPhase
  equipment: string[]
  /** Sum of all block rounds */
  totalRounds: number
  /** Estimated total duration in minutes including warmup/cooldown */
  totalDuration: number
  /** Estimated calories */
  calories: number
  /** Music vibe for this session (from workout program) */
  musicVibe?: MusicVibe
}

export interface TabataWeek {
  weekNumber: number
  title: string
  titleEn: string
  description: string
  descriptionEn: string
  focus: string
  focusEn: string
  /** Whether this is a deload week (reduced volume) */
  isDeload: boolean
  sessions: TabataSession[]
}

export interface TabataProgram {
  id: TabataProgramId
  title: string
  titleEn: string
  description: string
  descriptionEn: string
  tier: ProgramTier
  /** Accent color for UI */
  accentColor: string
  /** Icon name for display */
  icon: string
  durationWeeks: number
  /** Sessions per week — may vary by week */
  sessionsPerWeek: number
  totalSessions: number
  equipment: {
    required: string[]
    optional: string[]
  }
  focusAreas: string[]
  focusAreasEn: string[]
  /** Program rules/principles displayed to user */
  principles: string[]
  principlesEn: string[]
  /** Criteria to pass before moving to next program */
  completionCriteria: string[]
  completionCriteriaEn: string[]
  /** Recommended next program */
  nextProgramId?: TabataProgramId
  weeks: TabataWeek[]
}

/** Tabata-specific achievements */
export interface TabataAchievement {
  id: string
  title: string
  titleEn: string
  description: string
  descriptionEn: string
  icon: string
  requirement: number
  type: 'sessions' | 'streak' | 'weeks' | 'programs' | 'calories'
}

/** Progress tracking for tabata programs */
export interface TabataProgramProgress {
  programId: TabataProgramId
  currentWeek: number
  currentSessionIndex: number
  completedSessionIds: string[]
  isProgramCompleted: boolean
  startDate?: string
  lastSessionDate?: string
}

// ─── Timer Phases ─────────────────────────────────────────────

export type TabataTimerPhase =
  | 'WARMUP'
  | 'WORK'
  | 'REST'
  | 'INTER_BLOCK_REST'
  | 'COOLDOWN'
  | 'COMPLETE'

// ─── Legacy Types (kept for backward compatibility) ───────────

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

export type WeekNumber = 1 | 2 | 3 | 4

export type ProgramWorkout = {
  id: string
  week: WeekNumber
  order: number
  title: string
  description: string
  duration: 4
  exercises: ProgramExercise[]
  equipment: string[]
  focus: string[]
  tips: string[]
}

export type ProgramExercise = {
  name: string
  duration: 20
  reps?: string
  modification?: string
  progression?: string
}

export interface Week {
  weekNumber: WeekNumber
  title: string
  description: string
  focus: string
  workouts: ProgramWorkout[]
}

export interface Program {
  id: ProgramId
  title: string
  description: string
  durationWeeks: 4
  workoutsPerWeek: 5
  totalWorkouts: 20
  equipment: {
    required: string[]
    optional: string[]
  }
  focusAreas: string[]
  weeks: Week[]
}

export interface ProgramProgress {
  programId: ProgramId
  currentWeek: WeekNumber
  currentWorkoutIndex: number
  completedWorkoutIds: string[]
  isProgramCompleted: boolean
  startDate?: string
  lastWorkoutDate?: string
}

export interface AssessmentExercise {
  name: string
  duration: 20
  purpose: string
}

export interface Assessment {
  id: 'initial-assessment'
  title: string
  description: string
  duration: 4
  exercises: AssessmentExercise[]
  tips: string[]
}

export interface AssessmentResult {
  completedAt: string
  exercisesCompleted: string[]
  notes?: string
  recommendedProgram?: ProgramId
}

export type UserProgramState = {
  selectedProgram: ProgramId | null
  programsProgress: Record<ProgramId, ProgramProgress>
  assessment: {
    isCompleted: boolean
    result: AssessmentResult | null
  }
}
