-- Migration 056: Kombinationsübungen (Phase 2 MVP) — Slots + Pool-Kandidaten
-- Fachgrundlage: functional/Shinkan Trainingsmodule Kombinationsuebungen Spezifikation V2.md §6

ALTER TABLE exercises
    ADD COLUMN IF NOT EXISTS exercise_kind VARCHAR(20) NOT NULL DEFAULT 'simple'
        CHECK (exercise_kind IN ('simple', 'combination')),
    ADD COLUMN IF NOT EXISTS method_archetype VARCHAR(80),
    ADD COLUMN IF NOT EXISTS method_profile JSONB NOT NULL DEFAULT '{}'::jsonb;

CREATE INDEX IF NOT EXISTS idx_exercises_exercise_kind ON exercises(exercise_kind);
CREATE INDEX IF NOT EXISTS idx_exercises_method_archetype ON exercises(method_archetype)
    WHERE method_archetype IS NOT NULL;

CREATE TABLE IF NOT EXISTS combination_exercise_slots (
    id SERIAL PRIMARY KEY,
    exercise_id INT NOT NULL REFERENCES exercises(id) ON DELETE CASCADE,
    slot_index INT NOT NULL,
    title VARCHAR(200),
    UNIQUE (exercise_id, slot_index)
);

CREATE INDEX IF NOT EXISTS idx_combination_exercise_slots_exercise ON combination_exercise_slots(exercise_id);

CREATE TABLE IF NOT EXISTS combination_slot_candidates (
    id SERIAL PRIMARY KEY,
    slot_id INT NOT NULL REFERENCES combination_exercise_slots(id) ON DELETE CASCADE,
    candidate_exercise_id INT NOT NULL REFERENCES exercises(id) ON DELETE CASCADE,
    sort_order INT NOT NULL DEFAULT 0,
    UNIQUE (slot_id, candidate_exercise_id)
);

CREATE INDEX IF NOT EXISTS idx_combination_slot_candidates_slot ON combination_slot_candidates(slot_id);
CREATE INDEX IF NOT EXISTS idx_combination_slot_candidates_exercise ON combination_slot_candidates(candidate_exercise_id);
