"""Key variant resolution service for metadata stage."""

from typing import Dict, List


class KeyVariantResolver:
    """Single responsibility: Resolve canonical keys to variant keys deterministically."""

    def __init__(self, variants_config: Dict[str, List[str]]):
        """
        Initialize with variant configuration.

        Args:
            variants_config: Dictionary mapping canonical keys to variant lists
                            e.g., {"environment": ["env", "Environment", "ENV"]}
        """
        self.variants_config = variants_config

    def get_variant_for_resource(self, canonical_key: str, resource_fingerprint: str) -> str:
        """
        Return variant key for this resource deterministically.

        Args:
            canonical_key: The canonical key name (e.g., "environment")
            resource_fingerprint: Unique resource identifier for consistent selection

        Returns:
            Selected variant key for this resource

        Raises:
            KeyError: If canonical_key has no variants configured
        """
        if not self.has_variants(canonical_key):
            raise KeyError(f"No variants configured for canonical key: {canonical_key}")

        variants = self.variants_config[canonical_key]
        # Use hash of fingerprint to select variant deterministically
        variant_index = hash(f"{canonical_key}:{resource_fingerprint}") % len(variants)
        return variants[variant_index]

    def has_variants(self, canonical_key: str) -> bool:
        """
        Check if canonical key has variants configured.

        Args:
            canonical_key: The canonical key name to check

        Returns:
            True if key has variants configured, False otherwise
        """
        return canonical_key in self.variants_config