-- Migration 007: Activity Type Mappings (Learnable System)
-- Replaces hardcoded mappings with DB-based configurable system
-- Created: 2026-03-21

-- ========================================
-- 1. Create activity_type_mappings table
-- ========================================
CREATE TABLE IF NOT EXISTS activity_type_mappings (
  id SERIAL PRIMARY KEY,
  activity_type VARCHAR(100) NOT NULL,
  training_type_id INTEGER NOT NULL REFERENCES training_types(id) ON DELETE CASCADE,
  profile_id VARCHAR(36),  -- NULL = global mapping, otherwise user-specific
  source VARCHAR(20) DEFAULT 'manual',  -- 'manual', 'bulk', 'admin', 'default'
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT unique_activity_type_per_profile UNIQUE(activity_type, profile_id)
);

-- ========================================
-- 2. Create indexes
-- ========================================
CREATE INDEX IF NOT EXISTS idx_activity_type_mappings_type ON activity_type_mappings(activity_type);
CREATE INDEX IF NOT EXISTS idx_activity_type_mappings_profile ON activity_type_mappings(profile_id);

-- ========================================
-- 3. Seed default mappings (global)
-- ========================================
-- Note: These are the German Apple Health workout types
-- training_type_id references are based on existing training_types data

-- Helper function to get training_type_id by subcategory
DO $$
DECLARE
  v_running_id INTEGER;
  v_walk_id INTEGER;
  v_cycling_id INTEGER;
  v_swimming_id INTEGER;
  v_hypertrophy_id INTEGER;
  v_functional_id INTEGER;
  v_hiit_id INTEGER;
  v_yoga_id INTEGER;
  v_technique_id INTEGER;
  v_sparring_id INTEGER;
  v_rowing_id INTEGER;
  v_dance_id INTEGER;
  v_static_id INTEGER;
  v_regeneration_id INTEGER;
  v_meditation_id INTEGER;
  v_mindfulness_id INTEGER;
BEGIN
  -- Get training_type IDs
  SELECT id INTO v_running_id FROM training_types WHERE subcategory = 'running' LIMIT 1;
  SELECT id INTO v_walk_id FROM training_types WHERE subcategory = 'walk' LIMIT 1;
  SELECT id INTO v_cycling_id FROM training_types WHERE subcategory = 'cycling' LIMIT 1;
  SELECT id INTO v_swimming_id FROM training_types WHERE subcategory = 'swimming' LIMIT 1;
  SELECT id INTO v_hypertrophy_id FROM training_types WHERE subcategory = 'hypertrophy' LIMIT 1;
  SELECT id INTO v_functional_id FROM training_types WHERE subcategory = 'functional' LIMIT 1;
  SELECT id INTO v_hiit_id FROM training_types WHERE subcategory = 'hiit' LIMIT 1;
  SELECT id INTO v_yoga_id FROM training_types WHERE subcategory = 'yoga' LIMIT 1;
  SELECT id INTO v_technique_id FROM training_types WHERE subcategory = 'technique' LIMIT 1;
  SELECT id INTO v_sparring_id FROM training_types WHERE subcategory = 'sparring' LIMIT 1;
  SELECT id INTO v_rowing_id FROM training_types WHERE subcategory = 'rowing' LIMIT 1;
  SELECT id INTO v_dance_id FROM training_types WHERE subcategory = 'dance' LIMIT 1;
  SELECT id INTO v_static_id FROM training_types WHERE subcategory = 'static' LIMIT 1;
  SELECT id INTO v_regeneration_id FROM training_types WHERE subcategory = 'regeneration' LIMIT 1;
  SELECT id INTO v_meditation_id FROM training_types WHERE subcategory = 'meditation' LIMIT 1;
  SELECT id INTO v_mindfulness_id FROM training_types WHERE subcategory = 'mindfulness' LIMIT 1;

  -- Insert default mappings (German Apple Health names)
  INSERT INTO activity_type_mappings (activity_type, training_type_id, profile_id, source) VALUES
    -- German workout types
    ('Laufen', v_running_id, NULL, 'default'),
    ('Gehen', v_walk_id, NULL, 'default'),
    ('Wandern', v_walk_id, NULL, 'default'),
    ('Outdoor Spaziergang', v_walk_id, NULL, 'default'),
    ('Innenräume Spaziergang', v_walk_id, NULL, 'default'),
    ('Spaziergang', v_walk_id, NULL, 'default'),
    ('Radfahren', v_cycling_id, NULL, 'default'),
    ('Schwimmen', v_swimming_id, NULL, 'default'),
    ('Traditionelles Krafttraining', v_hypertrophy_id, NULL, 'default'),
    ('Funktionelles Krafttraining', v_functional_id, NULL, 'default'),
    ('Hochintensives Intervalltraining', v_hiit_id, NULL, 'default'),
    ('Yoga', v_yoga_id, NULL, 'default'),
    ('Kampfsport', v_technique_id, NULL, 'default'),
    ('Matrial Arts', v_technique_id, NULL, 'default'),  -- Common typo
    ('Boxen', v_sparring_id, NULL, 'default'),
    ('Rudern', v_rowing_id, NULL, 'default'),
    ('Tanzen', v_dance_id, NULL, 'default'),
    ('Cardio Dance', v_dance_id, NULL, 'default'),
    ('Flexibilität', v_static_id, NULL, 'default'),
    ('Abwärmen', v_regeneration_id, NULL, 'default'),
    ('Cooldown', v_regeneration_id, NULL, 'default'),
    ('Meditation', v_meditation_id, NULL, 'default'),
    ('Achtsamkeit', v_mindfulness_id, NULL, 'default'),
    ('Geist & Körper', v_yoga_id, NULL, 'default')
  ON CONFLICT (activity_type, profile_id) DO NOTHING;

  -- English workout types (for compatibility)
  INSERT INTO activity_type_mappings (activity_type, training_type_id, profile_id, source) VALUES
    ('Running', v_running_id, NULL, 'default'),
    ('Walking', v_walk_id, NULL, 'default'),
    ('Hiking', v_walk_id, NULL, 'default'),
    ('Cycling', v_cycling_id, NULL, 'default'),
    ('Swimming', v_swimming_id, NULL, 'default'),
    ('Traditional Strength Training', v_hypertrophy_id, NULL, 'default'),
    ('Functional Strength Training', v_functional_id, NULL, 'default'),
    ('High Intensity Interval Training', v_hiit_id, NULL, 'default'),
    ('Martial Arts', v_technique_id, NULL, 'default'),
    ('Boxing', v_sparring_id, NULL, 'default'),
    ('Rowing', v_rowing_id, NULL, 'default'),
    ('Dance', v_dance_id, NULL, 'default'),
    ('Core Training', v_functional_id, NULL, 'default'),
    ('Flexibility', v_static_id, NULL, 'default'),
    ('Mindfulness', v_mindfulness_id, NULL, 'default')
  ON CONFLICT (activity_type, profile_id) DO NOTHING;
END $$;

-- ========================================
-- 4. Add comment
-- ========================================
COMMENT ON TABLE activity_type_mappings IS 'v9d Phase 1b: Learnable activity type to training type mappings. Replaces hardcoded mappings.';
