#!/usr/bin/env bash
# Suite30 JSPR — ONE log, prior-experiment filesystem conventions.
set -Eeuo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="${AQ_ROOT:-/root/disk_3tb/earlywarning/30072026}"
PYTHON="${PYTHON:-/root/miniconda3/envs/Huy/bin/python3}"
export OMP_NUM_THREADS="${OMP_NUM_THREADS:-4}"
export MKL_NUM_THREADS="${MKL_NUM_THREADS:-4}"
mkdir -p "$ROOT/log" "$ROOT/results/suite30_joint_portfolio"
LOG="$ROOT/log/suite30_joint_portfolio.log"
RESULTS="$ROOT/results/suite30_joint_portfolio"
run_pipeline() {
  echo "=================================================================="
  echo "[START] $(date -Iseconds) Suite30 JSPR"
  echo "[CODE] $HERE"
  echo "[ROOT] $ROOT"
  echo "[PYTHON] $PYTHON"
  echo "[LOG] $LOG"
  echo "[RESULTS] $RESULTS"
  echo "[CONFIG] folds=1..7 faculties=all seeds=29,129,229"
  echo "[CONFIG] modes=course_only,portfolio_independent,joint_no_attention,joint"
  echo "[CONFIG] epochs=18 patience=5 group_batch_size=64 checkpoints=on"
  printf '[ARGS]';printf ' %q' "$@";printf '\n'
  echo "[PREFLIGHT]"
  "$PYTHON" -u "$HERE/check_suite30_environment.py" \
      --root "$ROOT" \
      --cache-dir "$ROOT/results/cross_faculty_residual_moe/cache" \
      --structured-dir "$ROOT/results/suite12_structured_cache" \
      --output-dir "$RESULTS" \
      --folds 1,2,3,4,5,6,7 --faculties all "$@"
  echo "[TEST] Suite30 model + data + reporting tests"
  (cd "$HERE" && "$PYTHON" -m unittest discover -s tests -v)
  echo "[TRAIN] Suite30 full run"
  (cd "$HERE" && "$PYTHON" -u train_suite30.py \
      --cache-dir "$ROOT/results/cross_faculty_residual_moe/cache" \
      --structured-dir "$ROOT/results/suite12_structured_cache" \
      --suite29-results "$ROOT/results/suite29_enrollment_set" \
      --output-dir "$RESULTS" \
      --folds 1,2,3,4,5,6,7 --faculties all \
      --modes course_only,portfolio_independent,joint_no_attention,joint \
      --seeds 29,129,229 --epochs 18 --patience 5 \
      --validation-blocks 3 --group-batch-size 64 --save-checkpoints "$@")
  echo "[COMPLETE] $(date -Iseconds) Suite30 JSPR"
}
# tee without -a overwrites the previous run: exactly one stable log file.
(
  set -Eeuo pipefail
  trap 's=$?; if ((s==0)); then echo "[RUN STATUS] SUCCESS $(date -Iseconds)"; else echo "[RUN STATUS] FAILURE code=$s $(date -Iseconds)"; fi' EXIT
  run_pipeline "$@"
) 2>&1 | tee "$LOG"
