#!/usr/bin/env python3
"""Fake ``qwen --output-format stream-json --approval-mode plan -p ...``.

# recorded-from: qwen 0.23.3

Reads a JSON events spec from ``$NVSH_FAKE_EVENTS`` (a list of
``{"kind": ..., "text": ..., "error": ...}`` dicts -- see
``tests/_fake_adapters.py``) and emits envelope lines shaped like a real
``qwen --output-format stream-json --approval-mode plan -p "<prompt>"``
run (verified locally against qwen 0.23.3; home paths and the session id
from that run are redacted/replaced with placeholders here), so
``nvsh.agent.qwen.QwenAgent`` can be exercised end to end without a real
``qwen`` binary on PATH.

The real CLI's ``system``/``init`` line carries a lot more (``tools``,
``mcp_servers``, ``cwd``, ...) than this fake reproduces -- only the
``type``/``subtype`` shape ``QwenAgent._parse_line`` actually reads is kept,
plus enough surrounding fields (``uuid``, ``session_id``) to look like a
real line to anything eyeballing a fixture. Thinking/tool_use/tool_result
content parts are exercised directly by ``tests/test_agent_qwen.py``, not
through this generic events file -- ``tests/_fake_adapters.py`` only
serializes ``kind``/``text``/``error`` (no ``tool``/``args``), so this fake
sticks to the four kinds the shared conformance suite scripts: ``status``,
``text_delta``, ``error`` and ``done``.
"""

from __future__ import annotations

import json
import os
import sys

_SESSION_ID = "00000000-0000-4000-8000-000000000000"


def main() -> int:
    events_path = os.environ.get("NVSH_FAKE_EVENTS")
    events = json.loads(open(events_path, encoding="utf-8").read()) if events_path else []

    for event in events:
        kind = event.get("kind")
        if kind == "status":
            line = {
                "type": "system",
                "subtype": event.get("text", "") or "init",
                "uuid": _SESSION_ID,
                "session_id": _SESSION_ID,
                "qwen_code_version": "0.23.3",
            }
        elif kind == "text_delta":
            line = {
                "type": "assistant",
                "uuid": _SESSION_ID,
                "session_id": _SESSION_ID,
                "message": {
                    "role": "assistant",
                    "content": [{"type": "text", "text": event.get("text", "")}],
                },
            }
        elif kind == "error":
            print(
                json.dumps(
                    {
                        "type": "result",
                        "subtype": "error",
                        "is_error": True,
                        "session_id": _SESSION_ID,
                        "result": event.get("error", ""),
                    }
                )
            )
            sys.stdout.flush()
            return 1
        elif kind == "done":
            print(
                json.dumps(
                    {
                        "type": "result",
                        "subtype": "success",
                        "is_error": False,
                        "session_id": _SESSION_ID,
                    }
                )
            )
            sys.stdout.flush()
            return 0
        else:
            continue
        print(json.dumps(line))
        sys.stdout.flush()
    return 0


if __name__ == "__main__":
    sys.exit(main())
