#!/usr/bin/env python3
"""Package synthetic evidence, excluding credentials and server state."""
import json
import re
import shutil
import subprocess
import zipfile
from pathlib import Path

ROOT = Path(__file__).parent
RUN = ROOT / 'run-2026-10-30T0757'
for source, target in [
    ('after-server-private.log', 'after-initial-server-redacted.log'),
    ('before-isolated-private.log', 'before-server-redacted.log'),
    ('after-isolated-private.log', 'after-server-redacted.log'),
]:
    content = (ROOT / source).read_text()
    content = re.sub(r'pairingUrl:\s+\S+', 'pairingUrl: [REDACTED]', content)
    content = re.sub(r'([#?&]token=)[^\s&]+', r'\1[REDACTED]', content)
    content = re.sub(r'/api/assets/[^/\s]+', '/api/assets/[SIGNED-PATH-REDACTED]', content)
    (ROOT / target).write_text(content)

shutil.copyfile(ROOT.parent / 'proof-audit-1/matrix.md', ROOT / 'matrix.md')
scripts = [
    'fixture.html', 'seed-prompt.txt', 'seed-driver.mjs', 'capture-driver.mjs',
    'open-panel-harness.js', 'guest-component-harness.js', 'generate-evidence.py',
    'package-evidence.py', 'rebuild.md', 'matrix.md',
]
logs = [
    'before-install.log', 'before-server-redacted.log', 'after-server-redacted.log',
    'after-initial-server-redacted.log', 'generate-evidence.log', 'generate-evidence-2.log',
]
files = [ROOT / name for name in scripts + logs]
files += sorted(path for path in RUN.iterdir() if path.suffix in {'.png', '.mp4', '.json'} and 'settle-probe' not in path.name)
archive = ROOT / 'raw-capture-history.zip'
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as bundle:
    for path in files:
        bundle.write(path, path.relative_to(ROOT))
    for directory in ['run-2026-10-30T0750', 'exploratory']:
        for path in (ROOT / directory).rglob('*'):
            if path.is_file():
                bundle.write(path, path.relative_to(ROOT))
    for path in RUN.rglob('*'):
        if path.is_file() and (path.parent.name == 'filmstrip-work' or 'settle-probe' in path.name):
            bundle.write(path, path.relative_to(ROOT))
files.append(archive)
(ROOT / 'upload-manifest.json').write_text(json.dumps([str(path) for path in files], indent=2))
with (ROOT / 'uploads.md').open('w') as output:
    result = subprocess.run(['ghdrop', 'upload', '-m', *map(str, files)], stdout=output, stderr=subprocess.STDOUT)
print(f'uploaded command exit {result.returncode}; {len(files)} files; archive {archive.stat().st_size} bytes')
raise SystemExit(result.returncode)
