"""Public API for codeflash-core: AI client, models, telemetry, and exceptions."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from ._capabilities import (
        CompareResultsFn,
        DiscoverFn,
        ExtractContextFn,
        NormalizeFn,
        ParseResultsFn,
        ReplaceCodeFn,
        RunTestsFn,
    )
    from ._client import AIClient
    from ._configuration import LanguageConfiguration
    from ._model import (
        BenchmarkDetail,
        Candidate,
        FileDiffContent,
        OptimizationRequest,
        OptimizationReviewResult,
        PrComment,
    )
    from ._pipeline import (
        CandidateForest,
        CandidateNode,
        EvaluationContext,
    )
    from ._platform import PlatformClient
    from ._plugin import LanguagePlugin
    from ._state import LanguageState

_SUBMODULE_ATTRS: dict[str, tuple[str, str]] = {
    # Maps public name to its private submodule and attribute.
    "REQUIRED_CAPABILITIES": ("._capabilities", "REQUIRED_CAPABILITIES"),
    "CompareResultsFn": ("._capabilities", "CompareResultsFn"),
    "DiscoverFn": ("._capabilities", "DiscoverFn"),
    "ExtractContextFn": ("._capabilities", "ExtractContextFn"),
    "NormalizeFn": ("._capabilities", "NormalizeFn"),
    "ParseResultsFn": ("._capabilities", "ParseResultsFn"),
    "ReplaceCodeFn": ("._capabilities", "ReplaceCodeFn"),
    "RunTestsFn": ("._capabilities", "RunTestsFn"),
    "validate_capabilities": ("._capabilities", "validate_capabilities"),
    "AIClient": ("._client", "AIClient"),
    "LanguageConfiguration": ("._configuration", "LanguageConfiguration"),
    "check_and_push_branch": ("._git", "check_and_push_branch"),
    "get_repo_owner_and_name": ("._git", "get_repo_owner_and_name"),
    "BenchmarkDetail": ("._model", "BenchmarkDetail"),
    "Candidate": ("._model", "Candidate"),
    "FileDiffContent": ("._model", "FileDiffContent"),
    "OptimizationRequest": ("._model", "OptimizationRequest"),
    "OptimizationReviewResult": ("._model", "OptimizationReviewResult"),
    "PrComment": ("._model", "PrComment"),
    "humanize_runtime": ("._model", "humanize_runtime"),
    "CandidateForest": ("._pipeline", "CandidateForest"),
    "CandidateNode": ("._pipeline", "CandidateNode"),
    "EvaluationContext": ("._pipeline", "EvaluationContext"),
    "create_rank_dictionary": ("._pipeline", "create_rank_dictionary"),
    "dedup_candidates": ("._pipeline", "dedup_candidates"),
    "diff_length": ("._pipeline", "diff_length"),
    "filter_refined_candidates": ("._pipeline", "filter_refined_candidates"),
    "performance_gain": ("._pipeline", "performance_gain"),
    "select_best": ("._pipeline", "select_best"),
    "PlatformClient": ("._platform", "PlatformClient"),
    "parse_repo_owner_and_name": ("._platform", "parse_repo_owner_and_name"),
    "LanguagePlugin": ("._plugin", "LanguagePlugin"),
    "LanguageState": ("._state", "LanguageState"),
    "init_telemetry": ("._telemetry", "init_telemetry"),
    "ph": ("._telemetry", "ph"),
    "AIServiceConnectionError": (".exceptions", "AIServiceConnectionError"),
    "AIServiceError": (".exceptions", "AIServiceError"),
    "InvalidAPIKeyError": (".exceptions", "InvalidAPIKeyError"),
}


def __getattr__(name: str) -> Any:
    if name == "__version__":
        from importlib.metadata import version as _get_version  # noqa: PLC0415

        try:
            val: str = _get_version("codeflash-core")
        except Exception:  # noqa: BLE001
            val = "0.0.0"
        globals()["__version__"] = val
        return val

    if name in _SUBMODULE_ATTRS:
        module_path, attr_name = _SUBMODULE_ATTRS[name]
        import importlib  # noqa: PLC0415

        mod = importlib.import_module(module_path, __name__)
        val = getattr(mod, attr_name)
        globals()[name] = val
        return val

    msg = f"module {__name__!r} has no attribute {name!r}"
    raise AttributeError(msg)


__all__ = [
    "REQUIRED_CAPABILITIES",
    "AIClient",
    "AIServiceConnectionError",
    "AIServiceError",
    "BenchmarkDetail",
    "Candidate",
    "CandidateForest",
    "CandidateNode",
    "CompareResultsFn",
    "DiscoverFn",
    "EvaluationContext",
    "ExtractContextFn",
    "FileDiffContent",
    "InvalidAPIKeyError",
    "LanguageConfiguration",
    "LanguagePlugin",
    "LanguageState",
    "NormalizeFn",
    "OptimizationRequest",
    "OptimizationReviewResult",
    "ParseResultsFn",
    "PlatformClient",
    "PrComment",
    "ReplaceCodeFn",
    "RunTestsFn",
    "__version__",
    "check_and_push_branch",
    "create_rank_dictionary",
    "dedup_candidates",
    "diff_length",
    "filter_refined_candidates",
    "get_repo_owner_and_name",
    "humanize_runtime",
    "init_telemetry",
    "parse_repo_owner_and_name",
    "performance_gain",
    "ph",
    "select_best",
    "validate_capabilities",
]
