"""Read only: report synthetic fixture bindings and filesystem state after cleanup."""
import json
import pathlib
import sqlite3
import subprocess
import sys

for side, checkout in zip(('before', 'after'), sys.argv[1:]):
    home = pathlib.Path(checkout) / '.t3/manual'
    db = sqlite3.connect(f'file:{home}/userdata/statev2.sqlite?mode=ro', uri=True)
    rows = db.execute('SELECT payload_json FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL').fetchall()
    threads = [json.loads(row[0]) for row in rows]
    shared = [t for t in threads if t['branch'] == 'shared-checkout']
    paths = [pathlib.Path(t['worktreePath']) for t in shared]
    folder = home / 'worktrees/cleanup-demo'
    worktree = folder / 'shared-checkout'
    print(json.dumps({
        'side': side,
        'commit': subprocess.check_output(['git', '-C', checkout, 'rev-parse', 'HEAD'], text=True).strip(),
        'live_thread_bindings_to_shared_checkout': len(shared),
        'shared_thread_titles': [t['title'] for t in shared],
        'stored_spellings_differ': len(set(map(str, paths))) == 2,
        'spellings_resolve_to_same_location': len({str(p.resolve()) for p in paths}) == 1,
        'shared_checkout_exists': worktree.exists(),
        'one_thread_control_exists': (folder / 'single-thread-control').exists(),
        'remaining_checkout_is_clean': subprocess.check_output(['git', '-C', str(worktree), 'status', '--porcelain'], text=True) == '' if worktree.exists() else None,
    }, ensure_ascii=False))
