# grantflow/tests/test_integration_readiness.py

from fastapi.testclient import TestClient

from grantflow.api.app import app

client = TestClient(app)


def test_health_endpoint():
    response = client.get("/health")
    assert response.status_code == 200
    body = response.json()
    assert body["status"] == "healthy"
    assert body["version"]
    diagnostics = body["diagnostics"]
    assert diagnostics["job_store"]["mode"] in {"inmem", "sqlite"}
    assert diagnostics["hitl_store"]["mode"] in {"inmem", "sqlite"}
    assert diagnostics["ingest_store"]["mode"] in {"inmem", "sqlite"}
    assert diagnostics["job_runner"]["mode"] in {"background_tasks", "inmemory_queue", "redis_queue"}
    assert isinstance(diagnostics["job_runner"]["queue_enabled"], bool)
    assert isinstance(diagnostics["job_runner"]["queue"]["queue_size"], int)
    assert isinstance(diagnostics["auth"]["api_key_configured"], bool)
    assert isinstance(diagnostics["auth"]["read_auth_required"], bool)
    assert diagnostics["vector_store"]["backend"] in {"chroma", "memory"}
    assert diagnostics["vector_store"]["collection_prefix"]
    preflight_policy = diagnostics["preflight_grounding_policy"]
    assert preflight_policy["mode"] in {"warn", "strict", "off"}
    thresholds = preflight_policy["thresholds"]
    assert 0.0 <= float(thresholds["high_risk_coverage_threshold"]) <= 1.0
    assert 0.0 <= float(thresholds["medium_risk_coverage_threshold"]) <= 1.0
    assert 0.0 <= float(thresholds["high_risk_depth_coverage_threshold"]) <= 1.0
    assert 0.0 <= float(thresholds["medium_risk_depth_coverage_threshold"]) <= 1.0
    assert int(thresholds["min_uploads"]) >= 1
    runtime_gate = diagnostics["runtime_grounded_quality_gate"]
    assert runtime_gate["mode"] in {"warn", "strict", "off"}
    runtime_thresholds = runtime_gate["thresholds"]
    assert int(runtime_thresholds["min_citations_for_gate"]) >= 0
    assert 0.0 <= float(runtime_thresholds["max_non_retrieval_citation_rate"]) <= 1.0
    assert int(runtime_thresholds["min_retrieval_grounded_citations"]) >= 0
    mel_policy = diagnostics["mel_grounding_policy"]
    assert mel_policy["mode"] in {"warn", "strict", "off"}
    mel_thresholds = mel_policy["thresholds"]
    assert int(mel_thresholds["min_mel_citations"]) >= 1
    assert 0.0 <= float(mel_thresholds["min_claim_support_rate"]) <= 1.0
    assert 0.0 <= float(mel_thresholds["min_traceability_complete_rate"]) <= 1.0
    assert 0.0 <= float(mel_thresholds["max_traceability_gap_rate"]) <= 1.0
    export_policy = diagnostics["export_grounding_policy"]
    assert export_policy["mode"] in {"warn", "strict", "off"}
    export_thresholds = export_policy["thresholds"]
    assert int(export_thresholds["min_architect_citations"]) >= 1
    assert 0.0 <= float(export_thresholds["min_claim_support_rate"]) <= 1.0
    assert 0.0 <= float(export_thresholds["min_traceability_complete_rate"]) <= 1.0
    assert 0.0 <= float(export_thresholds["max_traceability_gap_rate"]) <= 1.0
    export_contract_policy = diagnostics["export_contract_policy"]
    assert export_contract_policy["mode"] in {"warn", "strict", "off"}
    export_runtime_gate_policy = diagnostics["export_runtime_grounded_gate_policy"]
    assert isinstance(export_runtime_gate_policy["require_pass"], bool)
    runtime_compatibility_policy = diagnostics["runtime_compatibility_policy"]
    assert runtime_compatibility_policy["mode"] in {"warn", "strict", "off"}
    runtime_status = runtime_compatibility_policy["status"]
    assert isinstance(runtime_status.get("python_version"), str)
    assert runtime_status.get("supported_range") == "3.11-3.13"
    assert isinstance(runtime_status.get("supported"), bool)
    tenant_policy = diagnostics["tenant_authz_configuration_policy"]
    assert tenant_policy["mode"] in {"warn", "strict", "off"}
    tenant_status = tenant_policy["status"]
    assert isinstance(tenant_status.get("enabled"), bool)
    assert isinstance(tenant_status.get("allowed_tenant_count"), int)
    assert tenant_status.get("default_tenant") is None or isinstance(tenant_status.get("default_tenant"), str)
    assert isinstance(tenant_status.get("issues"), list)
    assert isinstance(tenant_status.get("valid"), bool)
    assert isinstance(diagnostics.get("configuration_warnings"), list)
    dispatcher_policy = diagnostics["job_runner"]["dispatcher_worker_heartbeat_policy"]
    assert dispatcher_policy["mode"] in {"off", "warn", "strict"}
    dispatcher_heartbeat = diagnostics["job_runner"]["dispatcher_worker_heartbeat"]
    assert dispatcher_heartbeat is None or isinstance(dispatcher_heartbeat, dict)
