#!/bin/bash
# API Setup Script
# Creates a new API integration directory with templates

if [ -z "$1" ]; then
    echo "Usage: $0 <api-name>"
    echo "Example: $0 stripe"
    exit 1
fi

API_NAME="$1"
WORKSPACE_DIR="/home/openclaw/.openclaw/workspace"
API_DIR="$WORKSPACE_DIR/apis/$API_NAME"

# Create directory
mkdir -p "$API_DIR"

# Create config template
cat > "$API_DIR/config.json" << 'EOF'
{
  "api_key": "YOUR_API_KEY_HERE",
  "endpoint": "https://api.example.com/v1",
  "timeout": 30,
  "headers": {
    "Content-Type": "application/json"
  }
}
EOF

# Create test script
cat > "$API_DIR/test.sh" << EOF
#!/bin/bash
# Test API connection

CONFIG_FILE="\$(dirname "\$0")/config.json"

# Check config exists
if [ ! -f "\$CONFIG_FILE" ]; then
    echo "Error: config.json not found"
    exit 1
fi

# Extract values (requires jq)
API_KEY=\$(jq -r '.api_key' "\$CONFIG_FILE")
ENDPOINT=\$(jq -r '.endpoint' "\$CONFIG_FILE")

if [ "\$API_KEY" = "YOUR_API_KEY_HERE" ]; then
    echo "Error: Please set your API key in config.json"
    exit 1
fi

echo "Testing \$ENDPOINT..."
curl -s -H "Authorization: Bearer \$API_KEY" "\$ENDPOINT" || echo "Connection test complete"
EOF

chmod +x "$API_DIR/test.sh"

echo "✅ Created API integration: $API_NAME"
echo "📁 Location: $API_DIR"
echo ""
echo "Next steps:"
echo "1. Edit $API_DIR/config.json with your credentials"
echo "2. Run $API_DIR/test.sh to verify connection"
