# grantflow/tests/test_integration_readiness_policy_overrides.py

from fastapi.testclient import TestClient

from grantflow.api.app import app
from grantflow.core.config import config

client = TestClient(app)


def test_ready_endpoint_reflects_preflight_grounding_threshold_overrides(monkeypatch):
    monkeypatch.setattr(config.graph, "grounding_gate_mode", "strict")
    monkeypatch.setattr(config.graph, "preflight_grounding_policy_mode", "strict")
    monkeypatch.setattr(config.graph, "preflight_grounding_high_risk_coverage_threshold", 0.42)
    monkeypatch.setattr(config.graph, "preflight_grounding_medium_risk_coverage_threshold", 0.91)
    monkeypatch.setattr(config.graph, "preflight_grounding_min_uploads", 7)
    monkeypatch.setattr(config.graph, "preflight_grounding_min_key_claim_coverage_rate", 0.61)
    monkeypatch.setattr(config.graph, "preflight_grounding_max_fallback_claim_ratio", 0.72)
    monkeypatch.setattr(config.graph, "preflight_grounding_max_traceability_gap_rate", 0.33)
    monkeypatch.setattr(config.graph, "preflight_grounding_min_threshold_hit_rate", 0.44)

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    policy = body["checks"]["preflight_grounding_policy"]
    assert policy["mode"] == "strict"
    thresholds = policy["thresholds"]
    assert thresholds["high_risk_coverage_threshold"] == 0.42
    assert thresholds["medium_risk_coverage_threshold"] == 0.91
    assert thresholds["min_uploads"] == 7
    assert thresholds["min_key_claim_coverage_rate"] == 0.61
    assert thresholds["max_fallback_claim_ratio"] == 0.72
    assert thresholds["max_traceability_gap_rate"] == 0.33
    assert thresholds["min_threshold_hit_rate"] == 0.44


def test_ready_endpoint_preflight_policy_mode_can_differ_from_pipeline_mode(monkeypatch):
    monkeypatch.setattr(config.graph, "grounding_gate_mode", "strict")
    monkeypatch.setattr(config.graph, "preflight_grounding_policy_mode", "warn")

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    assert body["checks"]["preflight_grounding_policy"]["mode"] == "warn"


def test_ready_endpoint_reflects_runtime_grounded_quality_gate_overrides(monkeypatch):
    monkeypatch.setattr(config.graph, "runtime_grounded_quality_gate_mode", "strict")
    monkeypatch.setattr(config.graph, "runtime_grounded_quality_gate_min_citations", 9)
    monkeypatch.setattr(
        config.graph,
        "runtime_grounded_quality_gate_max_non_retrieval_citation_rate",
        0.22,
    )
    monkeypatch.setattr(
        config.graph,
        "runtime_grounded_quality_gate_min_retrieval_grounded_citations",
        4,
    )

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    policy = body["checks"]["runtime_grounded_quality_gate"]
    assert policy["mode"] == "strict"
    thresholds = policy["thresholds"]
    assert thresholds["min_citations_for_gate"] == 9
    assert thresholds["max_non_retrieval_citation_rate"] == 0.22
    assert thresholds["min_retrieval_grounded_citations"] == 4


def test_ready_endpoint_reflects_mel_grounding_policy_overrides(monkeypatch):
    monkeypatch.setattr(config.graph, "mel_grounding_policy_mode", "strict")
    monkeypatch.setattr(config.graph, "mel_grounding_min_mel_citations", 5)
    monkeypatch.setattr(config.graph, "mel_grounding_min_claim_support_rate", 0.61)
    monkeypatch.setattr(config.graph, "mel_grounding_min_traceability_complete_rate", 0.72)
    monkeypatch.setattr(config.graph, "mel_grounding_max_traceability_gap_rate", 0.28)

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    mel_policy = body["checks"]["mel_grounding_policy"]
    assert mel_policy["mode"] == "strict"
    thresholds = mel_policy["thresholds"]
    assert thresholds["min_mel_citations"] == 5
    assert thresholds["min_claim_support_rate"] == 0.61
    assert thresholds["min_traceability_complete_rate"] == 0.72
    assert thresholds["max_traceability_gap_rate"] == 0.28


def test_ready_endpoint_reflects_export_grounding_policy_overrides(monkeypatch):
    monkeypatch.setattr(config.graph, "export_grounding_policy_mode", "strict")
    monkeypatch.setattr(config.graph, "export_grounding_min_architect_citations", 7)
    monkeypatch.setattr(config.graph, "export_grounding_min_claim_support_rate", 0.66)
    monkeypatch.setattr(config.graph, "export_grounding_min_traceability_complete_rate", 0.74)
    monkeypatch.setattr(config.graph, "export_grounding_max_traceability_gap_rate", 0.26)

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    export_policy = body["checks"]["export_grounding_policy"]
    assert export_policy["mode"] == "strict"
    thresholds = export_policy["thresholds"]
    assert thresholds["min_architect_citations"] == 7
    assert thresholds["min_claim_support_rate"] == 0.66
    assert thresholds["min_traceability_complete_rate"] == 0.74
    assert thresholds["max_traceability_gap_rate"] == 0.26


def test_ready_endpoint_reflects_export_contract_policy_override(monkeypatch):
    monkeypatch.setattr(config.graph, "export_contract_policy_mode", "strict")

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    contract_policy = body["checks"]["export_contract_policy"]
    assert contract_policy["mode"] == "strict"


def test_ready_endpoint_reflects_export_runtime_grounded_gate_policy_override(monkeypatch):
    monkeypatch.setattr(config.graph, "export_require_grounded_gate_pass", True)

    response = client.get("/ready")
    assert response.status_code == 200
    body = response.json()
    policy = body["checks"]["export_runtime_grounded_gate_policy"]
    assert policy["require_pass"] is True
