from __future__ import annotations import logging from fastapi import APIRouter from app.api.schemas import AuthConfigOut, HealthOut, SearchStatusOut from app.electronics.db.connection import check_connection from app.infrastructure.security import auth_config_summary from app.infrastructure.settings import ( DB_NAME, EMBEDDINGS_MODEL, OLLAMA_MODEL_NAME, USE_DDG_SEARCH, USE_GOOGLE_CSE, ) from app.services import ollama_service logger = logging.getLogger(__name__) router = APIRouter(tags=["health"]) @router.get("/health", response_model=HealthOut) def health() -> HealthOut: """Liveness/readiness probe used by the React app to show a banner when Postgres or Ollama aren't reachable, instead of failing silently. Ollama being down does not make the service degraded: it is only used to fill spec gaps, and the pipeline runs deterministically without it.""" db_ok = check_connection() return HealthOut( status="ok" if db_ok else "degraded", database=db_ok, database_name=DB_NAME, ollama=bool(ollama_service._ensure_client()), ollama_model=OLLAMA_MODEL_NAME, embeddings_model=EMBEDDINGS_MODEL, search=SearchStatusOut(ddg=USE_DDG_SEARCH, google_cse=USE_GOOGLE_CSE), auth=AuthConfigOut(**auth_config_summary()), )