"""Provider → Strands model-class selection (CONTRACT §6).

Maps a provider id (``bedrock`` | ``anthropic`` | ``openai``) to the matching
Strands model instance, using the configured model ids. Temperature is pinned to
0 for deterministic adjudication. ``strands`` is imported lazily so importing
this module never requires the SDK.
"""

from __future__ import annotations

from typing import Any

SUPPORTED_PROVIDERS = ("bedrock", "anthropic", "openai")

# Deterministic adjudication — never sample.
_TEMPERATURE = 0.0


def build_model(provider: str, settings: "Any") -> Any:
    """Return a Strands model instance for ``provider``.

    Raises:
        ValueError: for an unknown provider.
        RuntimeError: if ``strands`` (or the provider SDK) is unavailable.
    """
    provider = provider.lower()
    if provider not in SUPPORTED_PROVIDERS:
        raise ValueError(
            f"Unsupported provider {provider!r}; expected one of {SUPPORTED_PROVIDERS}."
        )

    model_id = settings.model_id_for(provider)

    try:
        if provider == "bedrock":
            from strands.models import BedrockModel

            return BedrockModel(
                model_id=model_id,
                region_name=settings.aws_region,
                params={"temperature": _TEMPERATURE},
            )
        if provider == "anthropic":
            from strands.models.anthropic import AnthropicModel

            return AnthropicModel(
                client_args={"api_key": settings.anthropic_api_key},
                model_id=model_id,
                params={"temperature": _TEMPERATURE},
            )
        # openai
        from strands.models.openai import OpenAIModel

        return OpenAIModel(
            client_args={"api_key": settings.openai_api_key},
            model_id=model_id,
            params={"temperature": _TEMPERATURE},
        )
    except ImportError as exc:  # pragma: no cover - needs SDK absence
        raise RuntimeError(
            f"strands model class for provider {provider!r} is unavailable: {exc}. "
            "Install strands-agents and the provider SDK, or run with adjudicate=None."
        ) from exc
