#!/bin/bash
# Trigger approval card capture after deployment
# Usage: trigger-approval-capture.sh <slug> <type> <changes...>
#
# Types:
#   landing - Landing page at *.landing.symph.co
#   app     - Dev app at *.dev.apps.symph.co
#
# Example:
#   trigger-approval-capture.sh dinq landing "Updated hero section" "New typography"

set -e

SLUG="$1"
TYPE="$2"
shift 2
CHANGES=("$@")

if [ -z "$SLUG" ] || [ -z "$TYPE" ]; then
  echo "Usage: trigger-approval-capture.sh <slug> <type> <changes...>"
  echo "Types: landing, app"
  exit 1
fi

# Build JSON array of changes
CHANGES_JSON="["
first=true
for change in "${CHANGES[@]}"; do
  if [ "$first" = true ]; then
    first=false
  else
    CHANGES_JSON+=","
  fi
  CHANGES_JSON+="\"$change\""
done
CHANGES_JSON+="]"

# API endpoint
API_URL="${SYMPHONY_API_URL:-http://localhost:4000}/api/approval/capture"

echo "[ApprovalCapture] Triggering capture for $SLUG ($TYPE)"
echo "[ApprovalCapture] Changes: ${CHANGES[*]}"

# Fire-and-forget async request (don't wait for response)
curl -s -X POST "$API_URL" \
  -H "Content-Type: application/json" \
  -d "{
    \"productSlug\": \"$SLUG\",
    \"type\": \"$TYPE\",
    \"changes\": $CHANGES_JSON,
    \"triggeredBy\": \"deploy-script\"
  }" &

# Don't wait for curl to complete
disown

echo "[ApprovalCapture] Capture triggered (async)"
