#!/bin/bash
# Create Test Member for AS4 Settlement
# Creates a test member via API

set -e

BASE_URL="${AS4_BASE_URL:-http://localhost:3000}"
AUTH_TOKEN="${AS4_AUTH_TOKEN:-}"

MEMBER_ID="${1:-TEST-MEMBER-$(date +%s)}"
ORG_NAME="${2:-Test Bank}"

echo "Creating test member: $MEMBER_ID"

# Generate test certificate fingerprint (random)
FINGERPRINT=$(openssl rand -hex 32 | sed 's/\(..\)/\1:/g; s/:$//' | tr '[:lower:]' '[:upper:]')

curl -X POST "$BASE_URL/api/v1/as4/directory/members" \
  -H "Content-Type: application/json" \
  ${AUTH_TOKEN:+-H "Authorization: Bearer $AUTH_TOKEN"} \
  -d "{
    \"memberId\": \"$MEMBER_ID\",
    \"organizationName\": \"$ORG_NAME\",
    \"as4EndpointUrl\": \"https://test-bank.example.com/as4\",
    \"tlsCertFingerprint\": \"$FINGERPRINT\",
    \"allowedMessageTypes\": [\"DBIS.SI.202\", \"DBIS.SI.202COV\"],
    \"routingGroups\": [\"DEFAULT\"],
    \"capacityTier\": 3
  }" | jq '.'

echo ""
echo "Test member created: $MEMBER_ID"
