-- Migration 010: Umbenennung "training_styles" → "style_directions"
-- Erstellt: 2026-04-23
-- Beschreibung: Konsistente Terminologie - "Stilrichtungen" statt "Trainingsstile"
-- Grund: "Trainingsstil" wird neue separate Dimension (Breitensport/Leistungssport)

-- ============================================================================
-- PHASE 1: Tabellen umbenennen
-- ============================================================================

-- Haupttabelle umbenennen
ALTER TABLE IF EXISTS training_styles RENAME TO style_directions;

-- Junction-Tabelle umbenennen
ALTER TABLE IF EXISTS exercise_styles RENAME TO exercise_style_directions;

-- Training-Style-Target-Groups Junction umbenennen
ALTER TABLE IF EXISTS training_style_target_groups RENAME TO style_direction_target_groups;

-- ============================================================================
-- PHASE 2: Spalten umbenennen
-- ============================================================================

-- In style_directions (vormals training_styles)
-- Keine Spalten-Umbennung nötig - parent_style_id kann bleiben (bedeutet parent_direction)

-- In exercise_style_directions (vormals exercise_styles)
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'exercise_style_directions' AND column_name = 'training_style_id'
    ) THEN
        ALTER TABLE exercise_style_directions
        RENAME COLUMN training_style_id TO style_direction_id;
    END IF;
END $$;

-- In style_direction_target_groups (vormals training_style_target_groups)
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'style_direction_target_groups' AND column_name = 'training_style_id'
    ) THEN
        ALTER TABLE style_direction_target_groups
        RENAME COLUMN training_style_id TO style_direction_id;
    END IF;
END $$;

-- ============================================================================
-- PHASE 3: Constraints umbenennen (automatisch durch ALTER TABLE)
-- ============================================================================

-- Foreign Key Constraints werden automatisch umbenannt
-- Indizes werden automatisch umbenannt

-- ============================================================================
-- PHASE 4: Validierung
-- ============================================================================

-- Nach dieser Migration:
-- - training_styles → style_directions
-- - exercise_styles → exercise_style_directions
-- - training_style_target_groups → style_direction_target_groups
-- - training_style_id → style_direction_id (in Junction-Tabellen)

-- Nächste Migration 011: Neue Dimension "training_types" (Breitensport/Leistungssport)
