"""Dependency readiness checks without FastAPI or Runtime side effects."""

from __future__ import annotations

from datetime import datetime, timezone


def _check(name, probe):
    try:
        return {"ok": bool(probe()), "error": None}
    except Exception as exc:  # noqa: BLE001 - readiness must report bounded failure
        return {"ok": False, "error": f"{name}_unavailable:{type(exc).__name__}"}


def collect_runtime_health(engine, storage, redis_publisher, model_name: str) -> dict:
    """Return a bounded component summary suitable for /health and /ready."""
    components = {
        "triton_model": _check(
            "triton_model",
            lambda: engine.is_model_ready(model_name, force_refresh=True),
        ),
        "postgresql": _check("postgresql", storage.ping),
        "redis": _check("redis", redis_publisher.ping),
    }
    ready = all(component["ok"] for component in components.values())
    return {
        "status": "ready" if ready else "not_ready",
        "ready": ready,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "model_name": model_name,
        "components": components,
    }
