#!/usr/bin/env bash
# Submit the alpha-cv workflow: stacker train on 9Q + 9-fold walk-forward.
#
# Defaults match the validated 2Q config (decision-stride=200,
# scaled training H=1200 × N_par=25 × 8K episodes).
#
# Usage:
#   ./scripts/argo-alpha-cv.sh                     # submit on current HEAD
#   ./scripts/argo-alpha-cv.sh --branch main
#   ./scripts/argo-alpha-cv.sh --decision-stride 100 --horizon 2000

set -euo pipefail

SHA=HEAD
BRANCH=main
DECISION_STRIDE=200
HORIZON=1200
N_TRAIN_PAR=25
N_TRAIN_EPISODES=8000
FOLD_WINDOW=1900000
GPU_POOL=ci-training-l40s
WATCH=false

usage() {
  cat <<EOF
Usage: $0 [OPTIONS]
  --sha <commit>           Git SHA (default: HEAD)
  --branch <branch>        Git branch (default: main)
  --decision-stride <n>    Decisions every N bars (default: 200)
  --horizon <n>            Episode length in bars (default: 1200)
  --n-train-par <n>        Parallel envs during training (default: 25)
  --n-train-episodes <n>   Total training episodes (default: 8000)
  --fold-window <n>        Bars per fold window (default: 1900000)
  --gpu-pool <p>           GPU pool (default: ci-training-l40s)
  --watch                  Follow logs via argo watch
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --sha)               SHA="$2"; shift 2 ;;
    --branch)            BRANCH="$2"; shift 2 ;;
    --decision-stride)   DECISION_STRIDE="$2"; shift 2 ;;
    --horizon)           HORIZON="$2"; shift 2 ;;
    --n-train-par)       N_TRAIN_PAR="$2"; shift 2 ;;
    --n-train-episodes)  N_TRAIN_EPISODES="$2"; shift 2 ;;
    --fold-window)       FOLD_WINDOW="$2"; shift 2 ;;
    --gpu-pool)          GPU_POOL="$2"; shift 2 ;;
    --watch)             WATCH=true; shift ;;
    -h|--help)           usage; exit 0 ;;
    *) echo "Unknown option: $1"; usage; exit 1 ;;
  esac
done

case "$GPU_POOL" in
  ci-training-l40s)        SM=89 ;;
  ci-training-h100*|ci-training-h100x2|ci-training-h100-sxm) SM=90 ;;
  *) echo "Unknown gpu-pool: $GPU_POOL"; exit 1 ;;
esac

echo "Submitting alpha-cv workflow..."
echo "  branch:           $BRANCH"
echo "  sha:              $SHA"
echo "  decision-stride:  $DECISION_STRIDE"
echo "  horizon:          $HORIZON"
echo "  n-train-par:      $N_TRAIN_PAR"
echo "  n-train-episodes: $N_TRAIN_EPISODES"
echo "  fold-window:      $FOLD_WINDOW"
echo "  gpu-pool:         $GPU_POOL (sm_$SM)"

argo submit -n foxhunt --from=wftmpl/alpha-cv \
  -p commit-sha="$SHA" \
  -p git-branch="$BRANCH" \
  -p cuda-compute-cap="$SM" \
  -p gpu-pool="$GPU_POOL" \
  -p decision-stride="$DECISION_STRIDE" \
  -p horizon="$HORIZON" \
  -p n-train-par="$N_TRAIN_PAR" \
  -p n-train-episodes="$N_TRAIN_EPISODES" \
  -p fold-window="$FOLD_WINDOW" \
  $($WATCH && echo "--watch" || echo "")
