"""
VYNDR Context Adjustment Aggregator
Aggregates all contextual factors into a single context_adjustment value.
Used by both NBA and MLB grading pipelines.
"""

# All recognized context factor keys
CONTEXT_FACTORS = [
    'park_factor_adj',
    'weather_adj',
    'abs_adj',
    'home_road_adj',
    'day_night_adj',
    'lineup_protection_adj',
    'opponent_quality_adj',
    'teammate_impact_adj',
    'game_script_adj',
    'bullpen_state_adj',
    'tto_decay_adj',
    'catcher_framing_adj',
    'travel_fatigue_adj',
    'umpire_adj',
    'referee_adj',
]


def aggregate_context_adjustments(factors):
    """
    Aggregate all contextual factors into a single context_adjustment value.
    Each factor is a float adjustment to the player's projected stat.

    Args:
        factors: Dict mapping factor names to float adjustments.
            Missing factors default to 0.0.

    Returns:
        Float — total context adjustment (sum of all factors).
    """
    if not factors:
        return 0.0
    return sum(factors.get(k, 0.0) for k in CONTEXT_FACTORS)


def decompose_context(factors):
    """
    Return a breakdown of all non-zero context adjustments for grade response.

    Args:
        factors: Dict mapping factor names to float adjustments.

    Returns:
        Dict of non-zero factors with their values.
    """
    if not factors:
        return {}
    return {k: round(factors[k], 3) for k in CONTEXT_FACTORS
            if factors.get(k, 0.0) != 0.0}
