import json
import os
import urllib.request

QUESTION = {
    "type": "choice",
    "instructions": "What kind of issue is this?",
    "criteria": {
        "bug": "Something is broken",
        "feature": "A request for something new",
        "docs": "The documentation is wrong or missing",
        "question": "A question about how to use it",
        "other": "Anything else"
    }
}
# One JSON object per line: state and the answer chosen by a person.
with open("issue-triage-labels.jsonl", encoding="utf-8") as source:
    rows = [json.loads(line) for line in source if line.strip()]
examples = [
    {"request": {"state": row["state"], "questions": {"kind": QUESTION}},
     "answer": row["answer"]}
    for row in rows
]
body = {"id": "issue-triage", "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))
