"""Knowledge graph + RAG interface built on the OCR pipeline.

Lightweight submodules (graph, export) import without optional deps.
The txtai/litellm RAG pieces need: uv sync --extra kg.
"""

from __future__ import annotations

from typing import Any

_LAZY: dict[str, tuple[str, str]] = {
    "get_screenshots": (".ocr", "get_screenshots"),
    "extract_text": (".ocr", "extract_text"),
    "create_and_index": (".embeddings", "create_and_index"),
    "retrieve": (".rag", "retrieve"),
    "ask_wllm": (".rag", "ask_wllm"),
    "build_graph": (".graph", "build_graph"),
    "build_from_directory": (".graph", "build_from_directory"),
    "summary": (".graph", "summary"),
    "top_entities": (".graph", "top_entities"),
    "detect_anomalies": (".graph", "detect_anomalies"),
    "chunks_for_entity": (".graph", "chunks_for_entity"),
    "chunks_for_citation": (".graph", "chunks_for_citation"),
    "related_entities": (".graph", "related_entities"),
    "expand_context": (".graph", "expand_context"),
    "export_json": (".export", "export_json"),
    "export_graphml": (".export", "export_graphml"),
    "load_json": (".export", "load_json"),
    "Neo4jExporter": (".export", "Neo4jExporter"),
}

__all__ = list(_LAZY)


def __getattr__(name: str) -> Any:
    if name not in _LAZY:
        raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
    module_name, attr = _LAZY[name]
    try:
        from importlib import import_module

        module = import_module(module_name, __name__)
    except ImportError as exc:
        raise ImportError(
            f"kg_ocr.{name} needs optional dependencies: uv sync --extra kg "
            f"(graph/export only need networkx; embeddings/rag also need txtai, litellm)"
        ) from exc
    value = getattr(module, attr)
    globals()[name] = value
    return value
