from datetime import datetime, timedelta, timezone

from grantflow.api.public_views import _comment_triage_summary_payload, _finding_review_title


def test_finding_review_title_prefers_human_readable_mapping_for_known_codes():
    title = _finding_review_title(
        {
            "code": "USAID_DO_MISSING",
            "section": "toc",
            "message": "USAID ToC is missing Development Objectives.",
        }
    )
    assert title == "USAID Development Objectives Missing"


def test_finding_review_title_uses_semantic_mapping_for_boilerplate():
    title = _finding_review_title(
        {
            "code": "TOC_BOILERPLATE_REPETITION",
            "section": "toc",
            "message": "Theory of Change contains repeated boilerplate narrative across multiple sections.",
        }
    )
    assert title == "ToC Boilerplate Repetition"


def test_finding_review_title_uses_semantic_mapping_for_traceability_gap():
    title = _finding_review_title(
        {
            "code": "ARCHITECT_TRACEABILITY_GAP",
            "section": "toc",
            "message": "Architect claim citations contain significant traceability gaps with missing doc_id and page metadata.",
        }
    )
    assert title == "Citation Traceability Gap"


def test_comment_triage_summary_tracks_overdue_and_next_action():
    now = datetime.now(timezone.utc)
    summary = _comment_triage_summary_payload(
        review_comments=[
            {
                "comment_id": "comment-1",
                "status": "open",
                "section": "toc",
                "linked_finding_id": "finding-1",
                "ts": (now - timedelta(days=3)).isoformat(),
                "due_at": (now - timedelta(days=2, hours=20)).isoformat(),
            },
            {
                "comment_id": "comment-2",
                "status": "acknowledged",
                "section": "logframe",
                "linked_finding_id": "finding-2",
                "ts": (now - timedelta(hours=6)).isoformat(),
                "acknowledged_at": (now - timedelta(hours=5, minutes=45)).isoformat(),
            },
            {
                "comment_id": "comment-3",
                "status": "resolved",
                "section": "logframe",
                "ts": (now - timedelta(hours=5)).isoformat(),
                "resolved_at": (now - timedelta(hours=4, minutes=30)).isoformat(),
            },
        ],
        critic_findings=[
            {
                "id": "finding-1",
                "status": "open",
                "severity": "high",
                "section": "toc",
                "message": "Theory of Change repeats boilerplate narrative across multiple sections.",
            },
            {
                "id": "finding-2",
                "status": "acknowledged",
                "severity": "medium",
                "section": "logframe",
                "message": "Indicator baseline and target values still need confirmation.",
            },
        ],
        donor_id="usaid",
    )
    assert summary["open_comment_count"] == 2
    assert summary["resolved_comment_count"] == 1
    assert summary["acknowledged_comment_count"] == 1
    assert summary["overdue_comment_count"] == 1
    assert summary["stale_open_comment_count"] == 1
    assert summary["aging_band_counts"]["d3_7"] == 1
    assert summary["aging_band_counts"]["lt_24h"] == 1
    assert summary["next_comment_section"] == "toc"
    assert summary["next_comment_bucket"] == "logic"
    assert summary["comment_bucket_counts"]["logic"] == 1
    assert "USAID results hierarchy" in str(summary["next_recommended_action"])
