from __future__ import annotations

import asyncio

from app.qdrant.bootstrap import bootstrap_qdrant
from app.qdrant.client import get_qdrant_client


async def is_qdrant_ready(*, ensure_bootstrapped: bool = False) -> bool:
    """Ping Qdrant and optionally verify the embeddings collection exists."""
    try:
        ready = await asyncio.to_thread(_check_qdrant_sync, ensure_bootstrapped)
        return ready
    except Exception:
        return False


def _check_qdrant_sync(ensure_bootstrapped: bool) -> bool:
    client = get_qdrant_client()
    client.get_collections()
    if ensure_bootstrapped:
        bootstrap_qdrant(client)
    return True
