from __future__ import annotations

from grantflow.api.public_views import (
    public_job_critic_payload,
    public_job_export_payload,
    public_job_payload,
)

from grantflow.tests.contracts_helpers import fixture_json, sample_quality_contract_job


def test_public_job_payload_matches_golden_snapshot():
    expected = fixture_json("public_job_payload_golden.json")

    job = {
        "status": "done",
        "hitl_enabled": False,
        "webhook_url": "https://example.com/hook",
        "webhook_secret": "secret",
        "job_events": [{"event_id": "e1"}],
        "review_comments": [{"comment_id": "c1"}],
        "client_metadata": {"source": "demo"},
        "state": {
            "donor_id": "usaid",
            "strategy": {"internal": "secret"},
            "donor_strategy": {"internal": "secret"},
            "toc_draft": {"toc": {"brief": "Sample"}},
        },
    }

    actual = public_job_payload(job)
    assert actual == expected


def test_public_job_export_payload_matches_golden_snapshot_and_redacts_strategy():
    expected = fixture_json("public_job_export_payload_golden.json")
    job = {
        "status": "done",
        "webhook_url": "https://example.com/hook",
        "webhook_secret": "secret",
        "state": {
            "donor_id": "usaid",
            "strategy": {"internal": "x"},
            "donor_strategy": {"internal": "y"},
            "toc_draft": {"toc": {"brief": "Sample"}},
            "critic_notes": {
                "fatal_flaws": [
                    {
                        "finding_id": "finding-1",
                        "status": "open",
                        "severity": "high",
                        "section": "toc",
                        "code": "TOC_GAP",
                        "message": "Missing output chain",
                        "source": "rules",
                    }
                ]
            },
        },
        "review_comments": [
            {
                "comment_id": "comment-1",
                "status": "open",
                "section": "toc",
                "author": "rev",
                "message": "Need details",
                "linked_finding_id": "finding-1",
                "version_id": "toc_v1",
                "ts": "2026-02-27T00:00:00Z",
            }
        ],
    }

    actual = public_job_export_payload("job-1", job)
    assert actual == expected
    state = ((actual.get("payload") or {}).get("state") or {}) if isinstance(actual, dict) else {}
    assert "strategy" not in state
    assert "donor_strategy" not in state


def test_public_job_critic_payload_matches_golden_snapshot():
    expected = fixture_json("public_job_critic_payload_golden.json")
    job = sample_quality_contract_job()
    actual = public_job_critic_payload("job-quality-1", job)
    assert actual == expected


def test_public_job_export_payload_degrades_attachment_readiness_when_ready_file_missing():
    job = {
        "status": "done",
        "state": {
            "donor_id": "usaid",
            "toc_draft": {
                "attachment_manifest": [
                    {
                        "status": "ready",
                        "source_path": "/definitely/missing/path/attachment.pdf",
                    }
                ]
            },
            "export_contract_gate": {
                "submission_readiness_summary": {
                    "readiness_status": "ready",
                    "top_gap": "none",
                    "completeness_score": 100.0,
                    "submission_package_counts": {
                        "total": 2,
                        "ready": 2,
                        "partial": 0,
                    },
                    "attachment_manifest_counts": {
                        "total": 1,
                        "ready": 1,
                        "partial": 0,
                        "pending": 0,
                    },
                    "compliance_matrix_counts": {
                        "total": 1,
                        "ready": 1,
                        "partial": 0,
                    },
                }
            },
        },
    }

    actual = public_job_export_payload("job-attachment-check-1", job)
    readiness = (actual.get("payload") or {}).get("submission_package_readiness") or {}

    assert readiness.get("top_gap") == "attachment_files"
    assert readiness.get("readiness_status") == "partial"
    assert readiness.get("completeness_score") == 75.0
    assert readiness.get("attachment_manifest_counts") == {
        "total": 1,
        "ready": 0,
        "partial": 0,
        "pending": 1,
    }
    assert readiness.get("attachment_file_validation") == {"missing_ready_file_count": 1}


def test_public_job_export_payload_uses_state_export_contract_gate_when_provided():
    gate = {
        "policy_mode": "warn",
        "ready_for_export": False,
        "summary": "manual gate snapshot",
        "submission_readiness_summary": {
            "readiness_status": "partial",
            "completeness_score": 62.5,
        },
    }
    job = {
        "status": "done",
        "state": {
            "donor_id": "usaid",
            "export_contract_gate": gate,
            "toc_draft": {"project_goal": "Sample"},
        },
    }

    actual = public_job_export_payload("job-export-contract-1", job)
    export_contract = (actual.get("payload") or {}).get("export_contract") or {}

    assert export_contract == gate
    readiness = (actual.get("payload") or {}).get("submission_package_readiness") or {}
    assert readiness.get("readiness_status") == "partial"
    assert readiness.get("completeness_score") == 62.5
