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

ROOT="${AQ_ROOT:-/root/disk_3tb/earlywarning/30072026}"
THIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

LOG_DIR="$ROOT/log"
OUT_DIR="${SUITE19_OUT_DIR:-$ROOT/results/suite19_original}"

mkdir -p "$LOG_DIR" "$OUT_DIR"

if [[ -z "${SUITE17_CODE_DIR:-}" && -d "$ROOT/suite17" ]]; then
    export SUITE17_CODE_DIR="$ROOT/suite17"
fi

LOG="${SUITE19_LOG:-$LOG_DIR/suite19_original.log}"

if [[ "${SUITE19_LOG_APPEND:-0}" == "1" ]]; then
    exec > >(tee -a "$LOG") 2>&1
else
    exec > >(tee "$LOG") 2>&1
fi

echo "[RUN] Suite19 ORIGINAL started at $(date -Is)"
echo "[RUN] code=$THIS_DIR"
echo "[RUN] output=$OUT_DIR"
echo "[RUN] log=$LOG"

python3 -u "$THIS_DIR/check_suite19_environment_final.py" \
    --project_root "$ROOT"

echo
echo "[RUN] Environment passed. Starting Suite19 FINAL."
echo

python3 -u "$THIS_DIR/train_suite19_final.py" \
    --cache_dir "$ROOT/results/cross_faculty_residual_moe/cache" \
    --structured_cache_dir "$ROOT/results/suite12_structured_cache" \
    --out_dir "$OUT_DIR" \
    --faculties all \
    --folds 1,2,3,4,5,6,7 \
    --dev_folds 1,2,3,4,5 \
    --holdout_folds 6,7 \
    --encoders rfm,tabm,modern_nca,modern_nca_hard,modern_nca_mechanism \
    --latent_dims 32,64 \
    --seeds 42,43,44 \
    --device auto \
    \
    --tabm_backend official \
    --tabm_arch_type tabm \
    --tabm_depths 3 \
    --tabm_ensemble_size 8 \
    --tabm_dropout 0.10 \
    --tabm_epochs 40 \
    --tabm_batch_size 1024 \
    --tabm_lr 0.002 \
    \
    --nca_hidden_dim 128 \
    --nca_epochs 40 \
    --nca_batch_size 512 \
    --nca_temperatures 0.05,0.10,0.20 \
    --nca_aux_bce_grid 0,0.10 \
    --nca_lr 0.002 \
    --nca_hard_strength 2.0 \
    --nca_mechanism_tau 1.0 \
    --nca_retrieval_k 32 \
    --nca_retrieval_query_chunk 256 \
    --nca_retrieval_memory_chunk 8192 \
    --rfm_mem_gb 4 \
    \
    --rfm_backend official \
    --rfm_recursions 1,2,3 \
    --rfm_centers 512 \
    --rfm_agop_rows 4096 \
    --rfm_ridge 0.01 \
    --rfm_bandwidths 2.0 \
    \
    --representation_min_prior_blocks 4 \
    --representation_min_train_rows 5000 \
    --representation_min_failures 50 \
    --min_oof_representation_coverage 0.40 \
    \
    --probe_min_prior_blocks 4 \
    --probe_min_train_rows 5000 \
    --probe_min_failures 50 \
    --probe_hgb_iter 250 \
    --probe_max_train_rows 180000 \
    \
    --residual_eta_grid 0,0.05,0.10,0.20,0.30 \
    --residual_iter_candidates 20,40,60,80 \
    --residual_min_prior_blocks 4 \
    --residual_min_faculties 8 \
    --residual_min_failures 120 \
    --residual_min_rows 5000 \
    \
    --class_beta 0.9999 \
    --threshold_beta 2.0 \
    --bootstrap_samples 4000 \
    \
    --suite14_results_dir "$ROOT/results/suite14_authority_safe_collaborative_moe" \
    --suite16_results_dir "$ROOT/results/suite16_conditional_tree_boosting" \
    --suite17_results_dir "$ROOT/results/suite17_robust_forward_specialist_boosting" \
    --suite18_results_dir "$ROOT/results/suite18_fmm_forward_specialist_boosting" \
    --v2_results_dir "$ROOT/results/final_train_v2" \
    --council_results_dir "$ROOT/results/suite4/2_competence_council" \
    \
    --fail_fast \
    "$@"

echo
echo "[RUN PASS] Suite19 FINAL completed at $(date -Is)"