from __future__ import annotations

import httpx

from app.core.config import settings
from app.core.database import is_db_ready


async def is_embedding_engine_ready() -> bool:
    try:
        url = f"{settings.EMBEDDING_ENGINE_URL.rstrip('/')}/healthz"
        async with httpx.AsyncClient(timeout=5.0) as client:
            response = await client.get(url)
            return response.status_code == 200
    except Exception:
        return False


async def readiness_payload() -> tuple[dict, int]:
    mongo_ok = await is_db_ready()
    embedding_ok = await is_embedding_engine_ready()
    checks = {"mongo": mongo_ok, "embedding_engine": embedding_ok}
    all_ok = all(checks.values())
    body = {
        "status": "ready" if all_ok else "not_ready",
        "checks": checks,
    }
    return body, 200 if all_ok else 503
