#!/usr/bin/env bash
# Read every contact created after a date, page by page. Needs: curl, jq.
set -euo pipefail
BASE="${RELATIONSAI_API_BASE:-https://voice-api.relationsai.com/v1}"
CREATED_AFTER="${CREATED_AFTER:-2026-01-01T00:00:00.000Z}"
cursor=""

while :; do
  # The cursor only works with the same filters that produced it.
  page=$(curl -sS --fail-with-body -G "$BASE/contacts" \
    --data-urlencode "limit=100" \
    --data-urlencode "created_after=$CREATED_AFTER" \
    ${cursor:+--data-urlencode "cursor=$cursor"} \
    -H "Authorization: Bearer $RELATIONSAI_API_KEY") || { echo "$page" >&2; exit 1; }
  jq -r '.data[] | "\(.id) \(.phone_number) consent=\(.consent_granted) do_not_call=\(.do_not_call)"' <<<"$page"
  [ "$(jq -r '.has_more' <<<"$page")" = "true" ] || break
  cursor=$(jq -r '.next_cursor' <<<"$page")
done
