"""
Late-chase entry freshness gate.

F15 porting: Determines whether an entry is blocked due to late-chase risk.
ENTRY_FRESHNESS_GATE_V1 context: if late-chase is detected, sets freshnessState to
'BLOCK_LATE_CHASE' and prevents entry execution.

Ported from: src/gas_adapter_parts/gdf_04_execution_quality.gs:482
Parity reference: tests/parity/test_late_chase_gate_parity_v1.py
"""


def is_late_chase_blocked(breakout_quality_gate: str, late_chase_risk_score) -> bool:
    """
    Check if late-chase is blocked based on quality gate or risk threshold.

    GAS: bqRow.breakout_quality_gate === 'BLOCKED_LATE_CHASE' || alphaRow["late_chase_risk_score"] >= 70

    Args:
        breakout_quality_gate: The breakout quality gate state (string, e.g., 'BLOCKED_LATE_CHASE')
        late_chase_risk_score: Numeric risk score (int or float); can be None/NaN

    Returns:
        True if late-chase is blocked; False otherwise
    """
    # First condition: explicit gate block
    if breakout_quality_gate == 'BLOCKED_LATE_CHASE':
        return True

    # Second condition: risk score threshold
    if isinstance(late_chase_risk_score, (int, float)):
        # Handle NaN: float('nan') >= 70 returns False, which is correct (NaN blocks nothing)
        if late_chase_risk_score >= 70:
            return True

    return False
