-- Migration 033: Nutrition Focus Areas
-- Date: 2026-03-28
-- Purpose: Add missing nutrition category to complete focus area coverage

-- ============================================================================
-- Part 1: Add Nutrition Focus Areas
-- ============================================================================

INSERT INTO focus_area_definitions (key, name_de, name_en, icon, category, description) VALUES
-- Nutrition Category
('protein_intake', 'Proteinzufuhr', 'Protein Intake', '🥩', 'nutrition', 'Ausreichend Protein für Muskelaufbau/-erhalt'),
('calorie_balance', 'Kalorienbilanz', 'Calorie Balance', '⚖️', 'nutrition', 'Energiebilanz passend zum Ziel (Defizit/Überschuss)'),
('macro_consistency', 'Makro-Konsistenz', 'Macro Consistency', '📊', 'nutrition', 'Gleichmäßige Makronährstoff-Verteilung'),
('meal_timing', 'Mahlzeiten-Timing', 'Meal Timing', '⏰', 'nutrition', 'Regelmäßige Mahlzeiten und optimales Timing'),
('hydration', 'Flüssigkeitszufuhr', 'Hydration', '💧', 'nutrition', 'Ausreichende Flüssigkeitsaufnahme')
ON CONFLICT (key) DO NOTHING;

-- ============================================================================
-- Part 2: Auto-Mapping for Nutrition-Related Goals
-- ============================================================================

-- Helper function to get focus_area_id by key
CREATE OR REPLACE FUNCTION get_focus_area_id(area_key VARCHAR)
RETURNS UUID AS $$
BEGIN
    RETURN (SELECT id FROM focus_area_definitions WHERE key = area_key LIMIT 1);
END;
$$ LANGUAGE plpgsql;

-- Weight Loss goals → calorie_balance (40%) + protein_intake (30%)
-- (Already mapped to weight_loss in migration 031, adding nutrition aspects)
INSERT INTO goal_focus_contributions (goal_id, focus_area_id, contribution_weight)
SELECT g.id, fa.id,
    CASE fa.key
        WHEN 'calorie_balance' THEN 40.00
        WHEN 'protein_intake' THEN 30.00
    END
FROM goals g
CROSS JOIN focus_area_definitions fa
WHERE g.goal_type = 'weight'
  AND fa.key IN ('calorie_balance', 'protein_intake')
ON CONFLICT (goal_id, focus_area_id) DO NOTHING;

-- Body Fat goals → calorie_balance (30%) + protein_intake (40%)
INSERT INTO goal_focus_contributions (goal_id, focus_area_id, contribution_weight)
SELECT g.id, fa.id,
    CASE fa.key
        WHEN 'calorie_balance' THEN 30.00
        WHEN 'protein_intake' THEN 40.00
    END
FROM goals g
CROSS JOIN focus_area_definitions fa
WHERE g.goal_type = 'body_fat'
  AND fa.key IN ('calorie_balance', 'protein_intake')
ON CONFLICT (goal_id, focus_area_id) DO NOTHING;

-- Lean Mass goals → protein_intake (60%) + calorie_balance (20%)
INSERT INTO goal_focus_contributions (goal_id, focus_area_id, contribution_weight)
SELECT g.id, fa.id,
    CASE fa.key
        WHEN 'protein_intake' THEN 60.00
        WHEN 'calorie_balance' THEN 20.00
    END
FROM goals g
CROSS JOIN focus_area_definitions fa
WHERE g.goal_type = 'lean_mass'
  AND fa.key IN ('protein_intake', 'calorie_balance')
ON CONFLICT (goal_id, focus_area_id) DO NOTHING;

-- Strength goals → protein_intake (20%)
INSERT INTO goal_focus_contributions (goal_id, focus_area_id, contribution_weight)
SELECT g.id, get_focus_area_id('protein_intake'), 20.00
FROM goals g
WHERE g.goal_type = 'strength'
ON CONFLICT (goal_id, focus_area_id) DO NOTHING;

-- Cleanup helper function
DROP FUNCTION IF EXISTS get_focus_area_id(VARCHAR);

-- ============================================================================
-- Summary
-- ============================================================================

COMMENT ON COLUMN focus_area_definitions.category IS
'Categories: body_composition, training, endurance, coordination, mental, recovery, health, nutrition';

-- Count nutrition focus areas
DO $$
DECLARE
    nutrition_count INT;
BEGIN
    SELECT COUNT(*) INTO nutrition_count
    FROM focus_area_definitions
    WHERE category = 'nutrition';

    RAISE NOTICE 'Migration 033 complete: % nutrition focus areas added', nutrition_count;
END $$;
