#!/bin/bash
# reproduce/run_all_reproduce_tests.sh
#
# Sequential launcher for Track B (Reproduce) scripts.
# Phase 2: Open-loop target validation
# Phase 3: DRL inference with legacy-compatible config
#
# Usage:
#   bash run_all_reproduce_tests.sh [DEVICE_ID] [PHASE]
#   DEVICE_ID defaults to 0
#   PHASE defaults to "2,3" (run both phases)

set -euo pipefail

DEVICE_ID="${1:-0}"
PHASE="${2:-2,3}"
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 "=== Reproduce Tests ==="
log "Device: $DEVICE_ID, Phase: $PHASE"

# --- Phase 2: Open-loop validation ---
if [[ "$PHASE" == *"2"* ]]; then
    log ""
    log "--- Phase 2: Open-loop target validation ---"

    if conda run -n "$CONDA_ENV" python src/drl_pinball/reproduce/phase2_open_loop.py \
        --device "$DEVICE_ID" --scene all; then
        log "[PASS] Phase 2"
    else
        log "[FAIL] Phase 2"
    fi

    log "Waiting ${DELAY}s..."
    sleep "$DELAY"
fi

# --- Phase 3: DRL inference ---
if [[ "$PHASE" == *"3"* ]]; then
    log ""
    log "--- Phase 3: DRL inference with legacy-compat config ---"

    if conda run -n "$CONDA_ENV" python src/drl_pinball/reproduce/phase3_reproduce.py \
        --device "$DEVICE_ID" --scene all; then
        log "[PASS] Phase 3"
    else
        log "[FAIL] Phase 3"
    fi
fi

log ""
log "=== Reproduce tests complete ==="
