#!/bin/bash
# legacy_test/run_all_legacy_tests.sh
#
# Sequential launcher for all Track A (Legacy Test) scripts.
# Each script uses LegacyCelerisLab which compiles CUDA kernels.
# A 60-second delay between tests prevents compilation conflicts.
#
# Usage:
#   bash run_all_legacy_tests.sh [DEVICE_ID]
#   DEVICE_ID defaults to 0 if not provided.

set -euo pipefail

DEVICE_ID="${1:-0}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/../../.."  # repo root

log() { echo "[$(date '+%H:%M:%S')] $*"; }

CONDA_ENV="pycuda_3_10"
DELAY=60

log "=== Legacy Test: Run All ==="
log "Device: $DEVICE_ID, Conda: $CONDA_ENV, Delay: ${DELAY}s"

# Array of (name, script_path)
declare -a TESTS=(
    "Karman Re100:src/drl_pinball/legacy_test/test_karman_cloak_re100.py"
    "Steady Cloak:src/drl_pinball/legacy_test/test_steady_cloak.py"
    "Illusion 1L:src/drl_pinball/legacy_test/test_illusion_1L.py"
    "Vortex Lamb:src/drl_pinball/legacy_test/test_vortex_lamb.py"
    "Cross-Re Karman:src/drl_pinball/legacy_test/test_karman_cloak_crossre.py"
    "Illusion 0.75L/1.5L:src/drl_pinball/legacy_test/test_illusion_remaining.py"
    "Vortex Taylor:src/drl_pinball/legacy_test/test_vortex_taylor.py"
    "Erase (experimental):src/drl_pinball/legacy_test/test_erase.py"
)

PASS_COUNT=0
FAIL_COUNT=0
declare -a FAILED_NAMES=()

for test_entry in "${TESTS[@]}"; do
    name="${test_entry%%:*}"
    script="${test_entry##*:}"

    log ""
    log "--- $name ---"
    log "Running: conda run -n $CONDA_ENV python $script --device $DEVICE_ID"

    if conda run -n "$CONDA_ENV" python "$script" --device "$DEVICE_ID"; then
        log "[PASS] $name"
        PASS_COUNT=$((PASS_COUNT + 1))
    else
        log "[FAIL] $name (exit code $?)"
        FAIL_COUNT=$((FAIL_COUNT + 1))
        FAILED_NAMES+=("$name")
    fi

    # Avoid CUDA compilation conflicts: wait 60s between tests
    if [[ "$test_entry" != "${TESTS[-1]}" ]]; then
        log "Waiting ${DELAY}s for CUDA compilation lock to clear..."
        sleep "$DELAY"
    fi
done

log ""
log "=== Summary ==="
log "Passed: $PASS_COUNT / $((PASS_COUNT + FAIL_COUNT))"

if [[ $FAIL_COUNT -gt 0 ]]; then
    log "Failed tests:"
    for fn in "${FAILED_NAMES[@]}"; do
        log "  - $fn"
    done
    exit 1
fi

log "All tests passed."
