from fastapi import APIRouter
from sqlalchemy import text

from app.db import engine

router = APIRouter(prefix="/api", tags=["ops"])


@router.get("/healthz")
def healthz() -> dict:
    try:
        with engine.connect() as conn:
            conn.execute(text("SELECT 1"))
        return {"status": "ok", "database": "ok"}
    except Exception:
        # 200 with a degraded body, not 503 — the API process is alive and
        # the load balancer shouldn't cycle it for a transient DB blip.
        return {"status": "degraded", "database": "unreachable"}
