"""Tests for Jira ADF comment formatting."""

from __future__ import annotations

from packages.artifacts.design import DesignArtifact, MetadataPlanItem
from packages.integrations.jira import text_to_adf


def test_text_to_adf_heading_lists_links_and_labels():
    body = (
        "## Agent Nova design ready for review\n"
        "\n"
        "## Confluence design doc\n"
        "https://example.atlassian.net/wiki/spaces/ENG/pages/1\n"
        "\n"
        "Summary: Add Service_Level__c\n"
        "Approach: Custom field + validation\n"
        "\n"
        "Metadata plan:\n"
        "1. CustomField Service_Level__c (create) on Account\n"
        "2. ValidationRule Account_Service_Level_Required_For_Customer (create)\n"
        "\n"
        "Risks / escalations:\n"
        "- Page layouts out of allowlist\n"
        "\n"
        "Next step: reply with approve to continue, or reject to request design changes."
    )
    doc = text_to_adf(body)
    assert doc["type"] == "doc"
    types = [n["type"] for n in doc["content"]]
    assert "heading" in types
    assert "orderedList" in types
    assert "bulletList" in types
    assert "paragraph" in types

    # Clickable Confluence URL
    link_nodes = [
        n
        for block in doc["content"]
        for n in (block.get("content") or [])
        if n.get("type") == "text" and any(m.get("type") == "link" for m in n.get("marks") or [])
    ]
    assert any("atlassian.net/wiki" in n["text"] for n in link_nodes)

    # Bold Summary label
    summary_para = next(
        b
        for b in doc["content"]
        if b["type"] == "paragraph"
        and any((n.get("text") or "").startswith("Summary:") for n in b.get("content") or [])
    )
    summary_label = summary_para["content"][0]
    assert summary_label["marks"] == [{"type": "strong"}]


def test_design_review_comment_body_is_plain_and_structured():
    design = DesignArtifact(
        jira_key="AN-30",
        summary="Add Service_Level__c",
        approach="Field + validation",
        metadata_plan=[
            MetadataPlanItem(
                metadata_type="CustomField",
                api_name="Service_Level__c",
                action="create",
                object="Account",
            )
        ],
        risks_or_escalations=["Page layouts out of allowlist"],
        confidence=0.9,
    )
    body = design.design_review_comment_body()
    assert "*Summary:*" not in body
    assert "`CustomField`" not in body
    assert "## Agent Nova design ready for review" in body
    assert "1. CustomField Service_Level__c" in body
    assert "Next step: reply with approve" in body
