-- Tabata Kine Programs Schema
-- Migration 005: New kiné program system
-- Replaces the old 3-program model with 4 kiné programs

-- ─── Kine Programs ──────────────────────────────────────────

CREATE TABLE IF NOT EXISTS public.kine_programs (
  id TEXT PRIMARY KEY,  -- 'debutant', 'intermediaire', 'avance', 'bureau'
  title TEXT NOT NULL,
  title_en TEXT NOT NULL,
  description TEXT NOT NULL,
  description_en TEXT NOT NULL,
  tier TEXT NOT NULL CHECK (tier IN ('free', 'premium')),
  accent_color TEXT NOT NULL DEFAULT '#30D158',
  icon TEXT NOT NULL DEFAULT 'seedling',
  duration_weeks INTEGER NOT NULL DEFAULT 4,
  sessions_per_week INTEGER NOT NULL,
  total_sessions INTEGER NOT NULL,
  equipment JSONB NOT NULL DEFAULT '{"required": [], "optional": []}',
  focus_areas TEXT[] DEFAULT '{}',
  focus_areas_en TEXT[] DEFAULT '{}',
  principles TEXT[] DEFAULT '{}',
  principles_en TEXT[] DEFAULT '{}',
  completion_criteria TEXT[] DEFAULT '{}',
  completion_criteria_en TEXT[] DEFAULT '{}',
  next_program_id TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- ─── Kine Sessions ──────────────────────────────────────────

CREATE TABLE IF NOT EXISTS public.kine_sessions (
  id TEXT PRIMARY KEY,  -- 'deb-w1-s1', 'int-w2-s3', etc.
  program_id TEXT NOT NULL REFERENCES public.kine_programs(id) ON DELETE CASCADE,
  week_number INTEGER NOT NULL CHECK (week_number >= 1 AND week_number <= 6),
  day_number INTEGER NOT NULL CHECK (day_number >= 1 AND day_number <= 7),
  title TEXT NOT NULL,
  title_en TEXT NOT NULL,
  description TEXT,
  description_en TEXT,
  focus TEXT[] DEFAULT '{}',
  focus_en TEXT[] DEFAULT '{}',
  warmup JSONB NOT NULL,        -- WarmupPhase structure
  blocks JSONB NOT NULL,         -- TabataBlock[] array
  cooldown JSONB NOT NULL,       -- CooldownPhase structure
  equipment TEXT[] DEFAULT '{}',
  total_rounds INTEGER NOT NULL,
  total_duration INTEGER NOT NULL,  -- minutes
  calories INTEGER NOT NULL DEFAULT 0,
  sort_order INTEGER NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- ─── Kine Exercises Library ─────────────────────────────────
-- Track individual exercises for video generation

CREATE TABLE IF NOT EXISTS public.kine_exercises (
  id TEXT PRIMARY KEY,     -- slug: 'squat-classique', 'pont-fessier'
  name_fr TEXT NOT NULL,
  name_en TEXT NOT NULL,
  conseil_fr TEXT,
  conseil_en TEXT,
  modification TEXT,
  modification_en TEXT,
  progression TEXT,
  progression_en TEXT,
  video_url TEXT,
  video_generated BOOLEAN DEFAULT FALSE,
  video_generation_status TEXT CHECK (video_generation_status IN ('pending', 'generating', 'completed', 'failed')),
  video_generation_job_id TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- ─── Kine Weeks ─────────────────────────────────────────────

CREATE TABLE IF NOT EXISTS public.kine_weeks (
  id TEXT PRIMARY KEY,     -- 'deb-w1', 'int-w2', etc.
  program_id TEXT NOT NULL REFERENCES public.kine_programs(id) ON DELETE CASCADE,
  week_number INTEGER NOT NULL,
  title TEXT NOT NULL,
  title_en TEXT NOT NULL,
  description TEXT,
  description_en TEXT,
  focus TEXT,
  focus_en TEXT,
  is_deload BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- ─── Indexes ────────────────────────────────────────────────

CREATE INDEX IF NOT EXISTS idx_kine_sessions_program ON public.kine_sessions(program_id);
CREATE INDEX IF NOT EXISTS idx_kine_sessions_week ON public.kine_sessions(program_id, week_number);
CREATE INDEX IF NOT EXISTS idx_kine_weeks_program ON public.kine_weeks(program_id);
CREATE INDEX IF NOT EXISTS idx_kine_exercises_generated ON public.kine_exercises(video_generated);

-- ─── Row Level Security ─────────────────────────────────────

ALTER TABLE public.kine_programs ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.kine_sessions ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.kine_exercises ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.kine_weeks ENABLE ROW LEVEL SECURITY;

-- Public read access
CREATE POLICY "Public read kine_programs" ON public.kine_programs FOR SELECT USING (true);
CREATE POLICY "Public read kine_sessions" ON public.kine_sessions FOR SELECT USING (true);
CREATE POLICY "Public read kine_exercises" ON public.kine_exercises FOR SELECT USING (true);
CREATE POLICY "Public read kine_weeks" ON public.kine_weeks FOR SELECT USING (true);

-- ─── Seed: Debutant Program ─────────────────────────────────

INSERT INTO public.kine_programs (id, title, title_en, description, description_en, tier, accent_color, icon, duration_weeks, sessions_per_week, total_sessions) VALUES
('debutant', 'Débutant', 'Beginner',
 'Apprendre le protocole tabata, construire les bases techniques de chaque mouvement fondamental.',
 'Learn the tabata protocol, build technical foundations for each fundamental movement.',
 'free', '#30D158', 'seedling', 4, 3, 12)
ON CONFLICT (id) DO NOTHING;

-- Seed weeks
INSERT INTO public.kine_weeks (id, program_id, week_number, title, title_en, description, description_en, focus, focus_en, is_deload) VALUES
('deb-w1', 'debutant', 1, 'Découverte du rythme', 'Finding Your Rhythm', 'Un bloc tabata par séance (4 min) + échauffement + retour au calme.', 'One tabata block per session + warmup + cooldown.', 'Apprentissage du protocole 20/10', 'Learning the 20/10 protocol', false),
('deb-w2', 'debutant', 2, 'Consolidation', 'Building Strength', '2 blocs tabata + 1 min récup entre les blocs.', '2 tabata blocks + 1 min recovery between blocks.', 'Consolidation des mouvements', 'Consolidating movements', false),
('deb-w3', 'debutant', 3, 'Montée en intensité', 'Building Intensity', '3 blocs tabata + 1 min récupération entre chaque.', '3 tabata blocks + 1 min recovery between each.', 'Impacts très légers, volume augmenté', 'Very light impact, increased volume', false),
('deb-w4', 'debutant', 4, 'Décharge & consolidation', 'Deload & Consolidation', 'Retour à 2 blocs. Volume réduit de 40%.', 'Back to 2 blocks. Volume reduced by 40%.', 'Technique parfaite, respiration', 'Perfect technique, breathing', true)
ON CONFLICT (id) DO NOTHING;
