"""
Registry for managing query paradigms.
"""

from typing import Dict, List, Optional
from .base import BaseQueryParadigm


class QueryParadigmRegistry:
    """Registry for managing and discovering query paradigms."""

    def __init__(self):
        self._paradigms: Dict[str, BaseQueryParadigm] = {}

    def register(self, paradigm: BaseQueryParadigm) -> None:
        """Register a new query paradigm."""
        self._paradigms[paradigm.name.lower()] = paradigm

    def get(self, name: str) -> Optional[BaseQueryParadigm]:
        """Get a paradigm by name."""
        return self._paradigms.get(name.lower())

    def list_all(self) -> List[BaseQueryParadigm]:
        """Get all registered paradigms."""
        return list(self._paradigms.values())

    def list_by_category(self, category: str) -> List[BaseQueryParadigm]:
        """Get paradigms by category."""
        return [p for p in self._paradigms.values() if p.category == category]

    def list_by_complexity(self, complexity: str) -> List[BaseQueryParadigm]:
        """Get paradigms by complexity level."""
        return [p for p in self._paradigms.values() if p.complexity == complexity]

    def get_categories(self) -> List[str]:
        """Get all available categories."""
        return list(set(p.category for p in self._paradigms.values()))

    def get_complexity_levels(self) -> List[str]:
        """Get all available complexity levels."""
        return list(set(p.complexity for p in self._paradigms.values()))

    def search_paradigms(self, query: str) -> List[BaseQueryParadigm]:
        """Search paradigms by name or description."""
        query_lower = query.lower()
        results = []

        for paradigm in self._paradigms.values():
            if (query_lower in paradigm.name.lower() or
                query_lower in paradigm.description.lower()):
                results.append(paradigm)

        return results

    def get_translation_matrix(self) -> Dict[str, List[str]]:
        """Get matrix of which paradigms can translate to which others."""
        matrix = {}
        for paradigm in self._paradigms.values():
            targets = []
            for other in self._paradigms.values():
                if other.can_translate_from(paradigm.name):
                    targets.append(other.name)
            matrix[paradigm.name] = targets
        return matrix


# Global registry instance
registry = QueryParadigmRegistry()