#!/usr/bin/env bash
# Regenerate fxcache on PVC — uses the train workflow's ensure-binary + ensure-fxcache steps.
# Submits a lightweight workflow that compiles the binary and regenerates the feature cache.
# No GPU needed — runs entirely on ci-compile-cpu.
#
# Usage: ./scripts/argo-precompute.sh [--branch main] [--watch]
set -euo pipefail

BRANCH="main"
WATCH=false

while [[ $# -gt 0 ]]; do
  case "$1" in
    --branch) BRANCH="$2"; shift 2;;
    --watch)  WATCH=true; shift;;
    -h|--help)
      echo "Usage: $0 [--branch <branch>] [--watch]"
      echo ""
      echo "Regenerates fxcache on PVC with current code."
      echo "Uses ensure-binary + ensure-fxcache from the train workflow."
      echo "Runs on ci-compile-cpu — no GPU required."
      echo ""
      echo "The fxcache auto-invalidates on version mismatch"
      echo "(FXCACHE_VERSION in fxcache.rs). Old cache is deleted"
      echo "and regenerated automatically."
      exit 0;;
    *) echo "Unknown option: $1"; exit 1;;
  esac
done

echo "=== Argo Precompute fxcache ==="
echo "  branch: $BRANCH"
echo ""

# Submit a training workflow with 0 epochs — it will compile + ensure-fxcache, then stop.
# hyperopt-trials=0 + train-epochs=0 means only ensure-binary + ensure-fxcache run.
WORKFLOW_NAME=$(argo submit -n foxhunt --from=wftmpl/train \
  -p git-branch="$BRANCH" \
  -p hyperopt-trials=0 \
  -p train-epochs=0 \
  -o name 2>&1)

echo "Submitted: $WORKFLOW_NAME"
echo "  ensure-binary → ensure-fxcache (compile + regenerate)"
echo ""
echo "Monitor: kubectl -n foxhunt logs -f $WORKFLOW_NAME --tail=50"

if $WATCH; then
  echo ""
  echo "Watching..."
  argo watch -n foxhunt "$WORKFLOW_NAME"
fi
