#!/usr/bin/env bash
# Pull a month of attendance from the Kingdom Metrics public API.
#
# Usage:
#   export KM_API_KEY="km_pub_..."
#   ./curl.sh 2026-04-01 2026-04-30
#
# Get a key at https://app.kingdommetrics.com/dashboard/settings?section=integrations
set -euo pipefail

START="${1:-$(date -u -d '30 days ago' +%F)}"
END="${2:-$(date -u +%F)}"

if [[ -z "${KM_API_KEY:-}" ]]; then
  echo "error: set KM_API_KEY before running" >&2
  exit 1
fi

curl --fail --silent --show-error \
  -H "Authorization: Bearer ${KM_API_KEY}" \
  "https://api.kingdommetrics.com/public/v1/attendance?start_date=${START}&end_date=${END}" \
  | jq '.data[] | "\(.date)  \(.event)  \(.count // "—")  (\(.status))"'
