"""Cross-platform golden-prompt parity.

Every prompt mode both front-ends can assemble is compared byte-for-byte against
the committed golden files in ``assets/golden-prompts/`` (generated by the Swift
core with ``DIPLOMAT_GOLDEN_WRITE=1 swift run DiplomatCoreSmoke``). The Swift
smoke test asserts the SAME files, so Swift and Python can only drift from each
other by failing one CI job. The goldens are canonical: a mismatch here means
the Python builder is wrong.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from diplomat_app.audit import AuditConfig  # noqa: E402
from diplomat_app.conflicts import ConflictConfig  # noqa: E402
from diplomat_runtime.prtarget import PRTarget  # noqa: E402
from diplomat_runtime.review import ReviewConfig  # noqa: E402

GOLDEN_DIR = Path(__file__).resolve().parents[3] / "diplomat-core" / "assets" / "golden-prompts"

# The gh-authenticated viewer the goldens were generated with (see main.swift).
ME = "testuser"

# Mirrors main.swift's goldenModes list — same constructors, same arguments.
# (AuditConfig dropped its dead `me` field on BOTH platforms.)
GOLDEN_MODES: dict[str, object] = {
    "review-mine-max": lambda: ReviewConfig(depth="max", me=ME),
    "review-user-max": lambda: ReviewConfig(
        depth="max", target=PRTarget.SOMEONE, username="someuser", me=ME
    ),
    "review-single-unknown": lambda: ReviewConfig(
        depth="max", target=PRTarget.SPECIFIC, me=ME, specific_pr="337"
    ),
    "conflicts-mine": lambda: ConflictConfig(me=ME),
    "conflicts-user": lambda: ConflictConfig(
        target=PRTarget.SOMEONE, username="someuser", me=ME
    ),
    "conflicts-single": lambda: ConflictConfig(
        target=PRTarget.SPECIFIC, me=ME, specific_pr="337"
    ),
    "audit": lambda: AuditConfig(),
    "audit-issues": lambda: AuditConfig(fix_issues=True),
    "audit-prs": lambda: AuditConfig(open_prs=True),
    "audit-all": lambda: AuditConfig(fix_issues=True, open_prs=True),
}


def test_every_golden_file_is_covered():
    """A new golden mode on the Swift side must gain a Python twin here."""
    on_disk = {p.stem for p in GOLDEN_DIR.glob("*.txt")}
    assert on_disk == set(GOLDEN_MODES), (
        f"golden files {sorted(on_disk)} != modes under test {sorted(GOLDEN_MODES)}"
    )


@pytest.mark.parametrize("name", sorted(GOLDEN_MODES))
def test_prompt_matches_golden(name: str):
    golden_path = GOLDEN_DIR / f"{name}.txt"
    assert golden_path.is_file(), (
        f"missing golden {name}.txt — run DIPLOMAT_GOLDEN_WRITE=1 swift run "
        "DiplomatCoreSmoke"
    )
    golden = golden_path.read_bytes()
    assert golden, f"empty golden {name}.txt"
    prompt = GOLDEN_MODES[name]().build_prompt().encode("utf-8")
    assert prompt == golden, f"prompt {name} drifted from its golden file"
