"""Helper script for tdd_plan_apply_yes_flag.robot smoke tests.

Each subcommand exercises the real CLI path (no mocks) to reproduce bug #932.
The ``apply`` command is invoked with ``--yes`` or ``-y`` to verify
that the flag is recognised by the CLI framework.

The helper reports the **real** outcome: it exits 0 and prints the sentinel
when the flag is accepted (bug is fixed), and exits 1 when the flag is
rejected (bug still present).  The bug is now fixed and the
``@tdd_expected_fail`` tag has been removed; these tests serve as permanent
regression guards.
"""

from __future__ import annotations

import sys
from collections.abc import Callable
from pathlib import Path
from typing import NoReturn

# Ensure local source tree is importable.
_ROOT = Path(__file__).resolve().parents[1]
_SRC = str(_ROOT / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

from typer.testing import CliRunner  # noqa: E402

from cleveragents.cli.commands.plan import app as plan_app  # noqa: E402

runner = CliRunner()


def _fail(message: str) -> NoReturn:
    """Print an error message to stderr and exit with code 1."""
    print(message, file=sys.stderr)
    sys.exit(1)


# ---------------------------------------------------------------------------
# Subcommands
# ---------------------------------------------------------------------------


def check_yes_long() -> None:
    """Invoke ``apply --yes DUMMY`` and verify the flag is accepted.

    Exits 0 with sentinel when ``--yes`` is recognised (bug fixed).
    Exits 1 when Typer rejects the flag (bug still present).
    """
    result = runner.invoke(plan_app, ["apply", "--yes", "DUMMY_PLAN_ID"])
    if "No such option" in result.output or result.exit_code == 2:
        _fail(
            f"apply rejected --yes flag.\n"
            f"Exit code: {result.exit_code}\n"
            f"Output: {result.output}"
        )
    print("tdd-plan-apply-yes-flag-long-ok")


def check_yes_short() -> None:
    """Invoke ``apply -y DUMMY`` and verify the flag is accepted.

    Exits 0 with sentinel when ``-y`` is recognised (bug fixed).
    Exits 1 when Typer rejects the flag (bug still present).
    """
    result = runner.invoke(plan_app, ["apply", "-y", "DUMMY_PLAN_ID"])
    if "No such option" in result.output or result.exit_code == 2:
        _fail(
            f"apply rejected -y flag.\n"
            f"Exit code: {result.exit_code}\n"
            f"Output: {result.output}"
        )
    print("tdd-plan-apply-yes-flag-short-ok")


# ---------------------------------------------------------------------------
# Dispatcher
# ---------------------------------------------------------------------------

_COMMANDS: dict[str, Callable[[], None]] = {
    "check-yes-long": check_yes_long,
    "check-yes-short": check_yes_short,
}

if __name__ == "__main__":
    if len(sys.argv) < 2 or sys.argv[1] not in _COMMANDS:
        print(
            f"Usage: {sys.argv[0]} <{'|'.join(_COMMANDS)}>",
            file=sys.stderr,
        )
        sys.exit(1)
    cmd = _COMMANDS[sys.argv[1]]
    cmd()
