#!/usr/bin/env bash
# Legacy entrypoint: runs a short demo against Better Auth–style routes.
# See README.md in this directory for the full script index (examples/curl/).
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export ATHENA_AUTH_ORIGIN="${ATHENA_AUTH_ORIGIN:-http://localhost:3000}"

echo "=== Athena Auth examples (wrapped) ==="
echo "Origin: ${ATHENA_AUTH_ORIGIN}  (override with ATHENA_AUTH_ORIGIN)"
echo ""

echo "--- GET /ok ---"
"${ROOT}/curl/probe_ok.sh"
echo ""

EMAIL="${EMAIL:-user@example.com}"
PASSWORD="${PASSWORD:-password123}"

echo "--- POST /sign-up/email ---"
SIGNUP=$(
  curl -sS -X POST "${ATHENA_AUTH_ORIGIN}/api/auth/sign-up/email" \
    -H "Content-Type: application/json" \
    -d "{\"name\":\"Demo User\",\"email\":\"${EMAIL}\",\"password\":\"${PASSWORD}\"}"
)
echo "${SIGNUP}" | if command -v jq >/dev/null 2>&1; then jq .; else cat; fi

echo ""
echo "--- POST /sign-in/email ---"
SIGNIN=$(
  curl -sS -X POST "${ATHENA_AUTH_ORIGIN}/api/auth/sign-in/email" \
    -H "Content-Type: application/json" \
    -d "{\"email\":\"${EMAIL}\",\"password\":\"${PASSWORD}\"}"
)
echo "${SIGNIN}" | if command -v jq >/dev/null 2>&1; then jq .; else cat; fi

TOKEN=""
if command -v jq >/dev/null 2>&1; then
  TOKEN="$(echo "${SIGNIN}" | jq -r '.token // empty')"
fi
if [[ -z "${TOKEN}" ]]; then
  TOKEN="$(echo "${SIGNIN}" | grep -o '"token":"[^"]*' | head -1 | cut -d'"' -f4 || true)"
fi

if [[ -z "${TOKEN}" ]]; then
  echo ""
  echo "No token in sign-in response; skipping /get-session. Install jq for reliable parsing."
  exit 0
fi

echo ""
echo "--- GET /get-session ---"
curl -sS -X GET "${ATHENA_AUTH_ORIGIN}/api/auth/get-session" \
  -H "Authorization: Bearer ${TOKEN}" | if command -v jq >/dev/null 2>&1; then jq .; else cat; fi

echo ""
echo "=== Done. See curl/ for many more one-shot scripts. ==="
