"""Helper script for review playbook Robot Framework tests.

Used by robot/review_playbook.robot to verify the review playbook
document exists, parses correctly, and contains required sections.
"""

from __future__ import annotations

import sys
from pathlib import Path

PLAYBOOK_PATH = Path("docs/development/review_playbook.md")

REQUIRED_SECTIONS: list[str] = [
    "Priority Matrix",
    "PR Review Routing Table",
    "Focus Areas and Skip Rules",
    "Review SLA Guidance",
    "Architecture Review Checklist",
    "CLI Review Checklist",
    "DB Migration Review Checklist",
    "Security-Sensitive Change Checklists",
    "Required Test Matrix for Reviewers",
    "Acceptable vs Blocking Findings",
]

REQUIRED_NOX_SESSIONS: list[str] = [
    "lint",
    "typecheck",
    "unit_tests",
    "integration_tests",
    "coverage_report",
    "security_scan",
    "dead_code",
    "complexity",
    "benchmark",
]


def verify_file_exists() -> None:
    """Verify that the review playbook file exists."""
    if not PLAYBOOK_PATH.exists():
        print(
            f"ERROR: Review playbook not found at {PLAYBOOK_PATH}",
            file=sys.stderr,
        )
        sys.exit(1)
    print("review-playbook-exists-ok")


def verify_sections() -> None:
    """Verify that all required sections are present in the playbook."""
    if not PLAYBOOK_PATH.exists():
        print(
            f"ERROR: Review playbook not found at {PLAYBOOK_PATH}",
            file=sys.stderr,
        )
        sys.exit(1)

    content: str = PLAYBOOK_PATH.read_text(encoding="utf-8")
    missing: list[str] = []
    for section in REQUIRED_SECTIONS:
        if section not in content:
            missing.append(section)

    if missing:
        print(
            f"ERROR: Missing sections: {', '.join(missing)}",
            file=sys.stderr,
        )
        sys.exit(1)
    print("review-playbook-sections-ok")


def verify_nox_references() -> None:
    """Verify that the playbook references all required nox sessions."""
    if not PLAYBOOK_PATH.exists():
        print(
            f"ERROR: Review playbook not found at {PLAYBOOK_PATH}",
            file=sys.stderr,
        )
        sys.exit(1)

    content: str = PLAYBOOK_PATH.read_text(encoding="utf-8")
    missing: list[str] = []
    for session in REQUIRED_NOX_SESSIONS:
        nox_ref = f"nox -s {session}"
        if nox_ref not in content:
            missing.append(session)

    if missing:
        print(
            f"ERROR: Missing nox session references: {', '.join(missing)}",
            file=sys.stderr,
        )
        sys.exit(1)
    print("review-playbook-nox-ok")


def verify_severity_levels() -> None:
    """Verify that the playbook defines all severity levels P0-P3."""
    if not PLAYBOOK_PATH.exists():
        print(
            f"ERROR: Review playbook not found at {PLAYBOOK_PATH}",
            file=sys.stderr,
        )
        sys.exit(1)

    content: str = PLAYBOOK_PATH.read_text(encoding="utf-8")
    missing: list[str] = []
    for level in ["P0", "P1", "P2", "P3"]:
        if level not in content:
            missing.append(level)

    if missing:
        print(
            f"ERROR: Missing severity levels: {', '.join(missing)}",
            file=sys.stderr,
        )
        sys.exit(1)
    print("review-playbook-severity-ok")


def main() -> None:
    """Route to the appropriate verification function."""
    if len(sys.argv) < 2:
        print(
            "Usage: helper_review_playbook.py <command>",
            file=sys.stderr,
        )
        sys.exit(1)

    command: str = sys.argv[1]
    commands: dict[str, object] = {
        "verify-file-exists": verify_file_exists,
        "verify-sections": verify_sections,
        "verify-nox-references": verify_nox_references,
        "verify-severity-levels": verify_severity_levels,
    }

    handler = commands.get(command)
    if handler is None:
        print(f"Unknown command: {command}", file=sys.stderr)
        sys.exit(1)

    if callable(handler):
        handler()


if __name__ == "__main__":
    main()
