"""Run from a paired Android client on unfiltered Chats, with Bravo remembered.
Usage: python3 reproduce.py SESSION OUTPUT_PARENT SIDE
"""
import datetime, pathlib, subprocess, sys, time
session, parent, side = sys.argv[1:]
out = pathlib.Path(parent) / (side + '-' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ'))
out.mkdir(parents=True)
def run(*args):
    result = subprocess.run(['agent-device', *args, '--session', session], text=True, capture_output=True)
    with (out / 'driver.log').open('a') as f:
        f.write(' '.join(args) + '\n' + result.stdout + result.stderr + '\n')
    result.check_returncode()
    return result.stdout

def click(label):
    run('click', 'text="' + label + '"', '--settle')
run('record', 'start', str(out / 'flow.mp4'), '--hide-touches')
click('Settings')
click('General')
settings = run('snapshot', '-i')
assert '[switch] "Use last project" [checked]' in settings
run('screenshot', str(out / 'settings.png'))
click('Chats')
click('New thread')
run('wait', 'text', 'Bravo', '20000')
run('screenshot', str(out / 'remembered-bravo.png'))
click('Navigate up')
click('Filter threads')
click('Project')
click('Alpha')
click('New thread')
run('wait', 'text', 'What should we build', '20000')
run('screenshot', str(out / 'final-a.png'))
time.sleep(0.5)
run('screenshot', str(out / 'final-b.png'))
run('record', 'stop')
print(out)
