"""
Anomaly configuration validation with fail-fast behavior.

Single-responsibility validator for period-anomaly type combinations
and parameter validation according to simplified configuration design.
"""

from typing import Dict, Any


def validate_anomaly_config(config: Dict[str, Any]) -> None:
    """
    Validate anomaly configuration, raise ValueError on invalid combinations.
    
    Args:
        config: Anomaly configuration dictionary
        
    Raises:
        ValueError: For invalid period-anomaly type combinations or missing parameters
    """
    if not config:
        return  # No anomaly configuration is valid
    
    # Required parameters
    anomaly_type = config.get("type")
    intensity = config.get("intensity")

    if not anomaly_type:
        raise ValueError("Anomaly configuration requires 'type' parameter")

    # Intensity is optional - anomaly functions have defaults
    # Generate stage may provide variance_config for auto-calculation
    # Telemetry stage uses built-in defaults in anomaly functions
    
    # Validate anomaly type is supported
    _validate_anomaly_type(anomaly_type)
    
    # Validate intensity ranges (only if provided)
    if intensity is not None:
        _validate_intensity(intensity, anomaly_type)
    
    # Validate no deprecated parameters are present
    _validate_no_deprecated_parameters(config)


def _validate_anomaly_type(anomaly_type: str) -> None:
    """Validate anomaly type is supported."""
    supported_anomaly_types = [
        "daily_spike", "daily_step_up", "daily_step_down",
        "hourly_spike", "hourly_step_up", "hourly_step_down"
    ]
    
    if anomaly_type not in supported_anomaly_types:
        raise ValueError(
            f"Unsupported anomaly type '{anomaly_type}'. "
            f"Supported types: {supported_anomaly_types}"
        )


def _validate_intensity(intensity: float, anomaly_type: str) -> None:
    """Validate intensity value is reasonable for anomaly type."""
    if not isinstance(intensity, (int, float)):
        raise ValueError(f"Intensity must be a number, got: {type(intensity)}")
    
    if intensity <= 0:
        raise ValueError("Intensity must be positive")
        
    # Step down anomalies should have intensity < 1.0 (reduction)
    if anomaly_type in ["daily_step_down", "hourly_step_down"]:
        if intensity >= 1.0:
            raise ValueError(
                f"Step down anomaly intensity should be < 1.0 (reduction), got: {intensity}"
            )
    
    # Very high intensities are likely configuration errors
    if intensity > 20.0:
        raise ValueError(f"Intensity {intensity} is unusually high (>20x), check configuration")


def _validate_no_deprecated_parameters(config: Dict[str, Any]) -> None:
    """Ensure no deprecated parameters are present."""
    deprecated_params = ["day", "hour", "duration_hours", "period"]
    found_deprecated = [param for param in deprecated_params if param in config]
    
    if found_deprecated:
        raise ValueError(
            f"Deprecated parameters {found_deprecated} found in anomaly configuration. "
            f"Period, day, hour, and duration are now auto-determined from anomaly type. Remove these parameters."
        )