"""Criteria computation callbacks.

Handles:
- Compute All button -> runs auto_compute_criteria
- Protocol scoring
- Results display
"""

from __future__ import annotations

from typing import Any

import dash
from dash import Input, Output, State, html
from dash.exceptions import PreventUpdate

from impakt.web.components.criteria import (
    auto_compute_criteria,
    build_criteria_results_display,
    score_protocol,
)
from impakt.web.state import AppState


def register_criteria_callbacks(app: dash.Dash, app_state: AppState) -> None:
    """Register criteria-related callbacks."""

    @app.callback(
        Output("criteria-results", "children"),
        [Input("compute-criteria-btn", "n_clicks")],
        [State("protocol-select", "value")],
    )
    def compute_criteria(n_clicks: int | None, protocol: str) -> Any:
        if not n_clicks:
            return html.Div(
                "Click 'Compute All' to auto-detect channels and calculate injury criteria.",
                className="text-muted small",
            )

        primary = app_state.primary_test
        if primary is None:
            return html.Div("No test loaded.", className="text-danger small")

        # Compute criteria
        criteria = auto_compute_criteria(primary.data)

        if not criteria:
            return html.Div(
                "No criteria could be computed. Check that the test has the required channels "
                "(head acceleration, neck force/moment, chest deflection, femur load).",
                className="text-warning small",
            )

        # Score against protocol
        protocol_result = score_protocol(criteria, protocol)

        return build_criteria_results_display(criteria, protocol_result)
