"""Copy one completed synthetic UI fixture into a freshly initialized, stopped server.
Usage: python seed.py SOURCE_DB TARGET_DB THREAD_ID TARGET_PROJECT_ID
Only projection rows are copied; auth, event history and other threads are untouched.
The source fixture was generated by a real Claude Haiku 5.5 turn for this reproduction.
"""
import json, sqlite3, sys
source, target, thread, project = sys.argv[1:]
s = sqlite3.connect('file:' + source + '?mode=ro', uri=True)
d = sqlite3.connect(target)
for (table,) in s.execute("SELECT name FROM sqlite_schema WHERE type='table' AND name LIKE 'orchestration_v2_projection_%'"):
    cols = [c[1] for c in s.execute(f'PRAGMA table_info({table})')]
    if 'thread_id' not in cols:
        continue
    rows = s.execute(f'SELECT * FROM {table} WHERE thread_id=?', (thread,)).fetchall()
    for row in rows:
        row = list(row)
        if 'project_id' in cols:
            row[cols.index('project_id')] = project
        if 'payload_json' in cols:
            i = cols.index('payload_json')
            payload = json.loads(row[i])
            if isinstance(payload, dict) and 'projectId' in payload:
                payload['projectId'] = project
            row[i] = json.dumps(payload)
        d.execute(f'INSERT OR REPLACE INTO {table} ({",".join(cols)}) VALUES ({",".join("?" for _ in cols)})', row)
d.commit()
print('Copied completed synthetic thread projections.')
