#!/usr/bin/env -S uv run --script
"""Smoke test for hybrid z-machine interpreter with Zork 1."""

import argparse
import sys
import traceback
from pathlib import Path


def main():
    parser = argparse.ArgumentParser(description="Run Zork 1 in Z-machine interpreter")
    parser.add_argument("--debug", action="store_true", help="Enable debug mode")
    args = parser.parse_args()

    # Add src to path so we can import mudlib
    project_root = Path(__file__).parent.parent
    sys.path.insert(0, str(project_root / "src"))

    from mudlib.zmachine import ZMachine
    from mudlib.zmachine.trivialzui import create_zui
    from mudlib.zmachine.zcpu import (
        ZCpuError,
        ZCpuNotImplemented,
        ZCpuQuit,
        ZCpuRestart,
    )

    print("=" * 60)
    print("Z-Machine Interpreter Test: Zork 1")
    print("=" * 60)
    print()
    print("Loading story file: content/stories/zork1.z3")

    story_path = project_root / "content" / "stories" / "zork1.z3"

    if not story_path.exists():
        print(f"ERROR: Story file not found at {story_path}")
        sys.exit(1)

    with open(story_path, "rb") as f:
        story_bytes = f.read()

    print(f"Loaded {len(story_bytes)} bytes")
    print()
    print("Creating ZUI and ZMachine instance...")
    if args.debug:
        print("Debug mode: ENABLED")

    ui = create_zui()
    zmachine = ZMachine(story_bytes, ui, debugmode=args.debug)

    print("Starting interpreter...")
    print("(Press Ctrl+C to exit)")
    print()
    print("-" * 60)
    print()

    try:
        zmachine.run()
        print()
        print("-" * 60)
        print("Interpreter exited normally.")

    except ZCpuQuit:
        print()
        print("-" * 60)
        print("Game quit via QUIT opcode.")

    except ZCpuRestart:
        print()
        print("-" * 60)
        print("Game requested restart via RESTART opcode.")
        print("(Restart not implemented in this test script)")

    except ZCpuNotImplemented as e:
        print()
        print("-" * 60)
        print("FAILED: Unimplemented opcode encountered")
        print(f"Error: {e}")
        print()
        traceback.print_exc()
        sys.exit(1)

    except ZCpuError as e:
        print()
        print("-" * 60)
        print("FAILED: Z-Machine CPU error")
        print(f"Error: {e}")
        print()
        traceback.print_exc()
        sys.exit(1)

    except KeyboardInterrupt:
        print()
        print()
        print("-" * 60)
        print("Interrupted by user (Ctrl+C)")

    except Exception as e:
        print()
        print("-" * 60)
        print("FAILED: Unexpected error")
        print(f"Error: {e}")
        print()
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()
