from io import BytesIO

from docx import Document

from grantflow.exporters.word_builder import build_docx_from_toc
from grantflow.tests.exporters_helpers import (
    _sample_citations,
    _sample_critic_findings,
    _sample_quality_summary,
    _sample_review_comments,
)


def test_word_export_includes_citation_traceability_section():
    toc_draft = {
        "toc": {
            "brief": "Sample ToC brief",
            "objectives": [{"title": "Obj 1", "description": "Desc", "citation": "usaid_ads201"}],
        }
    }
    content = build_docx_from_toc(
        toc_draft,
        "usaid",
        citations=_sample_citations(),
        critic_findings=_sample_critic_findings(),
        review_comments=_sample_review_comments(),
        quality_summary=_sample_quality_summary(),
    )
    doc = Document(BytesIO(content))
    text = "\n".join(p.text for p in doc.paragraphs)
    assert "Citation Traceability" in text
    assert "USAID ADS 201 p.12" in text
    assert "Official indicator guidance excerpt" in text
    assert "conf 0.82" in text
    assert "level outcome" in text
    assert "path toc.development_objectives[0].description" in text
    assert "evidence PMP and indicator reference evidence" in text
    assert "Review hint: Use this citation to support the indicator row in the USAID monitoring package." in text
    assert "Critic Findings" in text
    assert "ToC Schema Invalid" in text
    assert "Review Comments" in text
    assert "Adjusted objective wording and assumptions." in text
    assert "Quality Summary" in text
    assert "Quality score: 8.5" in text
    assert "Fatal flaw count: 1" in text
    assert "Review Readiness" in text
    assert "Open critic findings: 1" in text
    assert "Acknowledged critic findings: 1" in text
    assert "Resolved critic findings: 0" in text
    assert "High-severity open findings: 1" in text
    assert "Open review comments: 0" in text
    assert "Fallback/strategy citations: 1" in text
    assert "Acknowledged review comments: 0" in text
    assert "Resolved review comments: 1" in text
    assert "Critic finding resolution rate:" in text
    assert "Critic finding acknowledgment rate:" in text
    assert "Reviewer workflow resolution rate:" in text
    assert "Reviewer workflow acknowledgment rate:" in text
    assert "Next primary review action: resolve_finding" in text
    assert "Finding ack queue: 0" in text
    assert "Finding resolve queue: 1" in text
    assert "Comment ack queue: 0" in text
    assert "Comment resolve queue: 0" in text
    assert "Comment reopen queue: 1" in text
    assert "Comment threads aged 3-7d:" in text
    assert "Top reviewer action 1:" in text
    assert "Reviewer next step:" in text
    assert "Recommended action:" in text


def test_word_export_hides_diagnostics_when_disabled():
    toc_draft = {"toc": {"project_goal": "Improve service quality."}}
    content = build_docx_from_toc(
        toc_draft,
        "usaid",
        citations=_sample_citations(),
        critic_findings=_sample_critic_findings(),
        review_comments=_sample_review_comments(),
        quality_summary=_sample_quality_summary(),
        include_diagnostics=False,
    )
    text = "\n".join(p.text for p in Document(BytesIO(content)).paragraphs)
    assert "Template Profile" not in text
    assert "Export Contract Check" not in text
    assert "Quality Summary" not in text
    assert "Review Readiness" not in text
    assert "Citation Traceability" not in text
    assert "Critic Findings" not in text
    assert "Generated by: GrantFlow" not in text
    assert "USAID Results Framework" in text
