"""
Registry for built-in algorithm ids → callables.

Only code registers algorithms; templates reference ids declared here.
"""

from __future__ import annotations

from typing import Any, Callable, Dict, Mapping

from data_layer.training_profile.algorithms.builtin.linear_range import (
    ALGORITHM_ID as LINEAR_RANGE_ID,
    linear_range_score,
)
from data_layer.training_profile.algorithms.builtin.threshold_band import (
    ALGORITHM_ID as THRESHOLD_BAND_ID,
    threshold_band_score,
)
from data_layer.training_profile.models import AlgorithmRunResult

AlgorithmFn = Callable[..., AlgorithmRunResult]

_REGISTRY: Dict[str, AlgorithmFn] = {}


def register_algorithm(algorithm_id: str, fn: AlgorithmFn) -> None:
    if algorithm_id in _REGISTRY:
        raise ValueError(f"Algorithm already registered: {algorithm_id}")
    _REGISTRY[algorithm_id] = fn


def get_algorithm(algorithm_id: str) -> AlgorithmFn:
    if algorithm_id not in _REGISTRY:
        raise KeyError(f"Unknown algorithm_id: {algorithm_id}")
    return _REGISTRY[algorithm_id]


def list_algorithm_ids() -> tuple[str, ...]:
    return tuple(sorted(_REGISTRY.keys()))


def _register_defaults() -> None:
    register_algorithm(THRESHOLD_BAND_ID, threshold_band_score)
    register_algorithm(LINEAR_RANGE_ID, linear_range_score)


_register_defaults()
