#!/usr/bin/env python3
"""Send one OpenAI-compatible chat request to a meshnet HTTP endpoint."""

from __future__ import annotations

import argparse
import json
import urllib.request


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--url", default="http://localhost:8080", help="Tracker or gateway base URL")
    parser.add_argument("--model", default="qwen2.5-0.5b-instruct")
    parser.add_argument(
        "--prompt",
        default="What is 7 times 8? Answer in one word.",
    )
    parser.add_argument("--max-tokens", type=int, default=32)
    args = parser.parse_args()

    payload = {
        "model": args.model,
        "messages": [{"role": "user", "content": args.prompt}],
        "max_tokens": args.max_tokens,
        "stream": False,
    }
    request = urllib.request.Request(
        f"{args.url.rstrip('/')}/v1/chat/completions",
        data=json.dumps(payload).encode(),
        headers={"Content-Type": "application/json"},
        method="POST",
    )
    with urllib.request.urlopen(request, timeout=120) as response:
        body = json.loads(response.read())
    print(json.dumps(body, indent=2))
    return 0


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