"""Top-level LLM facade – thin wrapper around :class:`BackendAdapter`."""

from __future__ import annotations

from typing import Any

from thryve.config import LLMConfig
from thryve.core.backends.adapter import BackendAdapter


def create_llm(config: LLMConfig | None = None) -> BackendAdapter:
    """Create a :class:`BackendAdapter` from a :class:`LLMConfig`.

    If *config* is ``None``, a default ``LLMConfig`` is used.
    """
    if config is None:
        config = LLMConfig()

    params: dict[str, Any] = {
        "model": config.model,
        "temperature": config.temperature,
    }
    if config.api_key is not None:
        params["api_key"] = config.api_key
    if config.base_url is not None:
        params["base_url"] = config.base_url
    if config.max_tokens is not None:
        params["max_tokens"] = config.max_tokens
    params.update(config.extra)

    # Trigger backend registration on first use
    import thryve.core.backends  # noqa: F401

    return BackendAdapter(config.backend, params)
