-- Migration 013: Training Types → Focus Area Hierarchie
-- Author: Claude Code
-- Date: 2026-04-23
-- Purpose: Training Types sind kontextabhängig vom Fokusbereich

DO $$
BEGIN

-- ============================================================================
-- ADD focus_area_id TO training_types
-- ============================================================================

-- Add focus_area_id column if not exists
IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'training_types'
    AND column_name = 'focus_area_id'
) THEN
    ALTER TABLE training_types
    ADD COLUMN focus_area_id INT REFERENCES focus_areas(id) ON DELETE CASCADE;

    CREATE INDEX idx_training_types_focus_area ON training_types(focus_area_id);

    RAISE NOTICE 'Added focus_area_id to training_types';
END IF;


-- ============================================================================
-- MIGRATE EXISTING DATA (assign to Karate if exists)
-- ============================================================================

-- Assign existing training types to Karate (if Karate focus area exists)
DO $migrate$
DECLARE
    karate_id INT;
BEGIN
    -- Get Karate focus area ID
    SELECT id INTO karate_id FROM focus_areas WHERE name = 'Karate' LIMIT 1;

    IF karate_id IS NOT NULL THEN
        -- Assign existing training types to Karate
        UPDATE training_types
        SET focus_area_id = karate_id
        WHERE focus_area_id IS NULL;

        RAISE NOTICE 'Assigned existing training types to Karate (focus_area_id: %)', karate_id;
    ELSE
        RAISE NOTICE 'No Karate focus area found - skipping migration';
    END IF;
END $migrate$;


-- ============================================================================
-- SEED EXAMPLE DATA (focus-area-specific training types)
-- ============================================================================

DO $seed$
DECLARE
    karate_id INT;
    gewaltschutz_id INT;
    fitness_id INT;
BEGIN
    -- Get focus area IDs
    SELECT id INTO karate_id FROM focus_areas WHERE name = 'Karate' LIMIT 1;
    SELECT id INTO gewaltschutz_id FROM focus_areas WHERE name = 'Gewaltschutz' LIMIT 1;
    SELECT id INTO fitness_id FROM focus_areas WHERE name = 'Fitness' LIMIT 1;

    -- Karate-specific training types (if Karate exists)
    IF karate_id IS NOT NULL THEN
        INSERT INTO training_types (name, abbreviation, description, focus_area_id, sort_order, status)
        VALUES
            ('Dan-Vorbereitung', 'DAN', 'Vorbereitung auf Dan-Prüfungen', karate_id, 4, 'active')
        ON CONFLICT (name) DO NOTHING;
    END IF;

    -- Gewaltschutz-specific training types (if exists)
    IF gewaltschutz_id IS NOT NULL THEN
        INSERT INTO training_types (name, abbreviation, description, focus_area_id, sort_order, status)
        VALUES
            ('Präventivkurse', 'PREV', 'Präventive Selbstverteidigung und Deeskalation', gewaltschutz_id, 1, 'active'),
            ('Intensivtraining', 'INT', 'Intensives Selbstverteidigungs-Training', gewaltschutz_id, 2, 'active'),
            ('Spezialkurse', 'SPEC', 'Spezielle Kurse (Frauen, Sicherheitspersonal, etc.)', gewaltschutz_id, 3, 'active')
        ON CONFLICT (name) DO NOTHING;
    END IF;

    -- Fitness-specific training types (if exists)
    IF fitness_id IS NOT NULL THEN
        INSERT INTO training_types (name, abbreviation, description, focus_area_id, sort_order, status)
        VALUES
            ('Gesundheitssport', 'GES', 'Gesundheitsorientiertes Training', fitness_id, 1, 'active'),
            ('Funktionelles Training', 'FUNKT', 'Funktionelle Fitness und Beweglichkeit', fitness_id, 2, 'active')
        ON CONFLICT (name) DO NOTHING;
    END IF;

    RAISE NOTICE 'Seeded focus-area-specific training types';
END $seed$;


-- ============================================================================
-- UPDATE UNIQUE CONSTRAINT (if needed)
-- ============================================================================

-- Drop old unique constraint if exists
DO $constraint$
BEGIN
    IF EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'training_types_name_key'
    ) THEN
        ALTER TABLE training_types DROP CONSTRAINT training_types_name_key;
        RAISE NOTICE 'Dropped old unique constraint on name';
    END IF;
END $constraint$;

-- Add new unique constraint (name per focus_area)
DO $constraint$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'training_types_name_focus_area_key'
    ) THEN
        ALTER TABLE training_types
        ADD CONSTRAINT training_types_name_focus_area_key
        UNIQUE (name, focus_area_id);

        RAISE NOTICE 'Added unique constraint: name per focus_area';
    END IF;
END $constraint$;


RAISE NOTICE 'Migration 013 completed successfully';

END $$;
