-- Migration 004: Training Types & Categories
-- Part of v9d: Schlaf + Sport-Vertiefung
-- Created: 2026-03-21

-- ========================================
-- 1. Create training_types table
-- ========================================
CREATE TABLE IF NOT EXISTS training_types (
  id                SERIAL PRIMARY KEY,
  category          VARCHAR(50) NOT NULL,     -- Main category: 'cardio', 'strength', 'hiit', etc.
  subcategory       VARCHAR(50),              -- Optional: 'running', 'hypertrophy', etc.
  name_de           VARCHAR(100) NOT NULL,    -- German display name
  name_en           VARCHAR(100) NOT NULL,    -- English display name
  icon              VARCHAR(10),              -- Emoji icon
  sort_order        INTEGER DEFAULT 0,        -- For UI ordering
  created_at        TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- ========================================
-- 2. Add training type columns to activity_log
-- ========================================
ALTER TABLE activity_log
  ADD COLUMN IF NOT EXISTS training_type_id INTEGER REFERENCES training_types(id),
  ADD COLUMN IF NOT EXISTS training_category VARCHAR(50),     -- Denormalized for fast queries
  ADD COLUMN IF NOT EXISTS training_subcategory VARCHAR(50);  -- Denormalized

-- ========================================
-- 3. Create indexes
-- ========================================
CREATE INDEX IF NOT EXISTS idx_activity_training_type ON activity_log(training_type_id);
CREATE INDEX IF NOT EXISTS idx_activity_training_category ON activity_log(training_category);
CREATE INDEX IF NOT EXISTS idx_training_types_category ON training_types(category);

-- ========================================
-- 4. Seed training types data
-- ========================================

-- Cardio (Ausdauer)
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('cardio', 'running',     'Laufen',           'Running',          '🏃', 100),
  ('cardio', 'cycling',     'Radfahren',        'Cycling',          '🚴', 101),
  ('cardio', 'swimming',    'Schwimmen',        'Swimming',         '🏊', 102),
  ('cardio', 'rowing',      'Rudern',           'Rowing',           '🚣', 103),
  ('cardio', 'other',       'Sonstiges Cardio', 'Other Cardio',     '❤️', 104);

-- Kraft
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('strength', 'hypertrophy',      'Hypertrophie',           'Hypertrophy',          '💪', 200),
  ('strength', 'maxstrength',      'Maximalkraft',           'Max Strength',         '🏋️', 201),
  ('strength', 'endurance',        'Kraftausdauer',          'Strength Endurance',   '🔁', 202),
  ('strength', 'functional',       'Funktionell',            'Functional',           '⚡', 203);

-- Schnellkraft / HIIT
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('hiit', 'hiit',          'HIIT',                   'HIIT',                 '🔥', 300),
  ('hiit', 'explosive',     'Explosiv',               'Explosive',            '💥', 301),
  ('hiit', 'circuit',       'Circuit Training',       'Circuit Training',     '🔄', 302);

-- Kampfsport / Technikkraft
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('martial_arts', 'technique',     'Techniktraining',        'Technique Training',   '🥋', 400),
  ('martial_arts', 'sparring',      'Sparring / Wettkampf',   'Sparring / Competition', '🥊', 401),
  ('martial_arts', 'strength',      'Kraft für Kampfsport',   'Martial Arts Strength', '⚔️', 402);

-- Mobility & Dehnung
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('mobility', 'static',    'Statisches Dehnen',      'Static Stretching',    '🧘', 500),
  ('mobility', 'dynamic',   'Dynamisches Dehnen',     'Dynamic Stretching',   '🤸', 501),
  ('mobility', 'yoga',      'Yoga',                   'Yoga',                 '🕉️', 502),
  ('mobility', 'fascia',    'Faszienarbeit',          'Fascia Work',          '🎯', 503);

-- Erholung (aktiv)
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('recovery', 'walk',              'Spaziergang',            'Walk',                 '🚶', 600),
  ('recovery', 'swim_light',        'Leichtes Schwimmen',     'Light Swimming',       '🏊', 601),
  ('recovery', 'regeneration',      'Regenerationseinheit',   'Regeneration',         '💆', 602);

-- General / Uncategorized
INSERT INTO training_types (category, subcategory, name_de, name_en, icon, sort_order) VALUES
  ('other', NULL, 'Sonstiges', 'Other', '📝', 900);

-- ========================================
-- 5. Add comment
-- ========================================
COMMENT ON TABLE training_types IS 'v9d: Training type categories and subcategories';
COMMENT ON TABLE activity_log IS 'Extended in v9d with training_type_id for categorization';
