#!/usr/bin/env bash
# End-to-end remote-access validation: operator + device (CLI simulates Android).
#
# Flow (matches app + dashboard):
#   1. device disable (clean slate)
#   2. device enable → wait (not whitelisted)
#   3. operator whitelist + open session
#   4. device poll → connect
#   5. operator close session
#   6. device disable
#
# Usage:
#   ./scripts/ra_e2e_cli.sh
#   CRASHES_BASE=https://apps.f0xx.org/app/androidcast_project/crashes ./scripts/ra_e2e_cli.sh
#   ./scripts/ra_e2e_cli.sh --apply-wg
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
# shellcheck source=ra_lib.sh
source "$ROOT/scripts/ra_lib.sh"

APPLY_WG=0
DEVICE_ID="ra-e2e-$(date +%s)"
while [[ $# -gt 0 ]]; do
  case "$1" in
    --apply-wg) APPLY_WG=1; shift ;;
    --base) export CRASHES_BASE="$2"; shift 2 ;;
    --device-id) DEVICE_ID="$2"; shift 2 ;;
    -h|--help)
      echo "Usage: $0 [--base URL] [--device-id ID] [--apply-wg]"
      exit 0
      ;;
    *) echo "Unknown: $1" >&2; exit 2 ;;
  esac
done

export RA_DEVICE_ID="$DEVICE_ID"
export RA_RANDOM_FILE="/tmp/androidcast-ra-${DEVICE_ID}.random"
COOKIE_JAR="$(mktemp)"
trap 'rm -f "$COOKIE_JAR"' EXIT

echo "== Remote access E2E CLI =="
echo "BASE=$(ra_base)"
echo "DEVICE_ID=$DEVICE_ID"
echo

echo "== 1. device disable =="
bash "$ROOT/scripts/ra_device_sim.sh" disable --device-id "$DEVICE_ID" --base "$(ra_base)"
echo

echo "== 2. device enable (expect wait) =="
bash "$ROOT/scripts/ra_device_sim.sh" enable --once --device-id "$DEVICE_ID" --base "$(ra_base)" | tee /tmp/ra-e2e-enable.out
grep -q 'action=wait' /tmp/ra-e2e-enable.out || {
  echo "FAIL: expected wait before whitelist" >&2
  exit 1
}
echo OK
echo

echo "== 3. operator login + whitelist =="
ra_admin_login "$COOKIE_JAR"
ra_admin_post "$COOKIE_JAR" whitelist "{\"device_id\":\"$DEVICE_ID\",\"whitelisted\":true}" | ra_json '.'
echo OK whitelist
echo

echo "== 4. operator open session =="
ra_admin_post "$COOKIE_JAR" open_session "{\"device_id\":\"$DEVICE_ID\"}" | ra_json '.'
echo OK open_session
echo

echo "== 5. device poll until connect =="
WG_ARGS=()
[[ "$APPLY_WG" -eq 1 ]] && WG_ARGS+=(--apply-wg)
bash "$ROOT/scripts/ra_device_sim.sh" poll --until connect --device-id "$DEVICE_ID" --base "$(ra_base)" "${WG_ARGS[@]}"
echo OK connect
echo

echo "== 6. operator dashboard =="
ra_admin_post "$COOKIE_JAR" dashboard '{}' 2>/dev/null || \
  curl -sf -b "$COOKIE_JAR" "$(ra_admin_url dashboard)" | ra_json '{ok, permissions}'
echo

echo "== 7. operator close session =="
SESSION_ID="$(curl -sf -b "$COOKIE_JAR" "$(ra_admin_url sessions)" | ra_json -r '.sessions[] | select(.device_id=="'"$DEVICE_ID"'") | .session_id' | head -1)"
if [[ -z "$SESSION_ID" ]]; then
  SESSION_ID="$(curl -sf -b "$COOKIE_JAR" "$(ra_admin_url dashboard)" | ra_json -r '.active_sessions[0].session_id // empty')"
fi
if [[ -n "$SESSION_ID" ]]; then
  ra_admin_post "$COOKIE_JAR" close_session "{\"session_id\":\"$SESSION_ID\",\"device_id\":\"$DEVICE_ID\"}" | ra_json '.'
  echo OK close_session "$SESSION_ID"
else
  echo "WARN: no session_id to close"
fi
echo

echo "== 8. device disable =="
bash "$ROOT/scripts/ra_device_sim.sh" disable --device-id "$DEVICE_ID" --base "$(ra_base)"
echo

echo "== E2E CLI passed =="
