#!/usr/bin/env bash
# Capture each canonical UEC page once, then use the page's real theme switch
# to capture both light and dark without a second data load.
set -euo pipefail

ROOT=$(cd "$(dirname "$0")/.." && pwd)
BASE=${UEC_URL:-'https://platform-dev.invinsense.dev/xdr/uec/default'}
USERNAME=${UEC_USERNAME:?Set UEC_USERNAME}
PASSWORD=${UEC_PASSWORD:?Set UEC_PASSWORD}
SESSION=${UEC_SESSION:-uec-doc-refresh}
WAIT=${UEC_WAIT_SECONDS:-15}
export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/tmp/agent-browser-runtime}
export HOME=${UEC_BROWSER_HOME:-/tmp/agent-browser-home}
export AGENT_BROWSER_CONFIG=${AGENT_BROWSER_CONFIG:-/tmp/agent-browser.json}

command -v agent-browser >/dev/null || { echo 'agent-browser is required' >&2; exit 127; }
ab(){ agent-browser --session "$SESSION" "$@"; }
ab set viewport 1600 1000 >/dev/null

ab open "$BASE" >/dev/null
sleep "$WAIT"
if ab snapshot -i 2>/dev/null | grep -q 'textbox "Email"'; then
  ab find label Email fill "$USERNAME" >/dev/null
  ab find label Password fill "$PASSWORD" >/dev/null
  ab find role button click --name 'Sign in' >/dev/null
  sleep 8
fi
if ab get url 2>/dev/null | grep -q '/login'; then
  echo 'Sign-in failed; not retrying into the authentication rate limit' >&2
  exit 1
fi

n=0
while IFS='|' read -r slug path; do
  [ -n "$slug" ] || continue
  n=$((n + 1))
  ab open "$BASE$path" >/dev/null
  sleep "$WAIT"
  ab snapshot -i > /tmp/uec-refresh-snapshot.txt 2>/dev/null
  if grep -q 'textbox "Email"' /tmp/uec-refresh-snapshot.txt; then
    echo "$slug returned to login; refusing another sign-in" >&2
    exit 1
  fi

  if grep -q 'button "Switch to dark theme"' /tmp/uec-refresh-snapshot.txt; then
    first=light; second=dark
    ref=$(grep 'button "Switch to dark theme"' /tmp/uec-refresh-snapshot.txt | head -1 | sed -E 's/.*ref=([^]]+)\].*/\1/')
  elif grep -q 'button "Switch to light theme"' /tmp/uec-refresh-snapshot.txt; then
    first=dark; second=light
    ref=$(grep 'button "Switch to light theme"' /tmp/uec-refresh-snapshot.txt | head -1 | sed -E 's/.*ref=([^]]+)\].*/\1/')
  else
    echo "$slug has no light/dark switch" >&2
    exit 1
  fi

  mkdir -p "$ROOT/images/$first/pages" "$ROOT/images/$second/pages"
  ab screenshot --full "$ROOT/images/$first/pages/$slug.png" >/dev/null

  # Keyboard activation is reliable when a sticky header overlaps the click point.
  ab focus "@$ref" >/dev/null
  ab press Enter >/dev/null
  sleep 8
  verify=$(ab snapshot -i 2>/dev/null || true)
  state=$(ab eval "document.documentElement.className + '|' + (localStorage.getItem('theme') || '')" 2>/dev/null || true)
  state_needle="|$second"
  { grep -q "button \"Switch to $([ "$second" = dark ] && printf 'light' || printf 'dark') theme\"" <<<"$verify" || [[ "$state" == *"$state_needle"* ]]; } || { echo "$slug $second switch failed" >&2; exit 1; }
  ab screenshot --full "$ROOT/images/$second/pages/$slug.png" >/dev/null
  echo "$n|$slug|$first+$second|switch=verified"
done < "$ROOT/tools/uec-routes.tsv"
echo "captured $n pages in both themes"

# Capture the signed-out login page once, in both themes, with its own switch.
ab open "$BASE" >/dev/null
sleep "$WAIT"
ab find role button click --name 'Account menu' >/dev/null
sleep 1
ab find text 'Sign out' click >/dev/null
sleep "$WAIT"
ab snapshot -i > /tmp/uec-login-snapshot.txt 2>/dev/null
if grep -q 'button "Switch to dark theme"' /tmp/uec-login-snapshot.txt; then
  first=light; second=dark
  ref=$(grep 'button "Switch to dark theme"' /tmp/uec-login-snapshot.txt | head -1 | sed -E 's/.*ref=([^]]+)\].*/\1/')
elif grep -q 'button "Switch to light theme"' /tmp/uec-login-snapshot.txt; then
  first=dark; second=light
  ref=$(grep 'button "Switch to light theme"' /tmp/uec-login-snapshot.txt | head -1 | sed -E 's/.*ref=([^]]+)\].*/\1/')
else
  echo 'Signed-out login page has no theme switch' >&2
  exit 1
fi
ab screenshot --full "$ROOT/images/$first/pages/signed-out-login.png" >/dev/null
ab focus "@$ref" >/dev/null
ab press Enter >/dev/null
sleep 8
verify=$(ab snapshot -i 2>/dev/null || true)
state=$(ab eval "document.documentElement.className + '|' + (localStorage.getItem('theme') || '')" 2>/dev/null || true)
state_needle="|$second"
opposite=$([ "$second" = dark ] && printf 'light' || printf 'dark')
{ grep -q "button \"Switch to $opposite theme\"" <<<"$verify" || [[ "$state" == *"$state_needle"* ]]; } || { echo "Login $second switch failed" >&2; exit 1; }
ab screenshot --full "$ROOT/images/$second/pages/signed-out-login.png" >/dev/null
echo 'signed-out-login|both-themes|switch=verified'
