"""CARC-aware filter.

EXCLUDED = contractual / not recoverable / not a denial-of-payment in the
narrow sense (charge exceeds fee schedule, prior to coverage, etc.).
REVIEW = operator must decide (claim/service lacks info, non-covered
charges, duplicate, etc.). Anything else falls through to REBILL.
"""
from __future__ import annotations

from enum import Enum


class CarcDecision(str, Enum):
    EXCLUDED = "EXCLUDED"
    REVIEW = "REVIEW"
    REBILL = "REBILL"


# These CARCs are NOT recoverable denials. Do not rebill.
EXCLUDED_CARCS: frozenset[str] = frozenset({
    "CO-45",  # charge exceeds fee schedule / contractual obligation
    "CO-26",  # expenses incurred prior to coverage
    "CO-129",  # prior processing information; forward to next payer
})

# These CARCs need human review before rebill.
REVIEW_CARCS: frozenset[str] = frozenset({
    "PI-16",   # claim/service lacks information
    "PI-96",   # non-covered charges
    "PI-15",   # authorization / certification absent
    "PI-4",    # procedure not paid separately
    "PI-110",  # billing date predates service date
    "OA-18",   # exact duplicate (resubmit noise)
    "OA-23",   # impact of prior payer adjudication
})


def decide_carc(reasons: tuple[str, ...] | list[str]) -> CarcDecision:
    """Pick the strongest action: EXCLUDED > REVIEW > REBILL."""
    if any(r in EXCLUDED_CARCS for r in reasons):
        return CarcDecision.EXCLUDED
    if any(r in REVIEW_CARCS for r in reasons):
        return CarcDecision.REVIEW
    return CarcDecision.REBILL
