"""Render human-readable captures from the measurement driver's unchanged JSON."""
import json
import pathlib
import sys
source = pathlib.Path(sys.argv[1])
data = json.loads(source.read_text())
for side, label in [('base', 'before'), ('head', 'after')]:
    stats = data[side]
    rows = [s for s in data['samples'] if s['side'] == side]
    lines = [f'{label.upper()}: real T3 Code server branch lookup',
             f"Commit: {stats['sha']}",
             f"Environment: {data['platform']}, {data['architecture']}, Node {data['runtime']}",
             f"Fixture: {data['fixture']}",
             f"Method: {data['measurement']}", '',
             'One warm-up excluded; every measured run retained.',
             'Round    Lookup (ms)    Git processes    Result']
    lines += [f"{r['round']:>5}    {r['durationMs']:>11.2f}    {r['gitCount']:>13}    {json.dumps(r['result'])}" for r in rows]
    lines += ['', f"Median: {stats['medianMs']:.2f} ms",
              f"Range: {stats['minMs']:.2f}-{stats['maxMs']:.2f} ms",
              f"Full spread (max-min): {stats['spreadMs']:.2f} ms", '',
              'Git commands (same ordered sequence in every retained run):']
    first = rows[0]['commands']
    assert all(r['commands'] == first for r in rows), 'Command sequences differ'
    lines += [f'{i:>2}. git {command}' for i, command in enumerate(first, 1)]
    for command in ['remote', 'config --get branch.feature/local-only.remote', 'config --get-all remote.origin.url']:
        lines.append(f'Reads of [{command}]: {first.count(command)}')
    lines += ['', 'Lookup result: null in every run (no pull request for the unpublished local branch).',
              'Scope: lookup within the real startup sweep; excludes total server startup time.',
              'Synthetic data only. No tests or simulated git responses used.']
    target = source.parent / f'{label}.txt'
    target.write_text('\n'.join(lines) + '\n')
    print(target)
