#!/usr/bin/env sh
set -eu

URL="https://pharosatlas.com/api/answer"
BODY='{"question":"Does security film make ordinary glass bulletproof?","intent":"verify_claim","language":"en"}'

# 1. Read the canonical x402 v2 offer from the PAYMENT-REQUIRED header.
curl --fail-with-body --silent --show-error \
  --dump-header - --output /dev/null \
  --request POST "$URL" \
  --header 'Content-Type: application/json' \
  --data "$BODY" || test "$?" -eq 22

# 2. Sign the exact Base-mainnet USDC terms with an x402 v2 wallet client.
# 3. Retry the identical URL, method, and body. Put the base64 x402 payload in
#    PAYMENT_SIGNATURE; never paste a private key into this command or a log.
if test -z "$(printenv PAYMENT_SIGNATURE 2>/dev/null)"; then
  echo "Set PAYMENT_SIGNATURE to the signed x402 v2 payload" >&2
  exit 2
fi
curl --fail-with-body --silent --show-error \
  --request POST "$URL" \
  --header 'Content-Type: application/json' \
  --header "PAYMENT-SIGNATURE: $PAYMENT_SIGNATURE" \
  --data "$BODY"
