"""CLI entry point for susatest-agent."""
from __future__ import annotations

import argparse
import asyncio
import sys


def main():
    parser = argparse.ArgumentParser(
        prog="susatest-agent",
        description="Connect your local Android device to SUSATest cloud platform",
    )
    subparsers = parser.add_subparsers(dest="command")

    # connect command
    connect_parser = subparsers.add_parser("connect", help="Connect to SUSATest server")
    connect_parser.add_argument("--api-key", required=True, help="Your SUSATest API key (susa_xxxx)")
    connect_parser.add_argument("--server", default="wss://www.susatest.com", help="Server URL (default: wss://www.susatest.com)")
    connect_parser.add_argument("--device", default=None, help="Specific device serial (default: all connected devices)")
    connect_parser.add_argument("--adb-path", default="adb", help="Path to adb binary (default: adb)")

    # status command
    subparsers.add_parser("status", help="Show connection status")

    args = parser.parse_args()

    if args.command == "connect":
        from .agent import run_agent
        try:
            asyncio.run(run_agent(
                api_key=args.api_key,
                server_url=args.server,
                device_serial=args.device,
                adb_path=args.adb_path,
            ))
        except KeyboardInterrupt:
            print("\n[susatest] Disconnected.")
            sys.exit(0)
    elif args.command == "status":
        print("[susatest] Status check not yet implemented. Use 'connect' to start.")
    else:
        parser.print_help()
        sys.exit(1)


if __name__ == "__main__":
    main()
