"""
Hardcoded registry of search engine module paths and class names.

This is the single source of truth for which Python module/class implements
each search engine. These are internal wiring details — not user configuration.

Engines NOT in this registry (registered at runtime instead):
- library → LibraryRAGSearchEngine (registered in search_engines_config.py)
- collection_* → CollectionSearchEngine (registered in search_engines_config.py)
- LangChain retrievers (registered in search_engines_config.py)
"""

from dataclasses import dataclass
from typing import Dict, Optional


@dataclass(frozen=True)
class EngineEntry:
    """Immutable record mapping an engine name to its implementation."""

    module_path: str
    class_name: str
    full_search_module: Optional[str] = None
    full_search_class: Optional[str] = None


ENGINE_REGISTRY: Dict[str, EngineEntry] = {
    # --- Engines from default_settings.json ---
    "arxiv": EngineEntry(
        module_path=".engines.search_engine_arxiv",
        class_name="ArXivSearchEngine",
    ),
    "brave": EngineEntry(
        module_path=".engines.search_engine_brave",
        class_name="BraveSearchEngine",
        full_search_module=".engines.full_search",
        full_search_class="FullSearchResults",
    ),
    "exa": EngineEntry(
        module_path=".engines.search_engine_exa",
        class_name="ExaSearchEngine",
    ),
    "github": EngineEntry(
        module_path=".engines.search_engine_github",
        class_name="GitHubSearchEngine",
    ),
    "google_pse": EngineEntry(
        module_path=".engines.search_engine_google_pse",
        class_name="GooglePSESearchEngine",
        full_search_module=".engines.full_search",
        full_search_class="FullSearchResults",
    ),
    "mojeek": EngineEntry(
        module_path=".engines.search_engine_mojeek",
        class_name="MojeekSearchEngine",
        full_search_module=".engines.full_search",
        full_search_class="FullSearchResults",
    ),
    "pubmed": EngineEntry(
        module_path=".engines.search_engine_pubmed",
        class_name="PubMedSearchEngine",
    ),
    "searxng": EngineEntry(
        module_path=".engines.search_engine_searxng",
        class_name="SearXNGSearchEngine",
        full_search_module=".engines.full_search",
        full_search_class="FullSearchResults",
    ),
    "serpapi": EngineEntry(
        module_path=".engines.search_engine_serpapi",
        class_name="SerpAPISearchEngine",
        full_search_module=".engines.full_search",
        full_search_class="FullSearchResults",
    ),
    "tavily": EngineEntry(
        module_path=".engines.search_engine_tavily",
        class_name="TavilySearchEngine",
    ),
    "wayback": EngineEntry(
        module_path=".engines.search_engine_wayback",
        class_name="WaybackSearchEngine",
    ),
    "wikinews": EngineEntry(
        module_path=".engines.search_engine_wikinews",
        class_name="WikinewsSearchEngine",
    ),
    "wikipedia": EngineEntry(
        module_path=".engines.search_engine_wikipedia",
        class_name="WikipediaSearchEngine",
    ),
    # --- Engines from defaults/settings/search_engines/*.json ---
    "elasticsearch": EngineEntry(
        module_path=".engines.search_engine_elasticsearch",
        class_name="ElasticsearchSearchEngine",
    ),
    "paperless": EngineEntry(
        module_path=".engines.search_engine_paperless",
        class_name="PaperlessSearchEngine",
    ),
    "parallel": EngineEntry(
        module_path=".engines.parallel_search_engine",
        class_name="ParallelSearchEngine",
    ),
    "scaleserp": EngineEntry(
        module_path=".engines.search_engine_scaleserp",
        class_name="ScaleSerpSearchEngine",
    ),
    "serper": EngineEntry(
        module_path=".engines.search_engine_serper",
        class_name="SerperSearchEngine",
    ),
    # --- Engines from defaults/settings_*.json ---
    "nasa_ads": EngineEntry(
        module_path=".engines.search_engine_nasa_ads",
        class_name="NasaAdsSearchEngine",
    ),
    "openalex": EngineEntry(
        module_path=".engines.search_engine_openalex",
        class_name="OpenAlexSearchEngine",
    ),
    "semantic_scholar": EngineEntry(
        module_path=".engines.search_engine_semantic_scholar",
        class_name="SemanticScholarSearchEngine",
    ),
    # --- Engines from defaults/settings_*.json (added in #1540) ---
    "gutenberg": EngineEntry(
        module_path=".engines.search_engine_gutenberg",
        class_name="GutenbergSearchEngine",
    ),
    "openlibrary": EngineEntry(
        module_path=".engines.search_engine_openlibrary",
        class_name="OpenLibrarySearchEngine",
    ),
    "pubchem": EngineEntry(
        module_path=".engines.search_engine_pubchem",
        class_name="PubChemSearchEngine",
    ),
    "stackexchange": EngineEntry(
        module_path=".engines.search_engine_stackexchange",
        class_name="StackExchangeSearchEngine",
    ),
    "zenodo": EngineEntry(
        module_path=".engines.search_engine_zenodo",
        class_name="ZenodoSearchEngine",
    ),
    # --- Engines implemented but without settings files ---
    "ddg": EngineEntry(
        module_path=".engines.search_engine_ddg",
        class_name="DuckDuckGoSearchEngine",
    ),
    "guardian": EngineEntry(
        module_path=".engines.search_engine_guardian",
        class_name="GuardianSearchEngine",
    ),
    # --- Meta engine ---
    "auto": EngineEntry(
        module_path=".engines.meta_search_engine",
        class_name="MetaSearchEngine",
    ),
}


def get_engine_entry(name: str) -> Optional[EngineEntry]:
    """Look up an engine's implementation details by name."""
    return ENGINE_REGISTRY.get(name)
