"""Public API surface for codeflash-python."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from ._model import FunctionParent, FunctionSource, FunctionToOptimize
    from .api import OptimizationConfig, OptimizationSession, optimize_function

_SUBMODULE_ATTRS: dict[str, tuple[str, str]] = {
    "FunctionParent": ("._model", "FunctionParent"),
    "FunctionSource": ("._model", "FunctionSource"),
    "FunctionToOptimize": ("._model", "FunctionToOptimize"),
    "OptimizationConfig": (".api", "OptimizationConfig"),
    "OptimizationSession": (".api", "OptimizationSession"),
    "optimize_function": (".api", "optimize_function"),
}


def __getattr__(name: str) -> Any:
    """Lazy-load public API symbols and __version__ on first access."""
    if name == "__version__":
        from importlib.metadata import version as _get_version  # noqa: PLC0415

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

    if name in _SUBMODULE_ATTRS:
        import importlib  # noqa: PLC0415

        module_path, attr_name = _SUBMODULE_ATTRS[name]
        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__ = [
    "FunctionParent",
    "FunctionSource",
    "FunctionToOptimize",
    "OptimizationConfig",
    "OptimizationSession",
    "__version__",
    "optimize_function",
]
