-- Migration 007: Exercise Catalogs (Admin-verwaltbare Stammdaten)
-- Erstellt: 2026-04-22
-- Beschreibung: Dynamische Kataloge statt Hard-Coding

-- Fokusbereiche (statt hard-coded)
CREATE TABLE focus_areas (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    abbreviation VARCHAR(20),
    description TEXT,
    color VARCHAR(20),  -- Für UI (z.B. #1D9E75)
    icon VARCHAR(50),   -- Emoji oder Icon-Name
    sort_order INT,
    status VARCHAR(50) DEFAULT 'active',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_focus_areas_status ON focus_areas(status);

-- Trainingsstile (NEU: Shotokan, Goju-Ryu, etc.)
CREATE TABLE training_styles (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    abbreviation VARCHAR(20),
    description TEXT,
    parent_style_id INT REFERENCES training_styles(id),  -- z.B. Shotokan → Karate
    sort_order INT,
    status VARCHAR(50) DEFAULT 'active',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_training_styles_status ON training_styles(status);
CREATE INDEX idx_training_styles_parent ON training_styles(parent_style_id);

-- Trainingscharaktere (statt hard-coded)
CREATE TABLE training_characters (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    sort_order INT,
    status VARCHAR(50) DEFAULT 'active',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_training_characters_status ON training_characters(status);

-- Fähigkeitsbereiche (Kategorien für Skills)
CREATE TABLE skill_categories (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    parent_category_id INT REFERENCES skill_categories(id),  -- Hierarchie möglich
    sort_order INT,
    status VARCHAR(50) DEFAULT 'active',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_skill_categories_status ON skill_categories(status);
CREATE INDEX idx_skill_categories_parent ON skill_categories(parent_category_id);

-- Skills erweitern (Verknüpfung mit Kategorien)
ALTER TABLE skills
ADD COLUMN category_id INT REFERENCES skill_categories(id),
ADD COLUMN parent_skill_id INT REFERENCES skills(id),  -- Hierarchie: Tsuki → Mae-Zuki
ADD COLUMN level INT CHECK (level BETWEEN 1 AND 10),  -- Schwierigkeitsgrad
ADD COLUMN sort_order INT;

CREATE INDEX idx_skills_category ON skills(category_id);
CREATE INDEX idx_skills_parent ON skills(parent_skill_id);

-- Exercises erweitern
ALTER TABLE exercises
ADD COLUMN training_style_id INT REFERENCES training_styles(id),
ADD COLUMN training_character_id INT REFERENCES training_characters(id),
ADD COLUMN focus_area_id INT REFERENCES focus_areas(id);

-- Alte text-basierte Spalten können später deprecated werden
-- ALTER TABLE exercises DROP COLUMN focus_area;  -- später, nach Migration
-- ALTER TABLE exercises DROP COLUMN training_character;  -- später, nach Migration

CREATE INDEX idx_exercises_style ON exercises(training_style_id);
CREATE INDEX idx_exercises_character ON exercises(training_character_id);
CREATE INDEX idx_exercises_focus_area ON exercises(focus_area_id);

-- Trainer-Fokusbereich-Zuordnung (Welcher Trainer arbeitet in welchen Fokusbereichen?)
CREATE TABLE trainer_focus_areas (
    id SERIAL PRIMARY KEY,
    profile_id INT REFERENCES profiles(id) ON DELETE CASCADE,
    focus_area_id INT REFERENCES focus_areas(id) ON DELETE CASCADE,
    is_primary BOOLEAN DEFAULT false,  -- Primärer Fokusbereich des Trainers
    created_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(profile_id, focus_area_id)
);

CREATE INDEX idx_trainer_focus_areas_profile ON trainer_focus_areas(profile_id);
CREATE INDEX idx_trainer_focus_areas_focus ON trainer_focus_areas(focus_area_id);

-- Basis-Daten einfügen
INSERT INTO focus_areas (name, abbreviation, description, color, icon, sort_order) VALUES
('Karate', 'KAR', 'Traditionelles Karate', '#1D9E75', '🥋', 1),
('Selbstverteidigung', 'SV', 'Praktische Selbstverteidigung', '#D85A30', '🛡️', 2),
('Gewaltschutz', 'GS', 'Gewaltprävention und Deeskalation', '#EF9F27', '🤝', 3);

INSERT INTO training_styles (name, abbreviation, description, sort_order) VALUES
('Shotokan', 'SHO', 'Shotokan-Karate', 1),
('Goju-Ryu', 'GJR', 'Goju-Ryu-Karate', 2),
('Wado-Ryu', 'WAD', 'Wado-Ryu-Karate', 3),
('Shito-Ryu', 'SHI', 'Shito-Ryu-Karate', 4),
('Kyokushin', 'KYO', 'Kyokushin-Karate', 5);

INSERT INTO training_characters (name, description, sort_order) VALUES
('Grundlage', 'Einführung und Basisvermittlung', 1),
('Aufbau', 'Aufbauendes Training', 2),
('Vertiefung', 'Vertiefung und Spezialisierung', 3),
('Festigung', 'Wiederholung und Festigung', 4),
('Diagnose', 'Leistungsdiagnose und Test', 5),
('Wettkampf', 'Wettkampfvorbereitung', 6);

INSERT INTO skill_categories (name, description, sort_order) VALUES
('Kihon', 'Grundschultechniken', 1),
('Kumite', 'Kampftechniken', 2),
('Kata', 'Formen', 3),
('Selbstverteidigung', 'SV-Techniken', 4),
('Fitness', 'Kondition und Athletik', 5),
('Mental', 'Mentale Fähigkeiten', 6);
