#!/usr/bin/env bash
# Manual: one-command HEVC recovery runway after Theia comes back online.
# Not part of CI: this coordinates local preflights, passwordless Theia deploy,
# and hardware-in-the-loop RCT calibration.
set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/../.." >/dev/null 2>&1 && pwd)"
STAMP="$(date +%Y%m%d-%H%M%S)"

OUTPUT_DIR="${LESAVKA_HEVC_POST_REBOOT_OUTPUT_DIR:-/tmp/lesavka-hevc-post-reboot-${STAMP}}"
RUN_LOG="${OUTPUT_DIR}/hevc-post-reboot-sequence.log"
REMOTE_HOST="${LESAVKA_HEVC_POST_REBOOT_REMOTE_HOST:-${LESAVKA_HEVC_REENTRY_HOST:-theia}}"
REMOTE_REPO="${LESAVKA_HEVC_POST_REBOOT_REMOTE_REPO:-${LESAVKA_HEVC_REENTRY_REMOTE_REPO:-/home/theia/Development/lesavka-codex}}"
WAIT_SECONDS="${LESAVKA_HEVC_POST_REBOOT_WAIT_SECONDS:-900}"
WAIT_INTERVAL_SECONDS="${LESAVKA_HEVC_POST_REBOOT_WAIT_INTERVAL_SECONDS:-15}"

RUN_LOCAL_PREFLIGHTS="${LESAVKA_HEVC_POST_REBOOT_RUN_LOCAL_PREFLIGHTS:-1}"
RUN_REENTRY="${LESAVKA_HEVC_POST_REBOOT_RUN_REENTRY:-1}"
RUN_STATIC_MATRIX="${LESAVKA_HEVC_POST_REBOOT_RUN_STATIC_MATRIX:-1}"
RUN_FINAL_SANITY="${LESAVKA_HEVC_POST_REBOOT_RUN_FINAL_SANITY:-0}"

REENTRY_MODE="${LESAVKA_HEVC_POST_REBOOT_REENTRY_MODE:-1280x720@30}"
PENDING_MODES="${LESAVKA_HEVC_POST_REBOOT_PENDING_MODES:-1280x720@30,1280x720@20}"
FINAL_SANITY_MODES="${LESAVKA_HEVC_POST_REBOOT_FINAL_MODES:-1280x720@20,1280x720@30,1920x1080@20,1920x1080@30}"
RECONFIGURE_COMMAND="${LESAVKA_HEVC_POST_REBOOT_RECONFIGURE_COMMAND:-ssh ${REMOTE_HOST} sudo -n /usr/local/sbin/lesavka-dev-install reconfigure \"\$LESAVKA_MODE\" hevc}"

mkdir -p "${OUTPUT_DIR}"

echo "==> HEVC post-reboot sequence"
echo "   ↪ output_dir=${OUTPUT_DIR}"
echo "   ↪ run_log=${RUN_LOG}"
echo "   ↪ remote_host=${REMOTE_HOST}"
echo "   ↪ remote_repo=${REMOTE_REPO}"
echo "   ↪ wait_seconds=${WAIT_SECONDS} wait_interval_seconds=${WAIT_INTERVAL_SECONDS}"
echo "   ↪ local_preflights=${RUN_LOCAL_PREFLIGHTS} reentry=${RUN_REENTRY} static_matrix=${RUN_STATIC_MATRIX} final_sanity=${RUN_FINAL_SANITY}"
echo "   ↪ pending_modes=${PENDING_MODES}"
echo "   ↪ final_sanity_modes=${FINAL_SANITY_MODES}"
echo "   ↪ sudo is non-interactive only; no password prompt path is used"

run_local_preflights() {
  echo "== local HEVC bundle audit =="
  LESAVKA_LOCAL_HEVC_BUNDLE_AUDIT_OUTPUT_DIR="${OUTPUT_DIR}/local-bundle-audit" \
    "${SCRIPT_DIR}/run_local_hevc_bundle_audit.sh"

  echo "== local HEVC encoder preflight =="
  LESAVKA_LOCAL_HEVC_ENCODER_PREFLIGHT_OUTPUT_DIR="${OUTPUT_DIR}/local-encoder-preflight" \
    "${SCRIPT_DIR}/run_local_hevc_encoder_preflight.sh"
}

run_remote_reentry() {
  echo "== remote HEVC re-entry =="
  LESAVKA_HEVC_REENTRY_HOST="${REMOTE_HOST}" \
    LESAVKA_HEVC_REENTRY_REMOTE_REPO="${REMOTE_REPO}" \
    LESAVKA_HEVC_REENTRY_MODE="${REENTRY_MODE}" \
    LESAVKA_HEVC_REENTRY_CODEC=hevc \
    LESAVKA_HEVC_REENTRY_SYNC=1 \
    LESAVKA_HEVC_REENTRY_BUILD=1 \
    LESAVKA_HEVC_REENTRY_DEPLOY=1 \
    LESAVKA_HEVC_REENTRY_RECONFIGURE=1 \
    LESAVKA_HEVC_REENTRY_WAIT_SECONDS="${WAIT_SECONDS}" \
    LESAVKA_HEVC_REENTRY_WAIT_INTERVAL_SECONDS="${WAIT_INTERVAL_SECONDS}" \
    LESAVKA_HEVC_REENTRY_OUTPUT_DIR="${OUTPUT_DIR}/remote-reentry" \
    "${SCRIPT_DIR}/run_hevc_remote_reentry_check.sh"
}

