#!/usr/bin/env bash
set -euo pipefail

BASE_URL="${FUTRE_BASE_URL:-https://backend.nexwallet.com}"
API_KEY="${FUTRE_API_KEY:-futre_live_KsJXk0JcHvweuA5K9oYT4a_Py_JRpbji}"
COOKIE_JAR="${COOKIE_JAR:-/tmp/futre-api-example.cookies}"
EMAIL="dev-$(date +%s)@example.com"

echo "1) API catalog"
curl -sS \
  -H "X-FUTRE-API-Key: ${API_KEY}" \
  "${BASE_URL}/api/app/v1/catalog" | jq '{project, modules: [.modules[].name]}'

echo
echo "2) Create account"
curl -sS -c "${COOKIE_JAR}" \
  -H "content-type: application/json" \
  -H "X-FUTRE-API-Key: ${API_KEY}" \
  -d "{\"name\":\"Developer Test\",\"email\":\"${EMAIL}\",\"password\":\"test12345\"}" \
  "${BASE_URL}/api/signup" | jq '{user, addresses: .wallet.addresses}'

echo
echo "3) Get USDT TRC20 deposit address"
curl -sS -b "${COOKIE_JAR}" \
  -H "content-type: application/json" \
  -H "X-FUTRE-API-Key: ${API_KEY}" \
  -d '{"asset":"USDT","network":"TRC20"}' \
  "${BASE_URL}/api/deposit-address" | jq

echo
echo "4) Fetch unread notifications"
curl -sS -b "${COOKIE_JAR}" \
  -H "X-FUTRE-API-Key: ${API_KEY}" \
  "${BASE_URL}/api/popup-notifications?consume=1" | jq

echo
echo "5) Fetch market prices"
curl -sS \
  -H "X-FUTRE-API-Key: ${API_KEY}" \
  "${BASE_URL}/api/crypto-prices" | jq '{source, updatedAt, symbols: (.prices | keys)}'

echo
echo "6) Swap quote"
curl -sS -b "${COOKIE_JAR}" \
  -H "content-type: application/json" \
  -H "X-FUTRE-API-Key: ${API_KEY}" \
  -d '{"fromAsset":"USDT","fromNetwork":"TRC20","toAsset":"BTC","toNetwork":"Bitcoin","amount":"25"}' \
  "${BASE_URL}/api/swap-estimate" | jq

cat <<'NOTE'

Withdrawal example is intentionally not executed by default because it can move
funds when live withdrawals are enabled:

curl -sS -b "$COOKIE_JAR" \
  -H "content-type: application/json" \
  -H "X-FUTRE-API-Key: $API_KEY" \
  -d '{"asset":"USDT","network":"TRC20","amount":"10","toAddress":"T..."}' \
  "$BASE_URL/api/withdrawal-request" | jq
NOTE
