"""Pass, Fail or Reject decision of a v5 gate margin against a reject band."""

from raina_laya.features.grade_model.client import RejectBand
from raina_laya.features.serving.domain.contracts import Decision, Grade


def decide(
    margin: float,
    conditional: float,
    band: RejectBand | None,
) -> tuple[Decision, Grade | None]:
    """Decide one song from its mean gate margin and mean S-given-Pass logit.

    With a band, `margin >= hi` is Pass, `margin <= lo` is Fail and anything strictly
    between is rejected, as in the research evaluation. Without a band the boundary is
    zero and a tie stays Fail. A Pass is S when the conditional logit is above zero and
    A otherwise.
    """
    if band is None:
        passed = margin > 0.0
    elif margin >= band.hi:
        passed = True
    elif margin <= band.lo:
        passed = False
    else:
        return "reject", None
    if not passed:
        return "fail", "Fail"
    return "pass", "S" if conditional > 0.0 else "A"