run_hevc_static_matrix() {
  echo "== pending HEVC static calibration matrix =="
  LOCAL_OUTPUT_DIR="${OUTPUT_DIR}" \
    REMOTE_PULSE_CAPTURE_TOOL="${REMOTE_PULSE_CAPTURE_TOOL:-gst}" \
    REMOTE_PULSE_VIDEO_MODE="${REMOTE_PULSE_VIDEO_MODE:-mjpeg-cfr}" \
    LESAVKA_SERVER_RC_PROFILE=hevc \
    LESAVKA_SERVER_RC_MODES="${PENDING_MODES}" \
    LESAVKA_SERVER_RC_REPEAT_COUNT="${LESAVKA_HEVC_POST_REBOOT_REPEAT_COUNT:-3}" \
    LESAVKA_SERVER_RC_STATIC_MIN_RUNS="${LESAVKA_HEVC_POST_REBOOT_STATIC_MIN_RUNS:-3}" \
    LESAVKA_SERVER_RC_VERBOSE_PROBES="${LESAVKA_SERVER_RC_VERBOSE_PROBES:-0}" \
    LESAVKA_SERVER_RC_RECONFIGURE=1 \
    LESAVKA_SERVER_RC_PROMPT_SUDO_EARLY=0 \
    LESAVKA_SERVER_RC_RECONFIGURE_COMMAND="${RECONFIGURE_COMMAND}" \
    CAPTURE_SECONDS="${CAPTURE_SECONDS:-90}" \
    PROBE_TIMEOUT_SECONDS="${PROBE_TIMEOUT_SECONDS:-160}" \
    PROBE_DURATION_SECONDS="${PROBE_DURATION_SECONDS:-20}" \
    PROBE_WARMUP_SECONDS="${PROBE_WARMUP_SECONDS:-4}" \
    "${SCRIPT_DIR}/run_server_to_rc_mode_matrix.sh"
}

run_hevc_final_sanity() {
  echo "== final all-mode HEVC sanity matrix =="
  LOCAL_OUTPUT_DIR="${OUTPUT_DIR}" \
    REMOTE_PULSE_CAPTURE_TOOL="${REMOTE_PULSE_CAPTURE_TOOL:-gst}" \
    REMOTE_PULSE_VIDEO_MODE="${REMOTE_PULSE_VIDEO_MODE:-mjpeg-cfr}" \
    LESAVKA_SERVER_RC_PROFILE=hevc \
    LESAVKA_SERVER_RC_MODES="${FINAL_SANITY_MODES}" \
    LESAVKA_SERVER_RC_REPEAT_COUNT=1 \
    LESAVKA_SERVER_RC_VERBOSE_PROBES="${LESAVKA_SERVER_RC_VERBOSE_PROBES:-0}" \
    LESAVKA_SERVER_RC_RECONFIGURE=1 \
    LESAVKA_SERVER_RC_TUNE_DELAYS=0 \
    LESAVKA_SERVER_RC_PROMPT_SUDO_EARLY=0 \
    LESAVKA_SERVER_RC_RECONFIGURE_COMMAND="${RECONFIGURE_COMMAND}" \
    CAPTURE_SECONDS="${CAPTURE_SECONDS:-90}" \
    PROBE_TIMEOUT_SECONDS="${PROBE_TIMEOUT_SECONDS:-160}" \
    PROBE_DURATION_SECONDS="${PROBE_DURATION_SECONDS:-20}" \
    PROBE_WARMUP_SECONDS="${PROBE_WARMUP_SECONDS:-4}" \
    "${SCRIPT_DIR}/run_server_to_rc_mode_matrix.sh"
}

(
  cd "${REPO_ROOT}"

  if [[ "${RUN_LOCAL_PREFLIGHTS}" == "1" ]]; then
    run_local_preflights
  fi

  if [[ "${RUN_REENTRY}" == "1" ]]; then
    run_remote_reentry
  fi

  if [[ "${RUN_STATIC_MATRIX}" == "1" ]]; then
    run_hevc_static_matrix
  fi

  if [[ "${RUN_FINAL_SANITY}" == "1" ]]; then
    run_hevc_final_sanity
  fi
) 2>&1 | tee "${RUN_LOG}"

echo "==> done"
echo "artifact_dir: ${OUTPUT_DIR}"
echo "run_log: ${RUN_LOG}"
