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

Each subcommand exercises the real DI path (no mocks) to reproduce bug #570.
The helper reports the **real** outcome: it exits 0 and prints the sentinel
when the operation succeeds (bug is fixed), and exits 1 when the bug is
still present.  The ``tdd_expected_fail_listener`` on the Robot side handles
pass/fail inversion while the bug remains open.
"""

from __future__ import annotations

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

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

from helper_tdd_session_di_common import runner, setup_real_di, teardown  # noqa: E402

from cleveragents.cli.commands.session import app as session_app  # noqa: E402

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


def create_di_error() -> None:
    """Invoke ``session create`` through the real DI path.

    Exits 0 with sentinel when the command succeeds (bug fixed).
    Exits 1 when the command fails (bug still present).
    """
    db_path = setup_real_di()
    try:
        result = runner.invoke(session_app, ["create"])
        if result.exit_code == 0:
            print("tdd-session-create-di-error-ok")
        else:
            print(
                f"session create failed with exit code {result.exit_code}",
                file=sys.stderr,
            )
            sys.exit(1)
    finally:
        teardown(db_path)


def create_actor() -> None:
    """Invoke ``session create --actor`` through the real DI path.

    Exits 0 with sentinel when the command succeeds (bug fixed).
    Exits 1 when the command fails (bug still present).
    """
    db_path = setup_real_di()
    try:
        result = runner.invoke(
            session_app,
            ["create", "--actor", "openai/gpt-4"],
        )
        if result.exit_code == 0:
            print("tdd-session-create-actor-ok")
        else:
            print(
                f"session create --actor failed with exit code {result.exit_code}",
                file=sys.stderr,
            )
            sys.exit(1)
    finally:
        teardown(db_path)


def create_json() -> None:
    """Invoke ``session create --format json`` through the real DI path.

    Exits 0 with sentinel when the command succeeds (bug fixed).
    Exits 1 when the command fails (bug still present).
    """
    db_path = setup_real_di()
    try:
        result = runner.invoke(session_app, ["create", "--format", "json"])
        if result.exit_code == 0:
            print("tdd-session-create-json-ok")
        else:
            print(
                "session create --format json failed "
                f"with exit code {result.exit_code}",
                file=sys.stderr,
            )
            sys.exit(1)
    finally:
        teardown(db_path)


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

_COMMANDS: dict[str, Callable[[], None]] = {
    "create-di-error": create_di_error,
    "create-actor": create_actor,
    "create-json": create_json,
}

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()
