-- ═══════════════════════════════════════════════════════════════════════════
-- Migration 005: Body-Zone Workout Program System
-- Replaces unused programs/program_workouts tables with new model:
--   workout_programs (body_zone + difficulty) → program_tabatas (3 per program)
--   Each tabata: 2 exercises × 8 rounds (standard tabata: 20s work, 10s rest)
-- ═══════════════════════════════════════════════════════════════════════════

-- Drop unused tables (0 rows of data, never used in production)
DROP TABLE IF EXISTS program_workouts CASCADE;
DROP TABLE IF EXISTS programs CASCADE;

-- ─── Body zone enum ─────────────────────────────────────────────
CREATE TYPE body_zone AS ENUM ('upper-body', 'lower-body', 'full-body');

-- ─── workout_programs table ─────────────────────────────────────
CREATE TABLE workout_programs (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    title TEXT NOT NULL,
    description TEXT,
    body_zone body_zone NOT NULL,
    level TEXT NOT NULL CHECK (level IN ('Beginner', 'Intermediate', 'Advanced')),
    is_free BOOLEAN NOT NULL DEFAULT FALSE,
    estimated_duration INTEGER NOT NULL DEFAULT 12,
    estimated_calories INTEGER NOT NULL,
    icon TEXT,
    accent_color TEXT,
    sort_order INTEGER NOT NULL DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- ─── program_tabatas table ──────────────────────────────────────
CREATE TABLE program_tabatas (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    program_id UUID NOT NULL REFERENCES workout_programs(id) ON DELETE CASCADE,
    position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 3),

    -- Exercise 1 (performed on odd rounds: 1, 3, 5, 7)
    exercise_1_name TEXT NOT NULL,
    exercise_1_name_en TEXT,
    exercise_1_tip TEXT,
    exercise_1_tip_en TEXT,
    exercise_1_modification TEXT,
    exercise_1_modification_en TEXT,
    exercise_1_progression TEXT,
    exercise_1_progression_en TEXT,

    -- Exercise 2 (performed on even rounds: 2, 4, 6, 8)
    exercise_2_name TEXT NOT NULL,
    exercise_2_name_en TEXT,
    exercise_2_tip TEXT,
    exercise_2_tip_en TEXT,
    exercise_2_modification TEXT,
    exercise_2_modification_en TEXT,
    exercise_2_progression TEXT,
    exercise_2_progression_en TEXT,

    -- Tabata timing
    rounds INTEGER NOT NULL DEFAULT 8,
    work_time INTEGER NOT NULL DEFAULT 20,
    rest_time INTEGER NOT NULL DEFAULT 10,

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    UNIQUE(program_id, position)
);

-- ─── RLS ────────────────────────────────────────────────────────
ALTER TABLE workout_programs ENABLE ROW LEVEL SECURITY;
ALTER TABLE program_tabatas ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Public read workout_programs" ON workout_programs
    FOR SELECT USING (true);
CREATE POLICY "Public read program_tabatas" ON program_tabatas
    FOR SELECT USING (true);
CREATE POLICY "Admin all workout_programs" ON workout_programs
    FOR ALL USING (EXISTS (SELECT 1 FROM admin_users WHERE id = auth.uid()));
CREATE POLICY "Admin all program_tabatas" ON program_tabatas
    FOR ALL USING (EXISTS (SELECT 1 FROM admin_users WHERE id = auth.uid()));

-- ─── Triggers ───────────────────────────────────────────────────
CREATE TRIGGER set_workout_programs_updated_at
    BEFORE UPDATE ON workout_programs
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

-- ─── Indexes ────────────────────────────────────────────────────
CREATE INDEX idx_workout_programs_body_zone ON workout_programs(body_zone);
CREATE INDEX idx_workout_programs_level ON workout_programs(level);
CREATE INDEX idx_workout_programs_is_free ON workout_programs(is_free);
CREATE INDEX idx_program_tabatas_program_id ON program_tabatas(program_id);
