from __future__ import annotations

from grantflow.swarm.grounding_gate import evaluate_grounding_gate


def test_grounding_gate_passes_when_mode_off_even_with_weak_signals():
    state = {
        "citations": [
            {"citation_type": "fallback_namespace", "citation_confidence": 0.1},
            {"citation_type": "rag_low_confidence", "citation_confidence": 0.2},
        ],
        "architect_retrieval": {"enabled": True, "hits_count": 0},
    }
    gate = evaluate_grounding_gate(state, mode="off", min_citations_for_calibration=1)
    assert gate["mode"] == "off"
    assert gate["passed"] is True
    assert gate["blocking"] is False
    assert gate["reasons"]


def test_grounding_gate_warn_mode_flags_fallback_dominance_without_blocking():
    state = {
        "citations": [
            {"citation_type": "fallback_namespace", "citation_confidence": 0.1},
            {"citation_type": "fallback_namespace", "citation_confidence": 0.1},
            {"citation_type": "rag_low_confidence", "citation_confidence": 0.2},
            {"citation_type": "rag_low_confidence", "citation_confidence": 0.2},
            {"citation_type": "rag_claim_support", "citation_confidence": 0.9},
        ],
        "architect_retrieval": {"enabled": True, "hits_count": 0},
    }
    gate = evaluate_grounding_gate(state, mode="warn", min_citations_for_calibration=5)
    assert gate["mode"] == "warn"
    assert gate["passed"] is False
    assert gate["blocking"] is False
    assert "architect_retrieval_no_hits" in gate["reasons"]
    assert "fallback_or_low_rag_citations_dominate" in gate["reasons"]


def test_grounding_gate_warn_mode_flags_non_retrieval_dominance_when_retrieval_expected():
    state = {
        "citations": [
            {"citation_type": "strategy_reference", "citation_confidence": 0.75},
            {"citation_type": "strategy_reference", "citation_confidence": 0.75},
            {"citation_type": "strategy_reference", "citation_confidence": 0.75},
            {"citation_type": "strategy_reference", "citation_confidence": 0.75},
            {"citation_type": "rag_claim_support", "citation_confidence": 0.8},
        ],
        "architect_retrieval": {"enabled": True, "hits_count": 1},
    }
    gate = evaluate_grounding_gate(state, mode="warn", min_citations_for_calibration=5)
    assert gate["mode"] == "warn"
    assert gate["passed"] is False
    assert gate["blocking"] is False
    assert gate["strategy_reference_citation_count"] == 4
    assert gate["non_retrieval_citation_count"] == 4
    assert gate["retrieval_grounded_citation_count"] == 1
    assert "non_retrieval_citations_dominate_when_retrieval_enabled" in gate["reasons"]


def test_grounding_gate_strict_blocks_on_weak_signals():
    state = {
        "citations": [
            {"citation_type": "fallback_namespace", "citation_confidence": 0.1},
            {"citation_type": "fallback_namespace", "citation_confidence": 0.1},
            {"citation_type": "fallback_namespace", "citation_confidence": 0.1},
            {"citation_type": "rag_low_confidence", "citation_confidence": 0.2},
            {"citation_type": "rag_low_confidence", "citation_confidence": 0.2},
            {"citation_type": "rag_low_confidence", "citation_confidence": 0.2},
        ],
        "architect_retrieval": {"enabled": True, "hits_count": 0},
    }
    gate = evaluate_grounding_gate(state, mode="strict", min_citations_for_calibration=5)
    assert gate["mode"] == "strict"
    assert gate["passed"] is False
    assert gate["blocking"] is True
    assert gate["severity"] == "high"


def test_grounding_gate_warn_mode_flags_low_retrieval_metadata_completeness():
    state = {
        "citations": [
            {
                "citation_type": "rag_claim_support",
                "doc_id": "doc-1",
                "source": "policy.pdf",
                "retrieval_rank": 1,
                "retrieval_confidence": 0.9,
                "citation_confidence": 0.9,
            },
            {
                "citation_type": "rag_claim_support",
                "doc_id": "doc-2",
                "source": "policy.pdf",
                "citation_confidence": 0.9,
            },
            {
                "citation_type": "rag_claim_support",
                "doc_id": "doc-3",
                "source": "policy.pdf",
                "citation_confidence": 0.9,
            },
            {
                "citation_type": "rag_claim_support",
                "doc_id": "doc-4",
                "source": "policy.pdf",
                "citation_confidence": 0.9,
            },
            {
                "citation_type": "rag_claim_support",
                "doc_id": "doc-5",
                "source": "policy.pdf",
                "citation_confidence": 0.9,
            },
        ],
        "architect_retrieval": {"enabled": True, "hits_count": 5},
    }
    gate = evaluate_grounding_gate(
        state,
        mode="warn",
        min_citations_for_calibration=5,
        min_retrieval_metadata_complete_rate=0.8,
    )
    assert gate["mode"] == "warn"
    assert gate["passed"] is False
    assert gate["blocking"] is False
    assert gate["retrieval_grounded_citation_count"] == 5
    assert gate["retrieval_metadata_complete_citation_count"] == 1
    assert gate["retrieval_metadata_complete_rate"] == 0.2
    assert "retrieval_metadata_completeness_below_min" in gate["reasons"]
