-- Migration 025: Cleanup goal_type_definitions
-- Date: 2026-03-27
-- Purpose: Remove problematic FK columns and ensure seed data

-- Remove created_by/updated_by columns if they exist
-- (May have been created by failed Migration 024)
ALTER TABLE goal_type_definitions DROP COLUMN IF EXISTS created_by;
ALTER TABLE goal_type_definitions DROP COLUMN IF EXISTS updated_by;

-- Re-insert seed data (ON CONFLICT ensures idempotency)
-- This fixes cases where Migration 024 created table but failed to seed

-- 1. Weight
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    source_table, source_column, aggregation_method,
    description, is_system
) VALUES (
    'weight', 'Gewicht', 'Weight', 'kg', '⚖️', 'body',
    'weight_log', 'weight', 'latest',
    'Aktuelles Körpergewicht', true
)
ON CONFLICT (type_key) DO NOTHING;

-- 2. Body Fat
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    source_table, source_column, aggregation_method,
    description, is_system
) VALUES (
    'body_fat', 'Körperfett', 'Body Fat', '%', '📊', 'body',
    'caliper_log', 'body_fat_pct', 'latest',
    'Körperfettanteil aus Caliper-Messung', true
)
ON CONFLICT (type_key) DO NOTHING;

-- 3. Lean Mass
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    calculation_formula,
    description, is_system
) VALUES (
    'lean_mass', 'Muskelmasse', 'Lean Mass', 'kg', '💪', 'body',
    '{"type": "lean_mass", "dependencies": ["weight_log.weight", "caliper_log.body_fat_pct"], "formula": "weight - (weight * body_fat_pct / 100)"}',
    'Fettfreie Körpermasse (berechnet aus Gewicht und Körperfett)', true
)
ON CONFLICT (type_key) DO NOTHING;

-- 4. VO2 Max
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    source_table, source_column, aggregation_method,
    description, is_system
) VALUES (
    'vo2max', 'VO2Max', 'VO2Max', 'ml/kg/min', '🫁', 'recovery',
    'vitals_baseline', 'vo2_max', 'latest',
    'Maximale Sauerstoffaufnahme (geschätzt oder gemessen)', true
)
ON CONFLICT (type_key) DO NOTHING;

-- 5. Resting Heart Rate
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    source_table, source_column, aggregation_method,
    description, is_system
) VALUES (
    'rhr', 'Ruhepuls', 'Resting Heart Rate', 'bpm', '💓', 'recovery',
    'vitals_baseline', 'resting_hr', 'latest',
    'Ruhepuls morgens vor dem Aufstehen', true
)
ON CONFLICT (type_key) DO NOTHING;

-- 6. Blood Pressure
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    source_table, source_column, aggregation_method,
    description, is_system
) VALUES (
    'bp', 'Blutdruck', 'Blood Pressure', 'mmHg', '❤️', 'recovery',
    'blood_pressure_log', 'systolic', 'latest',
    'Blutdruck (aktuell nur systolisch, v2.0: beide Werte)', true
)
ON CONFLICT (type_key) DO NOTHING;

-- 7. Strength (inactive placeholder)
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    description, is_system, is_active
) VALUES (
    'strength', 'Kraft', 'Strength', 'kg', '🏋️', 'activity',
    'Maximalkraft (Platzhalter, Datenquelle in v2.0)', true, false
)
ON CONFLICT (type_key) DO NOTHING;

-- 8. Flexibility (inactive placeholder)
INSERT INTO goal_type_definitions (
    type_key, label_de, label_en, unit, icon, category,
    description, is_system, is_active
) VALUES (
    'flexibility', 'Beweglichkeit', 'Flexibility', 'cm', '🤸', 'activity',
    'Beweglichkeit (Platzhalter, Datenquelle in v2.0)', true, false
)
ON CONFLICT (type_key) DO NOTHING;
