"""
Period-to-timing mapping utilities for anomaly configuration.

Single-responsibility utility for auto-calculating day/hour parameters
based on CloudZero viewing periods and anomaly types.
"""

from typing import Tuple


def calculate_anomaly_timing(anomaly_type: str) -> Tuple[int, int, str]:
    """
    Calculate day offset, hour offset, and period for anomaly based on type.
    
    Auto-determines optimal period based on anomaly pattern:
    - Spikes: "Last 30 Days" for recurring monthly patterns
    - Step changes: "Last 90 Days" for cumulative quarterly patterns
    
    Args:
        anomaly_type: Type of anomaly (e.g., "daily_spike", "hourly_step_up")
        
    Returns:
        Tuple of (day_offset, hour_offset, period) 
        - day_offset: 0-indexed day within period
        - hour_offset: Hour within the day
        - period: Auto-determined period specification
        
    Raises:
        ValueError: For unsupported anomaly type
    """
    # Auto-determine period based on anomaly pattern
    if anomaly_type in ["daily_spike", "hourly_spike"]:
        # Spikes: recurring monthly patterns for consistent visualization
        period = "Last 30 Days"
        median_day = 15  # Day 16 (0-indexed) - shifted forward one day
    elif anomaly_type in ["daily_step_up", "daily_step_down", "hourly_step_up", "hourly_step_down"]:
        # Step changes: monthly patterns for frequent compound effects
        period = "Last 30 Days"
        median_day = 15  # Day 16 (0-indexed) - shifted forward one day
    else:
        supported_types = ["daily_spike", "hourly_spike", "daily_step_up", "daily_step_down", "hourly_step_up", "hourly_step_down"]
        raise ValueError(f"Unsupported anomaly type '{anomaly_type}'. Supported types: {supported_types}")
    
    # Calculate appropriate hour based on anomaly type
    if anomaly_type.startswith("hourly"):
        hour = _get_default_hour(anomaly_type)
    else:
        hour = 0  # Daily anomalies use beginning of day
    
    return median_day, hour, period


def _get_default_hour(anomaly_type: str) -> int:
    """Get appropriate hour for hourly anomaly types."""
    hour_mapping = {
        "hourly_spike": 14,      # 2 PM - peak traffic time
        "hourly_step_up": 9,     # 9 AM - business hours start
        "hourly_step_down": 10,  # 10 AM - maintenance window
    }
    
    return hour_mapping.get(anomaly_type, 12)  # Default to noon