"""
Pipeline Profile Converters

Convert unified profile patterns to pipeline configuration formats.
"""

from typing import Any, Dict

from ..patterns.anomaly import get_anomaly_pattern
from ..patterns.distribution import get_distribution_pattern
from ..patterns.growth import get_growth_pattern
from ..patterns.seasonality import get_seasonality_pattern


def convert_growth_profile_for_pipeline(profile_name: str) -> Dict[str, Any]:
    """Convert unified growth profile to pipeline variance parameters."""
    pattern = get_growth_pattern(profile_name)

    return {
        "description": pattern.description,
        "category": pattern.category,
        "pattern_type": pattern.growth_type.value,
        "growth_rate": pattern.growth_rate,
        "base_variance": pattern.base_variance,
        "duration": pattern.duration,
        "realistic_for": pattern.realistic_for,
    }


def convert_seasonality_profile_for_pipeline(profile_name: str) -> Dict[str, Any]:
    """Convert unified seasonality profile to pipeline timing parameters with multi-granularity variance."""
    pattern = get_seasonality_pattern(profile_name)

    return {
        "description": pattern.description,
        "category": pattern.category,
        "pattern_type": pattern.seasonality_type.value,
        "amplitude": pattern.amplitude,
        "phase": pattern.phase,
        "realistic_for": pattern.realistic_for,
        # Multi-granularity variance configuration
        "variance_config": {
            "hourly_variance": pattern.variance_config.hourly_variance,
            "daily_variance": pattern.variance_config.daily_variance,
            "weekly_variance": pattern.variance_config.weekly_variance,
            "monthly_variance": pattern.variance_config.monthly_variance,
            "phase_jitter": pattern.variance_config.phase_jitter,
        },
    }


def convert_anomaly_profile_for_pipeline(profile_name: str) -> Dict[str, Any]:
    """Convert unified anomaly profile to pipeline anomaly parameters."""
    pattern = get_anomaly_pattern(profile_name)

    # Map timing from anomaly pattern to pipeline format
    timing_map = {
        "random": "random",
        "business_hours": "business_hours",
        "monthly_end": "monthly_end",
        "quarterly": "quarterly",
        "ninety_day_cycle": "ninety_day_cycle",
    }

    return {
        "description": pattern.description,
        "category": pattern.category,
        "anomaly_type": pattern.anomaly_type.value,
        "frequency": pattern.frequency,
        "magnitude": pattern.magnitude,
        "duration": pattern.duration,
        "timing": timing_map.get(pattern.timing, "random"),
        "interval_days": pattern.interval_days,  # 90-day interval support
        "realistic_for": pattern.realistic_for,
    }


def convert_distribution_profile_for_pipeline(profile_name: str) -> Dict[str, Any]:
    """Convert unified distribution profile to pipeline allocation parameters."""
    pattern = get_distribution_pattern(profile_name)

    return {
        "description": pattern.description,
        "category": pattern.category,
        "strategy": pattern.strategy.value,
        "skew_factor": pattern.skew_factor,
        "realistic_for": pattern.realistic_for,
    }


def get_variance_profile_for_pipeline(profile_name: str) -> Dict[str, Any]:
    """
    Get variance profile in pipeline format.

    This maintains compatibility with existing pipeline code that expects
    variance profiles with specific keys and structure.
    """
    try:
        # Try growth pattern first
        growth_config = convert_growth_profile_for_pipeline(profile_name)

        # Convert to legacy pipeline format
        return {
            "description": growth_config["description"],
            "category": growth_config["category"],
            "pattern_type": growth_config["pattern_type"],
            "growth_rate": growth_config["growth_rate"],
            "base_variance": growth_config["base_variance"],
            "duration": growth_config["duration"],
            "realistic_for": growth_config["realistic_for"],
        }
    except ValueError:
        # If not found in growth patterns, try seasonality patterns
        try:
            seasonality_config = convert_seasonality_profile_for_pipeline(profile_name)

            # Convert seasonality to variance-like format
            return {
                "description": seasonality_config["description"],
                "category": seasonality_config["category"],
                "pattern_type": "cyclical",
                "growth_rate": 0.0,  # No growth for pure seasonality
                "base_variance": seasonality_config["amplitude"],
                "duration": "cyclical",
                "realistic_for": seasonality_config["realistic_for"],
            }
        except ValueError:
            raise ValueError(f"Unknown variance profile: {profile_name}")


def get_anomaly_profile_for_pipeline(profile_name: str) -> Dict[str, Any]:
    """
    Get anomaly profile in pipeline format.

    This maintains compatibility with existing pipeline code.
    """
    return convert_anomaly_profile_for_pipeline(profile_name)


# Maintain backward compatibility with existing pipeline imports
def get_variance_profile(profile_name: str) -> Dict[str, Any]:
    """Legacy function name for backward compatibility."""
    return get_variance_profile_for_pipeline(profile_name)


def get_anomaly_profile(profile_name: str) -> Dict[str, Any]:
    """Legacy function name for backward compatibility."""
    return get_anomaly_profile_for_pipeline(profile_name)
