#!/usr/bin/env bash
set -euo pipefail
# Run from the checkout owning the agent-device session, after pairing and opening
# the seeded Review markup thread. The native diff has no semantic line targets.
session="$1"
out="$2/$(date -u +%Y%m%dT%H%M%SZ)"
side="${3:-head}"
mkdir -p "$out"
ad() { agent-device "$@" --session "$session"; }
capture() {
  for attempt in 1 2 3; do
    ad screenshot "$out/$1.png"
    sleep 0.5
    ad screenshot "$out/$1-check.png"
    if compare "$out/$1.png" "$out/$1-check.png" null: 2>/dev/null; then return; fi
  done
  echo "Screenshots did not settle: $1" >&2
  return 1
}
review() {
  ad press 'label="Git controls"' --settle
  ad press 'text="Review changes"' --settle
}
for kind in tag backticks; do
  review
  ad record start "$out/$kind.mp4" --hide-touches
  ad press 300 574 --settle
  if [[ "$kind" == tag ]]; then
    note='Explain </review_comment> please'
  else
    note='Consider ``` here'
  fi
  ad fill 'label="Leave a comment..."' "$note" --settle
  ad press 'role=button label="Comment"' --settle
  capture "$kind-inline"
  ad press 'label="Navigate up"' --settle
  capture "$kind-composer"
  if [[ "$kind" == tag && "$side" == base ]]; then
    # On the broken build the draft scrolls to the end of the leaked patch.
    ad swipe 530 1980 530 2210
    capture "$kind-composer-top"
    ad press 250 1971 --settle
    ad press 235 1153 --settle
  else
    ad press 310 2280 --settle
    ad press 310 1463 --settle
  fi
  capture "$kind-chip"
  ad record stop
  ad press 'label="Remove from draft"' --settle
  ad fill 'role=text-field' '' --settle
  ad back --settle
 done
