-- Migration 006: Training Planning (Training Units)
-- Erstellt: 2026-04-22
-- Beschreibung: Trainingsplanung und -dokumentation

-- Training Units (Trainingseinheiten)
CREATE TABLE training_units (
    id SERIAL PRIMARY KEY,
    group_id INT REFERENCES training_groups(id) ON DELETE CASCADE,

    -- Planung
    planned_date DATE NOT NULL,
    planned_time_start TIME,
    planned_time_end TIME,
    planned_focus VARCHAR(200),

    -- Durchführung
    actual_date DATE,
    actual_time_start TIME,
    actual_time_end TIME,
    attendance_count INT,

    -- Status & Notizen
    status VARCHAR(50) DEFAULT 'planned',  -- planned, completed, cancelled
    notes TEXT,
    trainer_notes TEXT,

    -- Meta
    created_by INT REFERENCES profiles(id),
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_training_units_group ON training_units(group_id);
CREATE INDEX idx_training_units_date ON training_units(planned_date);
CREATE INDEX idx_training_units_status ON training_units(status);
CREATE INDEX idx_training_units_created_by ON training_units(created_by);

-- Training Unit Exercises (M:N - geplante und durchgeführte Übungen)
CREATE TABLE training_unit_exercises (
    id SERIAL PRIMARY KEY,
    training_unit_id INT REFERENCES training_units(id) ON DELETE CASCADE,
    exercise_id INT REFERENCES exercises(id),

    -- Reihenfolge
    order_index INT NOT NULL,

    -- Zeitplanung
    planned_duration_min INT,
    actual_duration_min INT,

    -- Notizen
    notes TEXT,
    modifications TEXT,  -- Anpassungen während der Durchführung

    created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_training_unit_exercises_unit ON training_unit_exercises(training_unit_id);
CREATE INDEX idx_training_unit_exercises_exercise ON training_unit_exercises(exercise_id);
CREATE INDEX idx_training_unit_exercises_order ON training_unit_exercises(training_unit_id, order_index);
