import pytest
from fastapi.testclient import TestClient


@pytest.fixture(autouse=True)
def _skip_startup_bootstrap(monkeypatch):
    monkeypatch.setattr("app.main.bootstrap_qdrant", lambda *_args, **_kwargs: "adpilot_embeddings")


@pytest.fixture
def client():
    from app.main import app

    return TestClient(app)


def test_health_liveness(client):
    response = client.get("/api/v1/health/")
    assert response.status_code == 200
    assert response.json()["status"] == "ok"


def test_readyz_all_ok(client, monkeypatch):
    async def _true():
        return True

    monkeypatch.setattr("app.api.readiness.is_db_ready", _true)
    monkeypatch.setattr("app.api.readiness.is_redis_ready", _true)
    monkeypatch.setattr("app.api.readiness.is_qdrant_ready", _true)

    response = client.get("/api/v1/health/readyz")
    assert response.status_code == 200
    body = response.json()
    assert body["status"] == "ready"
    assert body["checks"] == {"mongo": True, "redis": True, "qdrant": True}


def test_readyz_fails_when_qdrant_down(client, monkeypatch):
    async def _true():
        return True

    async def _false():
        return False

    monkeypatch.setattr("app.api.readiness.is_db_ready", _true)
    monkeypatch.setattr("app.api.readiness.is_redis_ready", _true)
    monkeypatch.setattr("app.api.readiness.is_qdrant_ready", _false)

    response = client.get("/api/v1/health/readyz")
    assert response.status_code == 503
    assert response.json()["status"] == "not_ready"
    assert response.json()["checks"]["qdrant"] is False
