#!/usr/bin/env bash
# Manual: local HEVC bundle preflight for lab transport work; not part of CI.
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
STAMP="$(date +%Y%m%d-%H%M%S)"
LOCAL_OUTPUT_DIR="${LESAVKA_LOCAL_HEVC_BUNDLE_AUDIT_OUTPUT_DIR:-/tmp/lesavka-local-hevc-bundle-audit-${STAMP}}"
LOCAL_AUDIT_JSON="${LESAVKA_LOCAL_HEVC_BUNDLE_AUDIT_JSON:-${LOCAL_OUTPUT_DIR}/hevc-bundle-audit.json}"
LOCAL_RUN_LOG="${LOCAL_OUTPUT_DIR}/hevc-bundle-audit.log"

mkdir -p "${LOCAL_OUTPUT_DIR}"

echo "==> local HEVC+audio bundle audit"
echo "   ↪ artifact_dir=${LOCAL_OUTPUT_DIR}"
echo "   ↪ audit_json=${LOCAL_AUDIT_JSON}"
echo "   ↪ run_log=${LOCAL_RUN_LOG}"
echo "   ↪ no remote host, sudo, tunnel, or RCT capture is used"

(
  cd "${REPO_ROOT}"
  export LESAVKA_LOCAL_HEVC_BUNDLE_AUDIT_JSON="${LOCAL_AUDIT_JSON}"
  cargo test -p lesavka_client hevc_probe_bundle_audit_writes_manifest -- --nocapture
  cargo test -p lesavka_client hevc_probe_bundle_train_covers_every_supported_mode -- --nocapture
  cargo test -p lesavka_client hevc_probe_bundle_train_drops_stale_events_as_complete_av_units_under_jitter -- --nocapture
  cargo test -p lesavka_client runtime_probe_hevc_video_and_audio_can_form_one_local_bundle -- --nocapture
) 2>&1 | tee "${LOCAL_RUN_LOG}"

echo "==> local HEVC+audio bundle audit summary"
"${REPO_ROOT}/scripts/manual/validate_local_hevc_bundle_audit.py" "${LOCAL_AUDIT_JSON}"

echo "==> done"
echo "artifact_dir: ${LOCAL_OUTPUT_DIR}"
echo "audit_json: ${LOCAL_AUDIT_JSON}"
echo "run_log: ${LOCAL_RUN_LOG}"
