#!/bin/bash
# Register SolaceNet capabilities for tokenization

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
SOLACENET_API_URL="${SOLACENET_API_URL:-http://localhost:3000}"
SOLACENET_API_KEY="${SOLACENET_API_KEY:-}"

if [ -z "$SOLACENET_API_KEY" ]; then
    echo -e "${RED}Error: SOLACENET_API_KEY not set${NC}"
    exit 1
fi

echo -e "${GREEN}Registering SolaceNet capabilities for tokenization...${NC}"

# Capabilities to register
declare -A CAPABILITIES=(
    ["tokenization.mint"]="Mint tokenized assets"
    ["tokenization.transfer"]="Transfer tokenized assets"
    ["tokenization.redeem"]="Redeem tokenized assets"
    ["tokenization.view"]="View tokenized assets"
)

# Register each capability
for capability_id in "${!CAPABILITIES[@]}"; do
    description="${CAPABILITIES[$capability_id]}"
    
    echo -e "${YELLOW}Registering capability: $capability_id${NC}"
    
    response=$(curl -s -X POST "${SOLACENET_API_URL}/api/v1/solacenet/capabilities" \
        -H "Authorization: Bearer ${SOLACENET_API_KEY}" \
        -H "Content-Type: application/json" \
        -d "{
            \"id\": \"${capability_id}\",
            \"name\": \"${description}\",
            \"description\": \"${description}\",
            \"category\": \"tokenization\",
            \"enabled\": true
        }")
    
    if echo "$response" | grep -q "id"; then
        echo -e "${GREEN}✓ Capability $capability_id registered successfully${NC}"
    else
        # Check if capability already exists
        if echo "$response" | grep -q "already exists"; then
            echo -e "${YELLOW}⚠ Capability $capability_id already exists${NC}"
        else
            echo -e "${RED}✗ Failed to register capability $capability_id${NC}"
            echo "Response: $response"
        fi
    fi
done

echo -e "${GREEN}SolaceNet capability registration complete!${NC}"
