"""
Time-shift configuration splitter service.

Splits time-shift configurations by provider for parallel execution.
"""

from typing import List
from ..models.time_shift_models import TimeShiftConfig
from ..models.parallel_execution_models import ProviderConfig


class TimeShiftConfigSplitter:
    """Splits time-shift configuration by provider."""

    def split_by_provider(
        self,
        config: TimeShiftConfig,
        execution_id: str
    ) -> List[ProviderConfig]:
        """
        Split config into provider-specific configs.

        Each provider gets its own config with unique dataset name
        for independent parallel processing via SQS FIFO message groups.

        Args:
            config: Time-shift configuration with multiple providers
            execution_id: Unique execution ID (for multi-user support)

        Returns:
            List of ProviderConfig, one per provider
        """
        return [
            self._create_provider_config(config, provider, execution_id)
            for provider in config.providers
        ]

    def _create_provider_config(
        self,
        config: TimeShiftConfig,
        provider: str,
        execution_id: str
    ) -> ProviderConfig:
        """Create config for single provider."""
        provider_specific = TimeShiftConfig(
            source_month=config.source_month,
            target_range=config.target_range,
            target_months=config.target_months,
            providers=[provider],  # Only this provider
            organizations=config.organizations
        )

        dataset_name = self._generate_dataset_name(provider, execution_id)

        return ProviderConfig(
            provider=provider,
            config=provider_specific,
            dataset_name=dataset_name
        )

    def _generate_dataset_name(self, provider: str, execution_id: str) -> str:
        """
        Generate unique dataset name from provider and execution ID.

        Format: time-shift-{provider}-{execution_id}
        Example: time-shift-aws-a3f7b2c4

        Normalizes provider name for use as SQS message_group_id.
        Execution ID ensures multi-user environments don't conflict.
        """
        normalized = provider.lower().replace(' ', '-')
        return f"time-shift-{normalized}-{execution_id}"
