#!/usr/bin/env bash
set -Eeuo pipefail
case "${1:-}" in
  validate)
    command -v node >/dev/null
    node -e 'const [a,b]=process.versions.node.split(".").map(Number); if(a<20 || (a===20 && b<19)) process.exit(1)'
    command -v python3 >/dev/null
    [[ -f "$ROBORTO_PROJECT_ROOT/node_modules/vite/bin/vite.js" ]]
    ;;
  prepare)
    python3 "$ROBORTO_PROJECT_ROOT/scripts/testtask_snapshot.py"
    ;;
  start-backend)
    # Frontend-only app; keep the runner's backend lifecycle slot alive.
    exec tail -f /dev/null
    ;;
  start-frontend)
    cd "$ROBORTO_WORKTREE"
    exec node node_modules/vite/bin/vite.js --host 127.0.0.1 --port 5187 --strictPort
    ;;
  credentials)
    echo 'Local app; no login. Snapshot storage is isolated by origin/profile. Use Tuesday or Saturday Morning to test EMOM.'
    ;;
  cleanup)
    # Runner owns its processes and disposable browser profile. Retain snapshot evidence.
    ;;
  *) echo 'Unknown testtask hook operation' >&2; exit 2 ;;
esac
