"""Run after pairing a fresh T3 server with two completed command-output Replay chats.
The project is a checkout of pingdotgg/t3code; titles: Earlier work and Second chat.
Use iPhone Duo / iOS 27.2, folded closed, portrait, light theme, 466x678.
Usage: python3 drive-flow.py SESSION OUTPUT_PARENT SIDE
Each run writes a new timestamped directory. No credentials are recorded here.
"""
import datetime
from pathlib import Path
import subprocess
import sys
import time
session, parent, side = sys.argv[1:]
out = Path(parent) / (side + '-' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ'))
out.mkdir(parents=True)
def device(*args):
    return subprocess.run(['agent-device', *args, '--session', session], check=True, text=True, capture_output=True).stdout
with (out/'driver.log').open('w') as log:
    def call(*args):
        result = device(*args)
        log.write(result); log.flush()
        return result
    call('record', 'start', str(out/'flow.mp4'), '--fps', '10', '--hide-touches')
    call('press', 'role=button label="PRs"', '--settle')
    call('press', 'label="Pull requests, switch list"', '--settle')
    call('press', 'role=button label="Issues"', '--settle')
    call('wait', 'text', 'Filter by involvement')
    call('scroll', 'down', '5000', '--settle')
    call('scroll', 'bottom', '--settle')
    call('scroll', 'top', '--settle')
    call('press', 'label="Filter by involvement"', '--settle')
    call('press', 'role=button label="Assigned to you"', '--settle')
    call('snapshot', '-i')
    call('screenshot', str(out/'assigned.png'))
    call('press', 'role=button label="Chats"', '--settle')
    call('press', 'label="Earlier work, Done"', '--settle')
    call('snapshot', '-i')
    call('screenshot', str(out/'chat-a.png'))
    time.sleep(0.5)
    call('screenshot', str(out/'chat-b.png'))
    call('record', 'stop')
print(out)
