"""Guard command helpers for Robot TDD listener validation fixtures."""

from __future__ import annotations

import sys
from collections.abc import Callable

from _tdd_fixture_runner import run_fixture, run_fixture_dryrun

__all__ = [
    "GUARD_COMMANDS",
    "cmd_dry_run_guard",
    "cmd_infra_error_guard",
    "cmd_setup_error_guard",
    "cmd_teardown_error_guard",
]


def cmd_setup_error_guard() -> int:
    """Verify setup error guard prevents inversion."""
    status, message = run_fixture("tdd_expected_fail_setup_error")
    if status == "FAIL" and "Setup failed" in message:
        print("tdd-setup-error-guard-ok")
        return 0
    print(
        f"FAIL: Expected FAIL with 'Setup failed' message but got "
        f"{status}. Message: {message}",
        file=sys.stderr,
    )
    return 1


def cmd_infra_error_guard() -> int:
    """Verify infrastructure error guard prevents inversion."""
    status, message = run_fixture("tdd_expected_fail_infra_error")
    if status == "FAIL" and "No keyword with name" in message:
        print("tdd-infra-error-guard-ok")
        return 0
    print(
        f"FAIL: Expected FAIL with 'No keyword with name' message but "
        f"got {status}. Message: {message}",
        file=sys.stderr,
    )
    return 1


def cmd_teardown_error_guard() -> int:
    """Verify teardown error guard prevents inversion."""
    status, message = run_fixture("tdd_expected_fail_teardown_error")
    if status == "FAIL" and "Teardown failed" in message:
        print("tdd-teardown-error-guard-ok")
        return 0
    print(
        f"FAIL: Expected FAIL with 'Teardown failed' message but got "
        f"{status}. Message: {message}",
        file=sys.stderr,
    )
    return 1


def cmd_dry_run_guard() -> int:
    """Verify dry-run guard prevents inversion."""
    status, message = run_fixture_dryrun("tdd_expected_fail_fails")
    if status == "PASS":
        print("tdd-dry-run-guard-ok")
        return 0
    print(
        f"FAIL: Expected PASS in dry-run mode but got {status}. Message: {message}",
        file=sys.stderr,
    )
    return 1


GUARD_COMMANDS: dict[str, Callable[[], int]] = {
    "setup-error-guard": cmd_setup_error_guard,
    "infra-error-guard": cmd_infra_error_guard,
    "teardown-error-guard": cmd_teardown_error_guard,
    "dry-run-guard": cmd_dry_run_guard,
}
