"""Shared model configuration helpers — read by all agents at startup."""
import os


def get_default_model(fallback: str = "gpt-5.2"):
    """Return the configured default model for standard agents."""
    model = os.getenv("DEFAULT_MODEL", fallback)
    return _resolve(model)


DIRECT_PROVIDER_PREFIXES = ("deepseek/", "minimax/", "openrouter/")


def is_openai_provider() -> bool:
    """Return True when the configured provider is OpenAI (not LiteLLM).

    OpenAI model IDs never contain a slash (e.g. 'gpt-5.2', 'o3').
    Any 'provider/model' string (e.g. 'anthropic/claude-sonnet-4-6',
    'litellm/gemini/gemini-3-flash') is treated as a LiteLLM-routed model.
    Direct third-party providers (deepseek/, minimax/, openrouter/) also
    return True since they use their own native APIs, not LiteLLM.
    """
    default = os.getenv("DEFAULT_MODEL", "")
    if "/" not in default:
        return True
    return default.startswith(DIRECT_PROVIDER_PREFIXES)


def _resolve(model: str):
    """Route 'provider/model' strings through the appropriate model wrapper.

    Handles direct third-party providers (deepseek/, minimax/, openrouter/)
    via their own native API clients. Falls back to LiteLLM for litellm/
    prefix or unknown provider/model strings.
    """
    if "/" not in model:
        return model
    if model.startswith("deepseek/"):
        from agency_swarm.utils.deepseek import build_deepseek_chat_model
        return build_deepseek_chat_model(model)
    if model.startswith("minimax/"):
        from agency_swarm.utils.minimax import build_minimax_chat_model
        return build_minimax_chat_model(model)
    if model.startswith("openrouter/"):
        from agency_swarm.utils.openrouter import build_openrouter_chat_model
        return build_openrouter_chat_model(model)
    bare = model[len("litellm/"):] if model.startswith("litellm/") else model
    try:
        from agency_swarm import LitellmModel  # noqa: PLC0415
        return LitellmModel(model=bare)
    except ImportError:
        return model
