# app/api/health.py
import httpx
from fastapi import APIRouter
from app.models.schemas import HealthResponse
from app.core.config import settings

router = APIRouter(prefix="/api", tags=["Health"])


@router.get("/health", response_model=HealthResponse)
async def health():
    """Check if Ollama is reachable and which models are available."""
    try:
        async with httpx.AsyncClient(timeout=3.0) as client:
            resp = await client.get(f"{settings.OLLAMA_URL}/api/tags")
            models = [m["name"] for m in resp.json().get("models", [])]
        return HealthResponse(
            status="ok",
            ollama="connected",
            model=settings.OLLAMA_MODEL,
            models=models,
        )
    except Exception:
        return HealthResponse(
            status="error",
            ollama="not reachable — run: ollama serve",
            model=settings.OLLAMA_MODEL,
        )

