"""Android Chrome/Gboard evidence interaction.

Prerequisites: lease Pixel 9 with its real Gboard (no test IME), pair Chrome
with the real built T3 web app, and open an empty thread in a fresh database.
Use the same light theme and 1080x2424 portrait display for each commit.
The server auto-created the empty project/thread; project title was renamed
with `t3 project rename` to "Keyboard proof". No copied database was used.

The recordings begin with 'first line' already typed. On base, Enter sends it;
on head, Enter inserts a newline. 'second line' makes the difference visible.
Then dismiss/reopen Gboard and repeat Enter, followed by 'third line'.
The commands below reproduce the interaction used for both captures; semantic
refs were used for the second Enter when available (the same Gboard key).
"""
import argparse
import datetime
import pathlib
import subprocess

p = argparse.ArgumentParser()
p.add_argument('--session', required=True)
p.add_argument('--output', required=True)
a = p.parse_args()
out = pathlib.Path(a.output) / datetime.datetime.now().strftime('%Y%m%dT%H%M%S')
out.mkdir(parents=True)

def device(*args):
    subprocess.run(['agent-device', *args, '--session', a.session], check=True)

device('press', '300', '2165')
device('type', 'first line')
device('record', 'start', str(out / 'enter-raw.mp4'), '--hide-touches')
device('snapshot', '-i')
device('press', '995', '2220', '--settle')
device('type', 'second line')
device('snapshot', '-i')
device('record', 'stop')
device('back', '--settle')
device('record', 'start', str(out / 'reopen-raw.mp4'), '--hide-touches')
device('press', '541', '2113', '--settle')
device('press', '995', '2220', '--settle')
device('type', 'third line')
device('snapshot', '-i')
device('record', 'stop')

# Export used for publication: remove Android/browser chrome (private origin),
# retain the entire T3 app and Gboard, and scale to half resolution.
for name in ['enter', 'reopen']:
    subprocess.run(['ffmpeg', '-y', '-v', 'error', '-i', str(out / f'{name}-raw.mp4'),
                    '-vf', 'crop=1080:2208:0:216,scale=540:1104', '-an',
                    '-c:v', 'libx264', '-crf', '22', str(out / f'{name}.mp4')], check=True)
