from __future__ import annotations

from pathlib import Path

from skills._shared.python.skill_testing import assert_skill_contract, repo_root


def _skill_root() -> Path:
    return Path(__file__).resolve().parents[1]


def _skill_text() -> str:
    return (_skill_root() / "SKILL.md").read_text(encoding="utf-8")


def _template_text() -> str:
    return (_skill_root() / "assets" / "templates" / "qa-report-template.md").read_text(encoding="utf-8")


def _compact(text: str) -> str:
    return " ".join(text.split())


def test_workflows_qa_browser_qa_contract() -> None:
    assert_skill_contract(_skill_root())


def test_workflows_qa_browser_qa_is_canonical_and_scriptless() -> None:
    skill_root = _skill_root()
    text = _skill_text()

    assert not (skill_root / "scripts").exists()
    assert "Canonical browser-facing QA workflow" in text
    assert "None. This is a scriptless workflow skill" in text
    assert "`web-qa`: compatibility entrypoint" in text
    assert "route new browser-backed QA work to `browser-qa`" in text


def test_workflows_qa_browser_qa_documents_modes_and_intake() -> None:
    text = _skill_text()
    compact = _compact(text)

    for mode in ["report-only", "acceptance-gate", "fix-loop"]:
        assert mode in text

    assert "Static evidence: use `web-evidence` only when" in text
    assert "Report-only" in text and "do not edit files, commit" in text
    assert "Active browser checks require real browser work before success can be recorded" in text
    assert "Source: user request, plan task, issue report, release gate, CI/deploy preview" in text
    assert "Static evidence does not prove browser behavior" in compact


def test_workflows_qa_browser_qa_documents_drivers_and_artifacts() -> None:
    text = _skill_text()
    compact = _compact(text)

    for driver in ["Browser", "Chrome", "Playwright", "`agent-browser`", "`web-evidence`"]:
        assert driver in text

    assert "browser-session verify" in text
    assert "screenshots, DOM observations, console summaries, network summaries" in text
    assert "Keep Playwright artifacts under `out/playwright/`" in text
    assert "assets/templates/qa-report-template.md" in text
    assert "browser-session`: browser QA evidence ledger" in compact
    assert "not a browser driver" in compact


def test_workflows_qa_browser_qa_documents_redaction_and_fix_loop_boundaries() -> None:
    text = _skill_text()
    compact = _compact(text)

    for guardrail in [
        "Do not retain raw cookies, credentials, auth headers",
        "secret query strings",
        "storage dumps",
        "If only unsafe evidence is available, do not retain it",
        "test-first-evidence",
        "explicit waiver",
        "canary-check` may supplement the gate but cannot replace required tests",
        "stop and report blocked if the fix exceeds scope",
        "review-evidence",
    ]:
        assert guardrail in compact


def test_workflows_qa_browser_qa_is_listed_in_root_catalog() -> None:
    readme = (repo_root() / "README.md").read_text(encoding="utf-8")

    assert "[browser-qa](./skills/workflows/qa/browser-qa/)" in readme
    assert "Browser-facing QA workflow" in readme


def test_workflows_qa_browser_qa_report_template_covers_scoring_and_ship_readiness() -> None:
    text = _template_text()

    assert "# Browser QA Report: {TARGET_NAME}" in text
    assert "## Health Score: {SCORE}/100" in text
    assert "## Ship Readiness" in text
    assert "**PR Summary:**" in text

    for category in ["Console", "Links", "Visual", "Functional", "UX", "Performance", "Accessibility"]:
        assert f"| {category} |" in text


def test_workflows_qa_browser_qa_report_template_covers_evidence_boundaries() -> None:
    text = _template_text()

    for evidence in [
        "`{BROWSER_SESSION_JSON_PATH}`",
        "browser-session verify",
        "Before",
        "After",
        "Console summary",
        "Network summary",
        "Playwright output",
        "Static evidence",
        "screenshots/issue-NNN-before.png",
        "screenshots/issue-NNN-after.png",
    ]:
        assert evidence in text
