#!/usr/bin/env bash
# Re-benchmark the feature/opencv5 pipeline against Amazon X-Ray, all 9 films, LVFace-B.
# Full end-to-end scene_analyze (decode→detect→scene→embed→match→presence) — NOT a replay,
# because the framework changed enough that old embedding dumps no longer represent the front half.
# Outputs land in experiments/results/xray_opencv5_lvface/ (durable; /tmp gets wiped).
set -uo pipefail

REPO="/home/dtourolle/Development/scene-actor-extraction"
cd "$REPO"

ARC="models/LVFace-B_Glint360K.onnx"
GAL="experiments/galleries/gallery_LVFace-B_Glint360K.h5"
OUT="experiments/results/xray_opencv5_lvface"
mkdir -p "$OUT"

BIN="build/scene_analyze"
LUT="experiments/file-lut.json"
FILMS="experiments/manifests/films.json"

# film slugs and their xray dirs, from films.json
mapfile -t ROWS < <(python3 -c '
import json
for f in json.load(open("'"$FILMS"'")):
    print(f["slug"] + "\t" + f["xray"])
')

echo "=== X-Ray re-benchmark (feature/opencv5, LVFace-B) — $(date) ===" | tee "$OUT/run.log"

for row in "${ROWS[@]}"; do
    slug="${row%%$'\t'*}"
    xray="${row#*$'\t'}"
    movie="$(python3 -c 'import json,sys; print(json.load(open("'"$LUT"'"))["'"$slug"'"])')"
    pred="$OUT/${slug}.json"

    echo "" | tee -a "$OUT/run.log"
    echo ">>> $slug" | tee -a "$OUT/run.log"
    if [ ! -f "$movie" ]; then
        echo "  SKIP: movie missing: $movie" | tee -a "$OUT/run.log"
        continue
    fi

    # Run the full pipeline (serial — ROCm GPU wedges at concurrency>2-3).
    "$BIN" --movie "$movie" --arcface "$ARC" --gallery "$GAL" \
        --output "$pred" >"$OUT/${slug}.pipeline.log" 2>&1
    rc=$?
    if [ $rc -ne 0 ] || [ ! -f "$pred" ]; then
        echo "  PIPELINE FAILED (rc=$rc) — see ${slug}.pipeline.log" | tee -a "$OUT/run.log"
        continue
    fi
    echo "  pipeline ok" | tee -a "$OUT/run.log"

    # Score against X-Ray, masked to gallery∩GT, 1s grid.
    python scripts/validation/sample_eval.py \
        --pred "$pred" --xray "$xray" --gallery "$GAL" --step 1.0 \
        >"$OUT/${slug}.eval.txt" 2>&1
    tail -8 "$OUT/${slug}.eval.txt" | tee -a "$OUT/run.log"
done

echo "" | tee -a "$OUT/run.log"
echo "=== DONE — $(date) ===" | tee -a "$OUT/run.log"
