#!/bin/sh
# Ask on your phone before deploying. Exits 0 only if you tap the first option.
#   ./approval-gate.sh "Deploy v2.3 to production?" && ./deploy.sh
API=https://ujtuxanqlzmhnrzijfoo.supabase.co/functions/v1/agent-api
QUESTION=${1:-"Deploy to production?"}
ID=$(curl -s $API -H "Authorization: Bearer $DROPLET_TOKEN" -H "Content-Type: application/json" -H "x-region: us-east-1" \
  -d "{\"action\":\"ask\",\"question\":\"$QUESTION\",\"risk\":\"high\",\"options\":[\"Deploy\",\"Stop\"]}" \
  | sed -n 's/.*"request_id":"\([^"]*\)".*/\1/p')
while :; do
  R=$(curl -s $API -H "Authorization: Bearer $DROPLET_TOKEN" -H "Content-Type: application/json" -H "x-region: us-east-1" -d "{\"action\":\"get_answer\",\"request_id\":\"$ID\",\"wait_seconds\":45}")
  case "$R" in
    *'"status":"answered"'*) echo "$R" | grep -q '"answer":"Deploy"' && exit 0 || exit 1 ;;
    *'"status":"cancelled"'*) exit 1 ;;
  esac
done
