import json,subprocess,base64,sys
from pathlib import Path
root=Path(sys.argv[1]); root.mkdir(parents=True, exist_ok=True)
(root/'project.json').write_bytes(subprocess.check_output(['glab','api','-X','POST','projects','-f','name=attachment-proof-43815','-f','visibility=public','-f','initialize_with_readme=true']))
p=json.loads((root/'project.json').read_text()); pid=p['id']
def api(path,data):
 f=root/'payload.json'; f.write_text(json.dumps(data)); return json.loads(subprocess.check_output(['glab','api','-X','POST',f'projects/{pid}/'+path,'--input',str(f)]))
subprocess.run(['convert','-size','640x360','xc:#216A8A','-fill','white','-gravity','center','-pointsize','38','-annotate','0','Attachment preview',str(root/'image.png')],check=True)
image=base64.b64encode((root/'image.png').read_bytes()).decode()
api('repository/commits',{'branch':'main','commit_message':'Add synthetic attachment images','actions':[{'action':'create','file_path':name,'encoding':'base64','content':image} for name in ['My Screenshot.png','café.png','100%.png']]})
api('repository/branches',{'branch':'attachment-demo','ref':'main'})
api('repository/commits',{'branch':'attachment-demo','commit_message':'Attachment example','actions':[{'action':'create','file_path':'example.txt','content':'Synthetic attachment example'}]})
raw=p['web_url']+'/-/raw/main/'
mr=api('merge_requests',{'source_branch':'attachment-demo','target_branch':'main','title':'Readable attachment filenames','description':'Screenshots attached below.\n\n'+raw+'My%20Screenshot.png\n\n'+raw+'caf%C3%A9.png\n\n'+raw+'100%.png'})
(root/'seed-result.json').write_text(json.dumps({'projectId':pid,'remote':p['http_url_to_repo'],'mr':mr['web_url']}))
print(json.dumps({'projectId':pid,'remote':p['http_url_to_repo'],'mr':mr['web_url']}))
