from __future__ import annotations

from grantflow.eval.harness import format_eval_suite_report


def test_format_eval_suite_report_highlights_fallback_dominance_signals():
    suite = {
        "suite_label": "llm-eval",
        "expectations_skipped": True,
        "case_count": 2,
        "passed_count": 2,
        "failed_count": 0,
        "all_passed": True,
        "cases": [
            {
                "case_id": "c1",
                "donor_id": "usaid",
                "passed": True,
                "failed_checks": [],
                "metrics": {
                    "quality_score": 7.0,
                    "critic_score": 7.0,
                    "toc_schema_valid": True,
                    "fatal_flaw_count": 2,
                    "llm_finding_label_counts": {"CAUSAL_LINK_DETAIL": 2, "BASELINE_TARGET_MISSING": 1},
                    "llm_advisory_applied_label_counts": {"CAUSAL_LINK_DETAIL": 2},
                    "llm_advisory_rejected_label_counts": {"BASELINE_TARGET_MISSING": 1},
                    "citations_total": 10,
                    "fallback_namespace_citation_count": 9,
                    "high_severity_fatal_flaw_count": 0,
                    "low_confidence_citation_count": 9,
                    "needs_revision": False,
                },
            },
            {
                "case_id": "c2",
                "donor_id": "eu",
                "passed": True,
                "failed_checks": [],
                "metrics": {
                    "quality_score": 7.0,
                    "critic_score": 7.0,
                    "toc_schema_valid": True,
                    "fatal_flaw_count": 1,
                    "llm_finding_label_counts": {"BASELINE_TARGET_MISSING": 1},
                    "llm_advisory_applied_label_counts": {},
                    "llm_advisory_rejected_label_counts": {"BASELINE_TARGET_MISSING": 1},
                    "citations_total": 4,
                    "fallback_namespace_citation_count": 4,
                    "high_severity_fatal_flaw_count": 0,
                    "low_confidence_citation_count": 4,
                    "needs_revision": False,
                },
            },
        ],
    }

    text = format_eval_suite_report(suite)
    assert "grounding_risk=fallback_dominant:high(90%)" in text
    assert "Grounding risk summary (fallback dominance)" in text
    assert "usaid: fallback_dominance=high (90%) fallback_ns_citations=9/10" in text
    assert "LLM finding label mix (suite-level)" in text
    assert "- BASELINE_TARGET_MISSING: 2" in text
    assert "- CAUSAL_LINK_DETAIL: 2" in text
    assert "LLM finding label mix by donor" in text
    assert "- usaid: CAUSAL_LINK_DETAIL=2, BASELINE_TARGET_MISSING=1" in text
    assert "LLM advisory label mix (applied)" in text
    assert "- CAUSAL_LINK_DETAIL: 2" in text
    assert "LLM advisory label mix (rejected)" in text
    assert "- BASELINE_TARGET_MISSING: 2" in text
    assert "LLM advisory label mix (rejected) by donor" in text
    assert "- usaid: BASELINE_TARGET_MISSING=1" in text
    assert "Grounding risk summary (non-retrieval dominance)" in text
    # c2 is below minimum citation threshold for dominance reporting
    assert "eu: fallback_dominance" not in text


def test_format_eval_suite_report_suppresses_grounding_risk_when_architect_rag_disabled():
    suite = {
        "suite_label": "baseline",
        "expectations_skipped": True,
        "case_count": 1,
        "passed_count": 1,
        "failed_count": 0,
        "all_passed": True,
        "cases": [
            {
                "case_id": "c_no_rag",
                "donor_id": "eu",
                "passed": True,
                "failed_checks": [],
                "metrics": {
                    "architect_rag_enabled": False,
                    "quality_score": 8.75,
                    "critic_score": 8.75,
                    "toc_schema_valid": True,
                    "fatal_flaw_count": 1,
                    "citations_total": 12,
                    "fallback_namespace_citation_count": 0,
                    "strategy_reference_citation_count": 12,
                    "non_retrieval_citation_count": 12,
                    "traceability_gap_citation_count": 0,
                    "high_severity_fatal_flaw_count": 0,
                    "low_confidence_citation_count": 0,
                    "needs_revision": False,
                },
            }
        ],
    }
    text = format_eval_suite_report(suite)
    assert "grounding_risk=" not in text
    assert "Grounding risk summary (fallback dominance)" not in text
    assert "Grounding risk summary (non-retrieval dominance)" not in text
