#!/usr/bin/env bash
set -euo pipefail

ROOT="${AQ_ROOT:-/root/disk_3tb/earlywarning/30072026}"

THIS_DIR="$(
    cd "$(dirname "${BASH_SOURCE[0]}")"
    pwd
)"

OUT_DIR="$ROOT/results/suite19_original"

FOLD_DIR="$OUT_DIR/fold_1"

SALVAGE_DIR="$FOLD_DIR/salvage"

mkdir -p \
    "$SALVAGE_DIR" \
    "$ROOT/log/archive"

STAMP="$(date +%Y%m%d_%H%M%S)"

ORIGINAL_LOG="$ROOT/log/suite19_original.log"

RESUME_LOG="$ROOT/log/suite19_fold1_resume_${STAMP}.log"

PRE_MANIFEST="$SALVAGE_DIR/fold_1_salvage_manifest.json"

POST_MANIFEST="$SALVAGE_DIR/fold_1_post_resume_manifest_${STAMP}.json"


echo "===================================================================================================="
echo "SUITE19 FOLD-1 SALVAGE / RESUME"
echo "===================================================================================================="
echo "root=$ROOT"
echo "output=$OUT_DIR"
echo "resume_log=$RESUME_LOG"
echo


# ----------------------------------------------------------------------
# Never permit a resume launcher to contain force flags.
# ----------------------------------------------------------------------
for arg in "$@"; do
    case "$arg" in
        --force_encoder|--force_probe)
            echo "[FATAL] $arg is forbidden in salvage/resume mode."
            exit 10
            ;;
    esac
done


# ----------------------------------------------------------------------
# Preserve the two-day log before anything else.
# ----------------------------------------------------------------------
if [[ -f "$ORIGINAL_LOG" ]]; then
    cp -a \
        "$ORIGINAL_LOG" \
        "$ROOT/log/archive/suite19_original_before_resume_${STAMP}.log"

    echo "[ARCHIVE] original log preserved"
fi


# ----------------------------------------------------------------------
# Validate and fingerprint the expensive Fold-1 work.
# ----------------------------------------------------------------------
python3 -u "$THIS_DIR/suite19_resume.py" snapshot \
    --out_dir "$OUT_DIR" \
    --fold 1 \
    --encoders tabm,modern_nca,modern_nca_hard,modern_nca_mechanism,rfm \
    --seeds 42,43,44 \
    --expect_complete tabm,modern_nca,modern_nca_hard,modern_nca_mechanism \
    --expect_pending rfm \
    --manifest "$PRE_MANIFEST"


echo
echo "[SALVAGE] Pre-resume manifest passed."
echo


# ----------------------------------------------------------------------
# Full production environment check.
# ----------------------------------------------------------------------
python3 -u \
    "$THIS_DIR/check_suite19_environment_final.py" \
    --project_root "$ROOT"


echo
echo "[RESUME] Starting Fold 1 only."
echo "[RESUME] Existing TabM/NCA caches MUST be reused."
echo


# ----------------------------------------------------------------------
# Run only Fold 1.
#
# We intentionally retain the full encoder list. Existing valid caches
# are loaded under the exact original cache signatures, while RFM is the
# only branch expected to perform new expensive work.
# ----------------------------------------------------------------------
set +e

SUITE19_LOG="$RESUME_LOG" \
SUITE19_OUT_DIR="$OUT_DIR" \
bash "$THIS_DIR/run_suite19.sh" \
    --folds 1 \
    "$@"

TRAIN_RC=$?

set -e


echo
echo "[VERIFY] Checking that salvaged caches were not rewritten."
echo

python3 -u \
    "$THIS_DIR/suite19_resume.py" verify \
    --manifest "$PRE_MANIFEST"


echo
echo "[POST-RESUME SNAPSHOT]"
echo

python3 -u \
    "$THIS_DIR/suite19_resume.py" snapshot \
    --out_dir "$OUT_DIR" \
    --fold 1 \
    --encoders tabm,modern_nca,modern_nca_hard,modern_nca_mechanism,rfm \
    --seeds 42,43,44 \
    --expect_complete tabm,modern_nca,modern_nca_hard,modern_nca_mechanism,rfm \
    --manifest "$POST_MANIFEST"


if [[ "$TRAIN_RC" -ne 0 ]]; then
    echo
    echo "[RESUME WARNING]"
    echo "Trainer exited with code $TRAIN_RC."
    echo "The previously salvaged expensive caches remain intact."
    echo "Inspect: $RESUME_LOG"
    exit "$TRAIN_RC"
fi


echo
echo "===================================================================================================="
echo "[FOLD-1 RESUME PASS]"
echo "All five representation branches are now complete."
echo "Original expensive caches remained byte-identical."
echo "===================================================================================================="
echo
echo "resume log:   $RESUME_LOG"
echo "pre manifest: $PRE_MANIFEST"
echo "post manifest:$POST_MANIFEST"