"""Create synthetic GitLab notes for the alert excerpt scenario; requires authenticated glab.
The scenario uses the returned remote and URLs. Delete the synthetic project after proof.
"""
import json
import subprocess
import time


def api(path, *args):
    return json.loads(subprocess.check_output(['glab', 'api', path, *args]))


project = api('projects', '-X', 'POST', '-f', f'name=alert-excerpt-proof-{int(time.time())}',
              '-f', 'visibility=private', '-f', 'initialize_with_readme=true')
project_id = project['id']
api(f'projects/{project_id}/repository/branches', '-X', 'POST', '-f',
    'branch=excerpt-proof', '-f', 'ref=main')
api(f'projects/{project_id}/repository/files/proof.txt', '-X', 'POST', '-f',
    'branch=excerpt-proof', '-f', 'content=Synthetic alert excerpt proof', '-f',
    'commit_message=Add proof fixture')
mr = api(f'projects/{project_id}/merge_requests', '-X', 'POST', '-f',
         'source_branch=excerpt-proof', '-f', 'target_branch=main', '-f',
         'title=Alert marker excerpt proof')
urls = []
for body in ['Keep `[!NOTE]` in the file.', '> [!NOTE]\n> Real alert.', 'Keep [!WARNING] in prose.']:
    note = api(f'projects/{project_id}/merge_requests/{mr["iid"]}/notes', '-X', 'POST',
               '-f', 'body=' + body)
    urls.append(mr['web_url'] + '#note_' + str(note['id']))
print(json.dumps({'projectId': project_id, 'remote': project['http_url_to_repo'], 'urls': urls}))
