from __future__ import annotations

import time


def run(*, server: str = "http://127.0.0.1:18765", device_token: str = "", once: bool = False) -> int:
    print(f"[bridge-client] 连接 {server}（MVP stub — WS relay 待扩展）")
    if device_token:
        print(f"[bridge-client] device_token={device_token[:8]}…")
    else:
        print("[bridge-client] 未提供 device_token；请先 POST /v1/bridge/pairing 配对")

    if once:
        return 0

    try:
        while True:
            time.sleep(30)
            print("[bridge-client] heartbeat …")
    except KeyboardInterrupt:
        print("\n[bridge-client] 退出")
    return 0


def main(argv: list[str] | None = None) -> int:
    import argparse

    parser = argparse.ArgumentParser(prog="niannian-bridge-client")
    parser.add_argument("--server", default="http://127.0.0.1:18765")
    parser.add_argument("--device-token", default="")
    parser.add_argument("--once", action="store_true")
    args = parser.parse_args(argv)
    return run(server=args.server, device_token=args.device_token, once=args.once)


if __name__ == "__main__":
    raise SystemExit(main())
