"""Agent-Endpoint: Hermes-Status + WebUI-Link (MC verlinkt nur, betreibt nicht)."""

from fastapi import APIRouter
from pydantic import BaseModel

from fastapi import HTTPException

from services.agent import agent_status, hermes_brain_info, set_agent_brain, update_brain_model

router = APIRouter(prefix="/api")


class BrainReq(BaseModel):
    model: str


class SetBrainReq(BaseModel):
    model_id: str


@router.get("/agent/status")
def status() -> dict:
    return agent_status()


@router.get("/agent/brain")
def brain_info() -> dict:
    """Aktuelles Agent-Hirn (hermes) + bestes NousResearch-Hermes-Update."""
    return hermes_brain_info()


@router.post("/agent/brain/set")
def set_brain(body: SetBrainReq) -> dict:
    """Setzt ein installiertes Modell als Agent-Hirn (Alias + warm-Gruppe + Config)."""
    res = set_agent_brain(body.model_id)
    if not res.get("ok"):
        raise HTTPException(400, res.get("reason", "Fehler beim Setzen des Agent-Hirns"))
    return res


@router.post("/agent/brain")
def set_brain_model(body: BrainReq) -> dict:
    ok = update_brain_model(body.model)
    return {"ok": ok}
