from grantflow.api.public_views import (
    public_job_review_workflow_payload,
    public_portfolio_review_workflow_payload,
)
from grantflow.tests.public_views_triage_helpers import clone_review_workflow_job


def test_job_review_workflow_payload_emits_comment_triage_and_reviewer_workflow_summary():
    payload = public_job_review_workflow_payload("job-1", clone_review_workflow_job())
    summary = payload["summary"]
    findings = payload["findings"]
    assert findings[0]["triage_priority_label"] == "P0 · Immediate"
    assert "triage_priority_rationale" in findings[0]
    assert "severity is high" in str(findings[0]["triage_priority_rationale"]).lower()
    assert isinstance(findings[0]["reviewer_next_actions"], list)
    assert findings[0]["reviewer_next_action_short"]
    assert findings[0]["reviewer_immediate_next_action"] == findings[0]["reviewer_next_action_short"]
    assert summary["acknowledged_comment_count"] == 1
    assert summary["comment_triage_summary"]["stale_comment_bucket_counts"]["logic"] == 2
    assert summary["comment_triage_summary"]["next_comment_bucket"] == "logic"
    assert summary["reviewer_workflow_summary"]["open_items"] == 3
    assert summary["reviewer_workflow_summary"]["acknowledged_items"] == 1
    assert summary["reviewer_workflow_summary"]["resolved_items"] == 1
    assert summary["reviewer_workflow_summary"]["top_stale_comment_bucket"] == "logic"
    assert summary["action_queue_summary"]["finding_ack_queue_count"] == 1
    assert summary["action_queue_summary"]["comment_ack_queue_count"] == 2
    assert summary["action_queue_summary"]["comment_resolve_queue_count"] == 1
    assert summary["action_queue_summary"]["comment_reopen_queue_count"] == 1
    assert summary["action_queue_summary"]["next_primary_action"] == "ack_finding"
    assert summary["throughput_summary"]["finding_ack_completed_count"] == 0
    assert summary["throughput_summary"]["comment_ack_completed_count"] == 1
    assert summary["throughput_summary"]["comment_resolve_completed_count"] == 1
    assert summary["throughput_summary"]["dominant_completed_action"] == "ack_comment"
    assert summary["queue_delta_summary"]["finding_ack_queue_count"] == 1
    assert summary["queue_delta_summary"]["comment_ack_net_delta"] == 1
    assert summary["queue_delta_summary"]["comment_resolve_net_delta"] == 0
    assert summary["triage_summary"]["next_priority_label"] == "P0 · Immediate"
    assert "ToC" in str(summary["triage_summary"]["next_action_brief"])
    assert "next_priority_rationale" in summary["triage_summary"]
    assert "severity is high" in str(summary["triage_summary"]["next_priority_rationale"]).lower()
    assert summary["triage_summary"]["next_immediate_action"] == summary["triage_summary"]["next_action_brief"]
    assert summary["review_workflow_policy_summary"]["status"] == "breach"
    assert summary["review_workflow_policy_summary"]["go_no_go_flag"] == "hold"
    assert "overdue_review_comments_present" in summary["review_workflow_policy_summary"]["breaches"]


def test_portfolio_review_workflow_payload_aggregates_reviewer_workflow_summary():
    portfolio = public_portfolio_review_workflow_payload(
        {
            "job-1": clone_review_workflow_job(),
            "job-2": clone_review_workflow_job(donor_id="eu"),
        }
    )
    summary = portfolio["summary"]
    assert summary["comment_status_counts"]["open"] == 2
    assert summary["reviewer_workflow_summary"]["open_items"] >= 2
    assert summary["reviewer_workflow_summary"]["stale_comment_bucket_counts"]["logic"] == 4
    assert summary["reviewer_workflow_summary"]["top_stale_comment_bucket"] == "logic"
    assert summary["action_queue_summary"]["finding_ack_queue_count"] == 2
    assert summary["action_queue_summary"]["comment_ack_queue_count"] == 2
    assert summary["action_queue_summary"]["next_primary_action"] == "ack_finding"
    assert summary["throughput_summary"]["comment_added_count"] == 2
    assert summary["throughput_summary"]["dominant_completed_action"] == "ack_comment"
    assert summary["queue_delta_summary"]["finding_ack_queue_count"] == 2
    assert summary["queue_delta_summary"]["comment_intake_net_delta"] == 0
    assert summary["review_workflow_policy_summary"]["status"] == "breach"
    assert summary["review_workflow_policy_summary"]["go_no_go_flag"] == "hold"
