"""Registry: Korrelations- und Treiber-Metriken (Data Layer correlations)."""

from placeholder_registry import (
    PlaceholderMetadata,
    MissingValuePolicy,
    OutputType,
    PlaceholderType,
    register_placeholder,
)
from ._evidence import tag_standard_evidence

CAT = "Korrelationen"
MVP = lambda reason, disp: MissingValuePolicy(
    available=False, value_raw=None, missing_reason=reason, legacy_display=disp
)


def register_korrelationen():
    for key, dl_fn, desc, tables, sem in [
        (
            "correlation_energy_weight_lag",
            "calculate_lag_correlation",
            "JSON: Lag-Korrelation Energiebilanz ↔ Gewicht",
            ["nutrition_log", "weight_log"],
            "correlations.calculate_lag_correlation(pid, 'energy', 'weight')",
        ),
        (
            "correlation_protein_lbm",
            "calculate_lag_correlation",
            "JSON: Lag-Korrelation Protein ↔ Magermasse",
            ["nutrition_log", "weight_log", "caliper_log"],
            "correlations.calculate_lag_correlation(pid, 'protein', 'lbm')",
        ),
        (
            "correlation_load_hrv",
            "calculate_lag_correlation",
            "JSON: Lag-Korrelation Trainingslast ↔ HRV",
            ["activity_log", "vitals_baseline"],
            "correlations.calculate_lag_correlation(pid, 'training_load', 'hrv')",
        ),
        (
            "correlation_load_rhr",
            "calculate_lag_correlation",
            "JSON: Lag-Korrelation Trainingslast ↔ Ruhepuls",
            ["activity_log", "vitals_baseline"],
            "correlations.calculate_lag_correlation(pid, 'training_load', 'rhr')",
        ),
        (
            "plateau_detected",
            "calculate_plateau_detected",
            "JSON: Platten-Erkennung (Gewicht/Körper)",
            ["weight_log", "caliper_log"],
            "correlations.calculate_plateau_detected",
        ),
        (
            "top_drivers",
            "calculate_top_drivers",
            "JSON: Top Treiber für Ziel-/Score-Variablen",
            ["weight_log", "nutrition_log", "activity_log", "vitals_baseline", "sleep_log"],
            "correlations.calculate_top_drivers",
        ),
    ]:
        m = PlaceholderMetadata(
            key=key,
            category=CAT,
            description=desc,
            resolver_module="backend/placeholder_resolver.py",
            resolver_function="_safe_json",
            data_layer_module="backend/data_layer/correlations.py",
            data_layer_function=dl_fn,
            source_tables=tables,
            semantic_contract=sem,
            business_meaning="Strukturierte Korrelationsausgabe für KI",
            unit="JSON",
            time_window="funktionsintern",
            output_type=OutputType.JSON,
            placeholder_type=PlaceholderType.RAW_DATA,
            format_hint="JSON-String",
            example_output="{}",
            minimum_data_requirements="Ausreichend gekoppelte Zeitreihen",
            quality_filter_policy=None,
            confidence_logic="Wie correlations.*",
            missing_value_policy=MVP("insufficient_data", "{}"),
            known_limitations="Bei wenigen Daten leer oder wenig robust",
            layer_1_decision=f"correlations.{dl_fn}",
            layer_2a_decision="_safe_json",
            layer_2b_reuse_possible=True,
            architecture_alignment="Phase 0c",
            issue_53_alignment="Layer 1",
            evidence={},
        )
        tag_standard_evidence(m)
        register_placeholder(m)


register_korrelationen()
