#!/usr/bin/env bash
set -euo pipefail
: "${PIJON_API_KEY:?Set PIJON_API_KEY privately}"
: "${PIJON_APPLICATION_ID:?Set PIJON_APPLICATION_ID}"
: "${PIJON_RECIPIENT_ID:?Set PIJON_RECIPIENT_ID}"
: "${PIJON_IDEMPOTENCY_KEY:?Set one stable key for this event}"
base="${PIJON_API_BASE_URL:-https://api.pijon.fyi/api/v1}"
result="$(curl --fail-with-body --silent --show-error --max-time 20 \
  --write-out '\n%{http_code}' \
  "${base%/}/notifications" \
  -H "Authorization: Bearer $PIJON_API_KEY" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H "Idempotency-Key: $PIJON_IDEMPOTENCY_KEY" \
  --data "{\"application_id\":\"$PIJON_APPLICATION_ID\",\"recipient_id\":\"$PIJON_RECIPIENT_ID\",\"body\":\"Hello from Pijon docs\"}")"
status="${result##*$'\n'}"
case "$status" in
  200|202) printf '%s\n' "${result%$'\n'*}" ;;
  *) printf 'Unexpected HTTP %s. Preserve this event key.\n' "$status" >&2; exit 1 ;;
esac
