"""Create the real GitLab repository used by the diff-order scenario.
Requires an authenticated glab and git credential helper. Uses synthetic data only.
Run in an empty directory. Update the scenario remote if the project name changes.
"""
import json
import subprocess
from pathlib import Path

def run(*args):
    return subprocess.check_output(args, text=True).strip()
name = 'factory-diff-order-44623-evidence-1'
project = json.loads(run('glab', 'api', '-X', 'POST', 'projects', '-f', f'name={name}', '-f', 'visibility=private'))
def git(*args):
    return run('git', *args)
for file, content in {'src/z.ts': 'export const z = 1;\n', 'src/other.ts': 'export const other = 1;\n', 'zzz/z.ts': 'export const unrelated = 1;\n'}.items():
    p = Path(file)
    p.parent.mkdir(parents=True, exist_ok=True)
    p.write_text(content)
git('init', '-b', 'main')
def commit(message):
    git('add', '.')
    git('-c', 'user.name=Scenario', '-c', 'user.email=scenario@example.invalid', 'commit', '-m', message)
commit('Initial synthetic repository')
git('remote', 'add', 'origin', project['http_url_to_repo'])
git('push', 'origin', 'main')
for branch, content, title in [
    ('repro-basename', 'import { z } from "./z";\nexport const a = z;\n', 'Relative import with unchanged target'),
    ('repro-control', 'export const a = 1;\n', 'Control without import'),
]:
    git('checkout', '-b', branch, 'main')
    Path('src/a.ts').write_text(content)
    Path('zzz/z.ts').write_text('export const unrelated = 2;\n')
    commit('Add importer and update unrelated file')
    git('push', 'origin', branch)
    mr = json.loads(run('glab', 'api', '-X', 'POST', f'projects/{project["id"]}/merge_requests', '-f', f'source_branch={branch}', '-f', 'target_branch=main', '-f', f'title={title}'))
    print(mr['web_url'])
