"""meshnet-gateway CLI entry point."""

import argparse
import sys


def main() -> None:
    parser = argparse.ArgumentParser(
        prog="meshnet-gateway",
        description="Distributed Inference Network HTTP gateway",
    )
    subparsers = parser.add_subparsers(dest="command")

    start_cmd = subparsers.add_parser("start", help="Start the gateway server")
    route_group = start_cmd.add_mutually_exclusive_group()
    route_group.add_argument("--tracker", default="http://localhost:8080", help="Tracker URL")
    route_group.add_argument(
        "--node-url",
        help="Single node URL for local smoke tests instead of tracker routing",
    )
    start_cmd.add_argument("--port", type=int, default=8080, help="Port to listen on")

    args = parser.parse_args()

    if args.command == "start":
        from meshnet_gateway.server import GatewayServer

        if args.node_url:
            gateway = GatewayServer(node_url=args.node_url, port=args.port)
            routing_label = f"Routing to node URL: {args.node_url}"
        else:
            gateway = GatewayServer(tracker_url=args.tracker, port=args.port)
            routing_label = f"Tracker URL: {args.tracker}"
        port = gateway.start()
        print(f"meshnet-gateway listening on port {port}")
        print(routing_label)
        try:
            import time
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            gateway.stop()
            sys.exit(0)
    else:
        parser.print_help()


if __name__ == "__main__":
    main()
