"""
Example calculation templates — declarative only; algorithms are referenced by id.

These are scaffolding examples, not production coaching rules.
"""

from __future__ import annotations

from typing import Dict

from data_layer.training_profile.models import CalculationTemplate, DimensionSpec, FocusAreaMapping

_TEMPLATES: Dict[str, CalculationTemplate] = {}


def _register(t: CalculationTemplate) -> None:
    if t.id in _TEMPLATES:
        raise ValueError(f"Duplicate template id: {t.id}")
    _TEMPLATES[t.id] = t


def get_calculation_template(template_id: str) -> CalculationTemplate:
    if template_id not in _TEMPLATES:
        raise KeyError(f"Unknown calculation template: {template_id}")
    return _TEMPLATES[template_id]


def list_calculation_template_ids() -> tuple[str, ...]:
    return tuple(sorted(_TEMPLATES.keys()))


# --- Example templates (illustrative) ---

_example_aerobic = CalculationTemplate(
    id="scaffold_example_aerobic_v1",
    version="1",
    label="Scaffold: aerobic-style intensity + volume (example)",
    dimensions=(
        DimensionSpec(
            key="intensity",
            algorithm_id="threshold_band",
            inputs=("avg_hr", "duration_min"),
            params={
                "value_key": "avg_hr",
                "bands": [
                    {"max": 120, "score": 0.25},
                    {"max": 140, "score": 0.55},
                    {"max": 160, "score": 0.85},
                    {"max": None, "score": 1.0},
                ],
            },
            maps_to=(
                FocusAreaMapping("aerobic_endurance", 0.7),
                FocusAreaMapping("cardiovascular_health", 0.3),
            ),
        ),
        DimensionSpec(
            key="volume",
            algorithm_id="linear_range",
            inputs=("duration_min", "distance_km"),
            params={
                "value_key": "duration_min",
                "min_value": 10.0,
                "max_value": 90.0,
                "invert": False,
            },
            maps_to=(FocusAreaMapping("aerobic_endurance", 0.8),),
        ),
    ),
)

_example_strength = CalculationTemplate(
    id="scaffold_example_strength_v1",
    version="1",
    label="Scaffold: strength session load (example)",
    dimensions=(
        DimensionSpec(
            key="effort",
            algorithm_id="linear_range",
            inputs=("duration_min",),
            params={
                "value_key": "duration_min",
                "min_value": 20.0,
                "max_value": 75.0,
                "invert": False,
            },
            maps_to=(
                FocusAreaMapping("strength", 0.9),
                FocusAreaMapping("strength_endurance", 0.1),
            ),
        ),
    ),
)

_register(_example_aerobic)
_register(_example_strength)
