"""Create the synthetic GitLab repository and open MR used by the scenario.
Requires an authenticated glab CLI. Run once, then update the scenario's project ID
and repository path from the output before comparing commits.
"""
import json
import subprocess
import time

def api(method, endpoint, **fields):
    args = ["glab", "api", "-X", method, endpoint]
    for key, value in fields.items():
        args += ["-f", f"{key}={value}"]
    return json.loads(subprocess.check_output(args, text=True))

name = f"t3-proof-29438-{int(time.time())}"
project = api("POST", "projects", name=name, visibility="public", initialize_with_readme="true")
project_id = project["id"]
api("POST", f"projects/{project_id}/repository/branches", branch="proof", ref="main")
api("POST", f"projects/{project_id}/repository/files/proof.txt", branch="proof", content="Synthetic evidence fixture", commit_message="Add proof fixture")
mr = api("POST", f"projects/{project_id}/merge_requests", source_branch="proof", target_branch="main", title="Factory repro MR")
print(json.dumps({"projectId": project_id, "repository": project["path_with_namespace"], "mr": mr["iid"]}))
# After running the comparison, close the MR and delete the synthetic project:
# glab api -X PUT projects/<id>/merge_requests/1 -f state_event=close
# glab api -X DELETE projects/<id>
