#!/usr/bin/env python3
"""Fake ``qwen -p`` for the conformance suite.

``qwen -p`` has no structured output protocol -- it prints plain assistant
text. This fake reads a JSON events spec from ``$NVSH_FAKE_EVENTS`` (see
``tests/_fake_adapters.py``) and emits plain lines, using a ``[status] ``
prefix for status-kind events (the heuristic ``nvsh.agent.qwen.QwenAgent``
recognizes for verbose/tool-status output). Errors go to stderr with a
non-zero exit, matching a real CLI failure.
"""

from __future__ import annotations

import json
import os
import sys


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":
            print(f"[status] {event.get('text', '')}")
            sys.stdout.flush()
        elif kind == "text_delta":
            print(event.get("text", ""))
            sys.stdout.flush()
        elif kind == "error":
            print(event.get("error", ""), file=sys.stderr)
            sys.stderr.flush()
            return 1
        elif kind == "done":
            return 0
    return 0


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