"""BUG C: analyst "artifacts ready" comment under the status-only model.

The comment must ask for the **Approved** status (not the obsolete
":approved:" reaction, not moving back to "In Progress") and link only the
docs that actually exist in the worktree.
"""

import os
import tempfile

os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")


def test_analyst_comment_asks_approved_with_links(monkeypatch, tmp_path):
    from src import stage_engine as SE

    # Worktree with only SOME of the candidate docs present.
    wt = tmp_path / "wt"
    docs = wt / "docs" / "work-items" / "ET-011"
    docs.mkdir(parents=True)
    for fname in ("00-business-request.md", "01-brd.md", "02-trz.md",
                  "03-acceptance-criteria.md", "04-test-plan.yaml"):
        (docs / fname).write_text("x")
    # 04b-ui-test-cases.md intentionally absent -> must NOT be linked

    monkeypatch.setattr(SE, "get_worktree_path", lambda repo, branch: str(wt))
    # public URL set -> links must be built from it (not gitea_url)
    monkeypatch.setattr(SE.settings, "gitea_url", "http://localhost:3000")
    monkeypatch.setattr(SE.settings, "gitea_public_url", "https://git.mva154.duckdns.org")
    monkeypatch.setattr(SE.settings, "gitea_owner", "admin")

    html = SE._build_analyst_ready_comment(
        "enduro-trails", "ET-011", "feature/ET-011-gpx-upload-feature"
    )

    # text asks for the Approved STATUS, not the obsolete mechanisms
    assert "Approved" in html
    assert ":approved:" not in html
    assert "In Progress" not in html
    assert "Rejected" in html
    # clickable links to docs that ACTUALLY exist
    assert "<a href=" in html
    base = ("https://git.mva154.duckdns.org/admin/enduro-trails/src/branch/"
            "feature/ET-011-gpx-upload-feature/docs/work-items/ET-011/")
    assert base + "01-brd.md" in html
    assert base + "04-test-plan.yaml" in html
    # the missing file is NOT invented
    assert "04b-ui-test-cases.md" not in html
    # internal git url must NOT appear in clickable links
    assert "localhost:3000" not in html


def test_analyst_comment_falls_back_to_gitea_url(monkeypatch, tmp_path):
    """When gitea_public_url is empty, links fall back to gitea_url."""
    from src import stage_engine as SE

    wt = tmp_path / "wt"
    docs = wt / "docs" / "work-items" / "ET-011"
    docs.mkdir(parents=True)
    (docs / "01-brd.md").write_text("x")

    monkeypatch.setattr(SE, "get_worktree_path", lambda repo, branch: str(wt))
    monkeypatch.setattr(SE.settings, "gitea_url", "http://localhost:3000")
    monkeypatch.setattr(SE.settings, "gitea_public_url", "")
    monkeypatch.setattr(SE.settings, "gitea_owner", "admin")

    html = SE._build_analyst_ready_comment(
        "enduro-trails", "ET-011", "feature/ET-011-gpx-upload-feature"
    )

    base = ("http://localhost:3000/admin/enduro-trails/src/branch/"
            "feature/ET-011-gpx-upload-feature/docs/work-items/ET-011/")
    assert base + "01-brd.md" in html
