from fastapi import APIRouter

from navi.config import settings
from navi.llm.ollama import OllamaBackend

router = APIRouter(tags=["health"])


@router.get("/health")
async def health() -> dict:
    embed_status = await _check_embed()
    return {
        "status": "ok",
        "embed": embed_status,
    }


@router.get("/health/embed")
async def health_embed() -> dict:
    return await _check_embed()


async def _check_embed() -> dict:
    backend = None
    try:
        from navi.api.deps import get_memory_store

        mem = get_memory_store()
        backend = getattr(mem, "_embedding_backend", None)
        if backend is None:
            return {"ok": False, "backend": "none", "error": "no_embedding_backend"}
        await backend.embed(["test"], model=settings.embedding_model)
        return {"ok": True, "backend": getattr(backend, "_host", "unknown"), "error": None}
    except Exception as e:
        host = getattr(backend, "_host", "unknown") if backend else "none"
        return {"ok": False, "backend": host, "error": str(e)}
