"""Simulation only: test the agent API from Linux without a Windows installation."""
import argparse
import json
import urllib.error
import urllib.request

parser = argparse.ArgumentParser()
parser.add_argument("--server", default="http://127.0.0.1:5088")
parser.add_argument("--code", required=True)
args = parser.parse_args()


def post(path, data, token=None):
    headers = {"Content-Type": "application/json"}
    if token:
        headers["Authorization"] = "Bearer " + token
    req = urllib.request.Request(args.server + path, json.dumps(data).encode(), headers, method="POST")
    with urllib.request.urlopen(req, timeout=15) as response:
        return json.load(response)


registration = post("/api/agent/enroll", {
    "code": args.code, "hostname": "RTAI-TESTE", "os": "Ambiente simulado",
    "agent_version": "simulator-0.1"
})
result = post("/api/agent/checkin", {
    "hostname": "RTAI-TESTE", "os": "Ambiente simulado",
    "agent_version": "simulator-0.1", "rt_protection": None, "av_enabled": None
}, registration["token"])
print("Cadastro e check-in OK:", registration["device_id"])
print("Resposta:", result)
