-- Migration 005: Exercises (Übungen)
-- Erstellt: 2026-04-22
-- Beschreibung: Übungsverwaltung (Kern-Modul)

-- Exercises (Übungen)
CREATE TABLE exercises (
    id SERIAL PRIMARY KEY,
    title VARCHAR(300) NOT NULL,
    summary TEXT,  -- Kurzbeschreibung
    goal TEXT NOT NULL,  -- Ziel der Übung
    execution TEXT NOT NULL,  -- Durchführung
    preparation TEXT,  -- Vorbereitung / Aufbau
    trainer_notes TEXT,  -- Hinweise für Trainer
    equipment JSONB,  -- ["Matten", "Pratzen", "Bälle"]

    -- Metadaten
    duration_min INT,
    duration_max INT,
    group_size_min INT,
    group_size_max INT,
    age_groups JSONB,  -- ["minis", "kinder", "erwachsene"]

    -- Fachliche Zuordnung
    focus_area VARCHAR(100),  -- karate, selbstverteidigung, gewaltschutz
    secondary_areas JSONB,
    training_character VARCHAR(50),  -- grundlage, aufbau, vertiefung, festigung, diagnose

    -- Methode
    primary_method_id INT REFERENCES training_methods(id),
    secondary_method_ids JSONB,

    -- Freigabe
    visibility VARCHAR(50) DEFAULT 'private',  -- private, club, official
    status VARCHAR(50) DEFAULT 'draft',  -- draft, in_review, approved, archived
    created_by INT REFERENCES profiles(id),
    club_id INT REFERENCES clubs(id),

    -- Import
    import_source VARCHAR(50),  -- mediawiki, manual
    import_id VARCHAR(200),

    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_exercises_created_by ON exercises(created_by);
CREATE INDEX idx_exercises_club ON exercises(club_id);
CREATE INDEX idx_exercises_visibility ON exercises(visibility);
CREATE INDEX idx_exercises_status ON exercises(status);
CREATE INDEX idx_exercises_focus_area ON exercises(focus_area);

-- Übungs-Fähigkeiten (M:N)
CREATE TABLE exercise_skills (
    id SERIAL PRIMARY KEY,
    exercise_id INT REFERENCES exercises(id) ON DELETE CASCADE,
    skill_id INT REFERENCES skills(id),
    is_primary BOOLEAN DEFAULT false,
    intensity INT CHECK (intensity BETWEEN 1 AND 5),  -- 1-5
    development_contribution VARCHAR(50),  -- low, medium, high
    required_level INT,  -- Eingangs-Niveau
    target_level INT,  -- Ziel-Niveau
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_exercise_skills_exercise ON exercise_skills(exercise_id);
CREATE INDEX idx_exercise_skills_skill ON exercise_skills(skill_id);

-- Übungsvarianten
CREATE TABLE exercise_variants (
    id SERIAL PRIMARY KEY,
    exercise_id INT REFERENCES exercises(id) ON DELETE CASCADE,
    variant_name VARCHAR(200) NOT NULL,
    description TEXT,
    execution_changes TEXT,
    duration_min INT,
    duration_max INT,
    equipment_changes JSONB,
    difficulty_adjustment VARCHAR(50),  -- easier, harder, adapted
    age_group_override JSONB,
    skill_focus_override JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_exercise_variants_exercise ON exercise_variants(exercise_id);

-- Medien (1:N)
CREATE TABLE exercise_media (
    id SERIAL PRIMARY KEY,
    exercise_id INT REFERENCES exercises(id) ON DELETE CASCADE,
    media_type VARCHAR(50),  -- image, video, document, sketch
    file_path TEXT,
    title VARCHAR(200),
    description TEXT,
    sort_order INT,
    is_primary BOOLEAN DEFAULT false,
    context VARCHAR(100),  -- ablauf, detail, trainer_hint
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_exercise_media_exercise ON exercise_media(exercise_id);
