"""
Integrations-Router: testet die OpenAI-kompatible Verbindung zur LLM-Engine.
Damit der „Verbinden"-Assistent dem Nutzer sagen kann: funktioniert ✓ + welche Modelle.
"""

from fastapi import APIRouter, Depends

from auth import auth
from config import LLAMA_SWAP_URL
from llamaswap import _swap_get

router = APIRouter(prefix="/api/integration", dependencies=[Depends(auth)])


@router.get("/test")
def test_connection():
    """Ruft die OpenAI-kompatible Modell-Liste der Engine ab (das, was externe Tools nutzen)."""
    try:
        data = _swap_get("/v1/models")
        items = data.get("data", data) if isinstance(data, dict) else data
        models = [m.get("id") for m in (items or []) if isinstance(m, dict) and m.get("id")]
        return {"ok": True, "models": models, "url": LLAMA_SWAP_URL}
    except Exception as exc:  # noqa: BLE001
        return {"ok": False, "error": str(exc), "url": LLAMA_SWAP_URL}
