#!/usr/bin/env bash
# Add a number to the do-not-call list. Safe to repeat. Needs: curl, jq.
set -euo pipefail
BASE="${RELATIONSAI_API_BASE:-https://voice-api.relationsai.com/v1}"
: "${PHONE:?Set PHONE to the number in E.164 format, for example +12025550107}"

out=$(mktemp)
status=$(curl -sS -o "$out" -w '%{http_code}' -X POST "$BASE/dnc-entries" \
  -H "Authorization: Bearer $RELATIONSAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --arg phone "$PHONE" '{phone_number: $phone}')")
case "$status" in
  201) echo "Added $(jq -r '.phone_number' "$out") as $(jq -r '.id' "$out")" ;;
  200) echo "Already listed as $(jq -r '.id' "$out")" ;;
  *) cat "$out" >&2; exit 1 ;;
esac
