"""Keiro-specific model providers (EB1 ensemble, Takumi client, GPT-5 router).

Re-exports canonical provider implementations that were moved from
``ember.models.providers`` and ``ember.operators.gpt5`` into this package
as part of the keiro/ember separation.
"""

from __future__ import annotations

import os

from keiro.providers.eb1_ensemble import Eb1EnsembleProvider
from keiro.providers.gpt5_router import (
    GPT5HighRouter,
    RoutedCall,
    RouterDirective,
    RouterResult,
)
from keiro.providers.keiro import KeirProvider
from keiro.providers.takumi import TakumiEb1ARuntimeProvider, TakumiProvider


def _is_gateway_configured() -> bool:
    """Check if Keiro gateway credentials are available.

    Returns True when *any* of these credential sources exist:
      1. ``KEIRO_API_KEY`` environment variable
      2. ``~/.keiro/credentials`` file with a non-empty ``api_key``
    """
    if os.environ.get("KEIRO_API_KEY"):
        return True
    try:
        from keiro.credentials import load_credentials

        creds = load_credentials()
        if creds.get("api_key"):
            return True
    except Exception:
        pass
    return False


def get_keiro_provider() -> type:
    """Return the appropriate Keiro provider class based on configuration.

    When the Keiro gateway is configured (KEIRO_API_KEY env var or
    ~/.keiro/credentials), returns KeirProvider (HTTP proxy to the gateway).
    Otherwise returns Eb1EnsembleProvider (local ensemble execution).
    """
    if _is_gateway_configured():
        return KeirProvider
    return Eb1EnsembleProvider


__all__ = [
    "Eb1EnsembleProvider",
    "GPT5HighRouter",
    "KeirProvider",
    "RoutedCall",
    "RouterDirective",
    "RouterResult",
    "TakumiEb1ARuntimeProvider",
    "TakumiProvider",
    "get_keiro_provider",
]
