"""120-day timely-filing gate for Colorado Medicaid.

Per HCPF's Colorado Medical Assistance Program provider manual, claims must
be filed within 120 days from the date of service. The gate is HARD by
default — past-window visits are surfaced as EXCLUDED_TIMELY_FILING in the
summary CSV and not emitted by Pipeline B.

The operator may pass override=True per batch to relax the gate (e.g. for
good-cause appeal visits). The decision is recorded in the summary CSV
either way so the audit trail shows the override.
"""
from dataclasses import dataclass
from datetime import date

FILING_WINDOW_DAYS = 120


@dataclass(frozen=True)
class TimelyFilingDecision:
    dos: date
    as_of: date
    days_old: int
    within_window: bool
    override: bool
    rebillable: bool


def timely_filing_decision(
    dos: date, as_of: date, override: bool
) -> TimelyFilingDecision:
    days_old = (as_of - dos).days
    within_window = days_old <= FILING_WINDOW_DAYS
    return TimelyFilingDecision(
        dos=dos, as_of=as_of, days_old=days_old,
        within_window=within_window, override=override,
        rebillable=within_window or override,
    )
