#!/usr/bin/env bash
# Send a correctly signed test event to your own receiver, for local development.
# Needs: curl, openssl. This signs with YOUR secret; RelationsAI is not involved.
#   RELATIONSAI_WEBHOOK_SECRET=whsec_... ./sign-and-send.sh [saved-event.json]
set -euo pipefail
: "${RELATIONSAI_WEBHOOK_SECRET:?Set RELATIONSAI_WEBHOOK_SECRET to the secret your receiver uses}"
RECEIVER_URL="${RECEIVER_URL:-http://localhost:8000/webhooks/relationsai}"

if [ $# -gt 0 ]; then
  body=$(cat "$1")
else
  body=$(printf '{"id":"evt_local%s","object":"event","type":"webhook_endpoint.test","created_at":"%s","livemode":false,"api_version":"2026-10-21","data":{"object":{"id":"we_local","object":"webhook_endpoint"}}}' \
    "$(openssl rand -hex 8)" "$(date -u +%Y-%m-%dT%H:%M:%S.000Z)")
fi

t=$(date +%s)
v1=$(printf '%s.%s' "$t" "$body" | openssl dgst -sha256 -hmac "$RELATIONSAI_WEBHOOK_SECRET" | sed 's/^.*= *//')

curl -sS --fail-with-body -X POST "$RECEIVER_URL" \
  -H "Content-Type: application/json" \
  -H "RelationsAI-Signature: t=$t,v1=$v1" \
  --data-binary "$body"
echo
