"""Offline triage policy. Does not call a model, authorize, or execute an action."""

import math
from dataclasses import dataclass


@dataclass(frozen=True)
class Policy:
    revision: str
    threshold: float
    evaluated: bool

    def __post_init__(self) -> None:
        if not self.revision.strip():
            raise ValueError("policy revision is required")
        if not valid_probability(self.threshold):
            raise ValueError("threshold must be finite and between zero and one")
        if not isinstance(self.evaluated, bool):
            raise ValueError("evaluated must be a boolean")


def valid_probability(value: object) -> bool:
    return (
        isinstance(value, (int, float))
        and not isinstance(value, bool)
        and math.isfinite(value)
        and 0 <= value <= 1
    )


def triage(probability: object, *, eligible: bool, policy: Policy) -> tuple[str, str]:
    """Route a record; eligibility must come from trusted application logic.

    `evaluated` means the caller has evaluated this policy for this task. It is
    bookkeeping, not a mathematical guarantee or an authorization mechanism.
    """
    if eligible is not True:
        return "review", "ineligible"
    if not policy.evaluated:
        return "review", "policy_not_evaluated"
    if not valid_probability(probability):
        return "review", "invalid_or_missing_probability"
    if probability < policy.threshold:
        return "review", "below_threshold"
    return "route", "threshold_met"
