"""
Provider name normalization service.

Centralizes provider name normalization logic to ensure consistent
handling across the metadata stage.
"""


class ProviderNormalizer:
    """
    Normalizes provider names to canonical forms.

    Ensures consistent provider identification across S3 data sources,
    configuration files, and processing logic.
    """

    # Canonical provider names
    AWS = 'aws'
    AWS_MARKETPLACE = 'awsmarketplace'
    AZURE = 'azure'
    GCP = 'gcp'
    ORACLE = 'oracle'

    # Provider name mappings: variations -> canonical
    _PROVIDER_MAPPINGS = {
        # AWS variations
        'aws': AWS,
        'amazon web services': AWS,
        'amazon': AWS,

        # AWS Marketplace variations (separate from AWS)
        'aws marketplace': AWS_MARKETPLACE,
        'awsmarketplace': AWS_MARKETPLACE,
        'aws-marketplace': AWS_MARKETPLACE,

        # Azure variations
        'azure': AZURE,
        'microsoft azure': AZURE,
        'microsoft': AZURE,

        # GCP variations
        'gcp': GCP,
        'google cloud platform': GCP,
        'google cloud': GCP,
        'google': GCP,

        # Oracle variations
        'oracle': ORACLE,
        'oracle cloud': ORACLE,
        'oci': ORACLE,
        'oracle cloud infrastructure': ORACLE,
    }

    @classmethod
    def normalize(cls, provider: str) -> str:
        """
        Normalize provider name to canonical form.

        Args:
            provider: Provider name in any format

        Returns:
            Canonical provider name (lowercase, no spaces)

        Examples:
            >>> ProviderNormalizer.normalize("AWS")
            'aws'
            >>> ProviderNormalizer.normalize("AWS Marketplace")
            'awsmarketplace'
            >>> ProviderNormalizer.normalize("Microsoft Azure")
            'azure'
        """
        provider_lower = provider.lower()

        # Check exact match in mappings
        if provider_lower in cls._PROVIDER_MAPPINGS:
            return cls._PROVIDER_MAPPINGS[provider_lower]

        # Fallback: remove spaces, hyphens, underscores
        return provider_lower.replace(' ', '').replace('-', '').replace('_', '')

    @classmethod
    def is_aws_family(cls, provider: str) -> bool:
        """
        Check if provider is in the AWS family (AWS or AWS Marketplace).

        Useful for logic that applies to both AWS and AWS Marketplace
        but not other providers.

        Args:
            provider: Provider name (normalized or original)

        Returns:
            True if provider is AWS or AWS Marketplace
        """
        normalized = cls.normalize(provider)
        return normalized in [cls.AWS, cls.AWS_MARKETPLACE]
