#!/usr/bin/env bash
# cURL + jq. Do not enable shell tracing or print complete responses.
set -euo pipefail
base="${TURBOPULSA_API_URL:-https://api.turbopulsa.com/sandbox/v1}"
case "$base" in https://api.turbopulsa.com/*) ;; http://127.0.0.1:*|http://localhost:*) test "${TURBOPULSA_FIXTURE:-}" = 1 ;; *) exit 1 ;; esac
: "${TURBOPULSA_API_KEY:?Set a server key}"
: "${TURBOPULSA_CLIENT_REFERENCE:?Persist your purchase reference}"
: "${TURBOPULSA_QUOTE_UUID:?Persist quote request UUID}"
: "${TURBOPULSA_CONFIRM_UUID:?Persist confirm request UUID}"
api() { curl --silent --show-error --max-time 15 -H "Authorization: Bearer $TURBOPULSA_API_KEY" -H 'Accept: application/json' "$@"; }
catalog_path=catalog
quote_path=quotes
if test "${TURBOPULSA_KIND:-prepaid}" = postpaid; then catalog_path=postpaid/catalog; quote_path=postpaid/inquiries; fi
catalog=$(api "$base/$catalog_path?per_page=50")
code="${TURBOPULSA_PRODUCT_CODE:-$(printf '%s' "$catalog" | jq -r '.data[0].code')}"
destination="${TURBOPULSA_DESTINATION_JSON:-}"
if test -z "$destination"; then destination='{"phone":"081200000000"}'; fi
if test "${TURBOPULSA_KIND:-prepaid}" = postpaid; then
  body=$(jq -n --arg code "$code" --arg ref "$TURBOPULSA_CLIENT_REFERENCE" --arg customer "${TURBOPULSA_CUSTOMER_NO:-001234567890}" '{product_code:$code,client_reference:$ref,customer_no:$customer}')
else
  body=$(jq -n --arg code "$code" --arg ref "$TURBOPULSA_CLIENT_REFERENCE" --argjson destination "$destination" '{product_code:$code,client_reference:$ref,destination:$destination}')
fi
quote=$(api "$base/$quote_path" -X POST -H 'Content-Type: application/json' -H "Idempotency-Key: $TURBOPULSA_QUOTE_UUID" --data "$body")
if test "${TURBOPULSA_KIND:-prepaid}" = postpaid; then
  for attempt in 1 2 3 4 5; do
    if printf '%s' "$quote" | jq -e '.data.id' >/dev/null; then break; fi
    inquiry=$(printf '%s' "$quote" | jq -r '.inquiry.id // empty')
    if test -z "$inquiry"; then break; fi
    sleep 1
    quote=$(api "$base/postpaid/inquiries/$inquiry")
  done
fi
printf '%s' "$quote" | jq -e '.data.id' >/dev/null
printf '%s' "$quote" | jq '{id:.data.id,name:.data.name,price:.data.price,currency:.data.currency,status:.data.status}'
printf 'Periksa tujuan lokal dan total. Ketik BAYAR untuk konfirmasi: '
read -r approval
if test "$approval" != BAYAR; then exit 0; fi
id=$(printf '%s' "$quote" | jq -r '.data.id')
body=$(printf '%s' "$quote" | jq '{confirmation_token:.data.confirmation_token}')
if ! confirmed=$(api "$base/orders/$id/confirm" -X POST -H 'Content-Type: application/json' -H "Idempotency-Key: $TURBOPULSA_CONFIRM_UUID" --data "$body"); then
  confirmed=$(api "$base/orders/$id")
fi
printf '%s' "$confirmed" | jq -e '.data.id' >/dev/null
# A 409 price change is NOT automatically re-confirmed.
if printf '%s' "$confirmed" | jq -e '.error' >/dev/null; then printf 'Periksa error dan quote baru; jangan membayar otomatis.\n'; exit 1; fi
current=$(api "$base/orders/$id")
printf '%s' "$current" | jq '{id:.data.id,status:.data.status,charged:.data.charged,currency:.data.currency}'
