"""Create real GitLab approval/withdrawal notes for the accompanying app scenario.
Requires glab authenticated to gitlab.com. The project is private and synthetic.
Use its returned HTTPS URL as the scenario's origin. Delete the project after proof.
"""
import json
import subprocess
import time

def api(path, *args):
    result = subprocess.run(["glab", "api", path, *args], text=True, capture_output=True, check=True)
    return json.loads(result.stdout) if result.stdout.strip() else None

project = api("projects", "-X", "POST", "-f", f"name=t3-approval-proof-{int(time.time())}",
              "-f", "visibility=private", "-f", "initialize_with_readme=true", "-f", "default_branch=main")
root = f"projects/{project['id']}/"
api(root + "repository/branches", "-X", "POST", "-f", "branch=feature", "-f", "ref=main")
api(root + "repository/files/a.txt", "-X", "POST", "-f", "branch=feature", "-f", "content=a", "-f", "commit_message=add a")
api(root + "merge_requests", "-X", "POST", "-f", "source_branch=feature", "-f", "target_branch=main", "-f", "title=Repro MR")
api(root + "merge_requests/1/approve", "-X", "POST")
api(root + "merge_requests/1/unapprove", "-X", "POST")
api(root + "merge_requests/1/notes", "-X", "POST", "-f", "body=A normal comment")
print(project["http_url_to_repo"])
print(json.dumps([{ "system": n["system"], "body": n["body"] } for n in api(root + "merge_requests/1/notes")]))
