import json
import os
import urllib.request

QUESTION = {
    "type": "noul",
    "instructions": "Is this message toxic: insulting, hateful, threatening or sexually explicit?"
}
# One JSON object per line: state and the answer chosen by a person.
with open("guardrails-labels.jsonl", encoding="utf-8") as source:
    rows = [json.loads(line) for line in source if line.strip()]
examples = [
    {"request": {"state": row["state"], "questions": {"toxic": QUESTION}},
     "answer": row["answer"]}
    for row in rows
]
body = {"id": "guardrails", "examples": examples, "certify": {"alpha": 0.05}}
base_url = os.environ.get("DECISIO_BASE_URL", "http://127.0.0.1:8000").rstrip("/")
request = urllib.request.Request(
    base_url + "/v1/tasks", data=json.dumps(body).encode(),
    headers={"Content-Type": "application/json"},
)
# Registration reads all your examples; allow more time than an ordinary decision.
with urllib.request.urlopen(request, timeout=300) as response:
    record = json.load(response)
print(json.dumps(record.get("certificate"), indent=2))
