from __future__ import annotations

from grantflow.api.public_views import _buyer_trust_summary


def _call(**overrides):
    defaults = dict(
        job_status="done",
        hitl_enabled=False,
        open_high_severity_finding_count=0,
        open_finding_count=0,
        export_contract_passed=True,
        citation_count=5,
        grounded_gate={},
        grounding_risk_level="low",
    )
    defaults.update(overrides)
    return _buyer_trust_summary(**defaults)


class TestVerdictExportReady:
    def test_all_green_no_hitl(self):
        result = _call()
        assert result["verdict"] == "export_ready"
        assert result["export_ready"] is True
        assert result["critic_passed"] is True
        assert result["citations_present"] is True
        assert result["hitl_resolved"] is None
        assert result["blocking_reasons"] == []

    def test_all_green_hitl_resolved(self):
        result = _call(hitl_enabled=True, job_status="done")
        assert result["verdict"] == "export_ready"
        assert result["hitl_resolved"] is True

    def test_grounded_gate_passed(self):
        result = _call(grounded_gate={"passed": True}, grounding_risk_level=None)
        assert result["verdict"] == "export_ready"
        assert result["grounded"] is True

    def test_no_grounding_signal_still_export_ready(self):
        result = _call(grounded_gate={}, grounding_risk_level=None)
        assert result["verdict"] == "export_ready"
        assert result["grounded"] is None


class TestVerdictNeedsRevision:
    def test_hitl_pending(self):
        result = _call(job_status="pending_hitl", hitl_enabled=True)
        assert result["verdict"] == "needs_revision"
        assert result["export_ready"] is False
        assert "hitl_pending" in result["blocking_reasons"]
        assert result["hitl_resolved"] is False

    def test_open_high_severity_finding(self):
        result = _call(open_high_severity_finding_count=2)
        assert result["verdict"] == "needs_revision"
        assert result["critic_passed"] is False
        assert any("open_high_severity_findings" in r for r in result["blocking_reasons"])

    def test_governance_flags_reflect_findings(self):
        result = _call(open_high_severity_finding_count=1, open_finding_count=3)
        assert result["governance_flags"]["open_high_severity_finding_count"] == 1
        assert result["governance_flags"]["open_finding_count"] == 3


class TestVerdictNeedsReview:
    def test_export_contract_failed_no_high_severity(self):
        result = _call(export_contract_passed=False)
        assert result["verdict"] == "needs_review"
        assert result["export_ready"] is False
        assert "export_contract_failed" in result["blocking_reasons"]

    def test_grounding_failed_no_high_severity(self):
        result = _call(grounded_gate={"passed": False}, grounding_risk_level=None)
        assert result["verdict"] == "needs_review"
        assert result["grounded"] is False
        assert "grounding_failed" in result["blocking_reasons"]

    def test_grounding_risk_high_no_gate(self):
        result = _call(grounded_gate={}, grounding_risk_level="high")
        assert result["grounded"] is False
        assert result["verdict"] == "needs_review"


class TestVerdictIncomplete:
    def test_running_job(self):
        result = _call(job_status="running")
        assert result["verdict"] == "incomplete"
        assert result["export_ready"] is False
        assert "job_not_terminal" in result["blocking_reasons"]

    def test_queued_job(self):
        result = _call(job_status="queued")
        assert result["verdict"] == "incomplete"

    def test_job_error(self):
        result = _call(job_status="error")
        assert result["verdict"] == "needs_review"
        assert "job_error" in result["blocking_reasons"]


class TestCitationsPresent:
    def test_no_citations(self):
        result = _call(citation_count=0)
        assert result["citations_present"] is False

    def test_with_citations(self):
        result = _call(citation_count=3)
        assert result["citations_present"] is True


class TestGroundedGatePriority:
    def test_gate_passed_overrides_risk_level_high(self):
        result = _call(grounded_gate={"passed": True}, grounding_risk_level="high")
        assert result["grounded"] is True

    def test_gate_failed_overrides_risk_level_low(self):
        result = _call(grounded_gate={"passed": False}, grounding_risk_level="low")
        assert result["grounded"] is False
