"""
Training Types API - v9d

Provides hierarchical list of training categories and subcategories
for activity classification.
"""
from fastapi import APIRouter, Depends
from db import get_db, get_cursor
from auth import require_auth

router = APIRouter(prefix="/api/training-types", tags=["training-types"])


@router.get("")
def list_training_types(session: dict = Depends(require_auth)):
    """
    Get all training types, grouped by category.

    Returns hierarchical structure:
    {
      "cardio": [
        {"id": 1, "subcategory": "running", "name_de": "Laufen", ...},
        ...
      ],
      "strength": [...],
      ...
    }
    """
    with get_db() as conn:
        cur = get_cursor(conn)
        cur.execute("""
            SELECT id, category, subcategory, name_de, name_en, icon, sort_order
            FROM training_types
            ORDER BY sort_order, category, subcategory
        """)
        rows = cur.fetchall()

    # Group by category
    grouped = {}
    for row in rows:
        cat = row['category']
        if cat not in grouped:
            grouped[cat] = []
        grouped[cat].append({
            'id': row['id'],
            'category': row['category'],
            'subcategory': row['subcategory'],
            'name_de': row['name_de'],
            'name_en': row['name_en'],
            'icon': row['icon'],
            'sort_order': row['sort_order']
        })

    return grouped


@router.get("/flat")
def list_training_types_flat(session: dict = Depends(require_auth)):
    """
    Get all training types as flat list (for simple dropdown).
    """
    with get_db() as conn:
        cur = get_cursor(conn)
        cur.execute("""
            SELECT id, category, subcategory, name_de, name_en, icon
            FROM training_types
            ORDER BY sort_order
        """)
        rows = cur.fetchall()

    return [dict(row) for row in rows]


@router.get("/categories")
def list_categories(session: dict = Depends(require_auth)):
    """
    Get list of unique categories with metadata.
    """
    categories = {
        'cardio': {
            'name_de': 'Cardio (Ausdauer)',
            'name_en': 'Cardio (Endurance)',
            'icon': '❤️',
            'color': '#EF4444'
        },
        'strength': {
            'name_de': 'Kraft',
            'name_en': 'Strength',
            'icon': '💪',
            'color': '#3B82F6'
        },
        'hiit': {
            'name_de': 'Schnellkraft / HIIT',
            'name_en': 'Power / HIIT',
            'icon': '🔥',
            'color': '#F59E0B'
        },
        'martial_arts': {
            'name_de': 'Kampfsport',
            'name_en': 'Martial Arts',
            'icon': '🥋',
            'color': '#8B5CF6'
        },
        'mobility': {
            'name_de': 'Mobility & Dehnung',
            'name_en': 'Mobility & Stretching',
            'icon': '🧘',
            'color': '#10B981'
        },
        'recovery': {
            'name_de': 'Erholung (aktiv)',
            'name_en': 'Recovery (active)',
            'icon': '💆',
            'color': '#6B7280'
        },
        'mind': {
            'name_de': 'Geist & Meditation',
            'name_en': 'Mind & Meditation',
            'icon': '🧘‍♂️',
            'color': '#A78BFA'
        },
        'other': {
            'name_de': 'Sonstiges',
            'name_en': 'Other',
            'icon': '📝',
            'color': '#9CA3AF'
        }
    }
    return categories
