#!/bin/bash

# Test script for BZZZ Setup API
echo "🧪 Testing BZZZ Setup API Integration"

# Start the server in background
echo "🚀 Starting BZZZ server..."
/tmp/bzzz-setup-test &
SERVER_PID=$!
sleep 3

# Function to make HTTP requests
make_request() {
    local method="$1"
    local url="$2"
    local data="$3"
    
    if [ -n "$data" ]; then
        curl -s -X "$method" \
             -H "Content-Type: application/json" \
             -d "$data" \
             "http://localhost:8080$url"
    else
        curl -s -X "$method" "http://localhost:8080$url"
    fi
}

echo ""
echo "1. Testing status endpoint..."
STATUS_RESPONSE=$(make_request "GET" "/api/status")
echo "   Status response: $STATUS_RESPONSE"

echo ""
echo "2. Testing setup requirement check..."
SETUP_REQUIRED=$(make_request "GET" "/api/setup/required")
echo "   Setup required: $SETUP_REQUIRED"

echo ""
echo "3. Testing system detection..."
SYSTEM_INFO=$(make_request "GET" "/api/setup/system")
echo "   System info: $SYSTEM_INFO" | head -c 200
echo "..."

echo ""
echo "4. Testing repository providers..."
PROVIDERS=$(make_request "GET" "/api/setup/repository/providers")
echo "   Providers: $PROVIDERS"

echo ""
echo "5. Testing repository validation..."
REPO_CONFIG='{"provider":"github","access_token":"dummy","owner":"test","repository":"test"}'
REPO_VALIDATION=$(make_request "POST" "/api/setup/repository/validate" "$REPO_CONFIG")
echo "   Repository validation: $REPO_VALIDATION"

echo ""
echo "6. Testing configuration validation..."
SETUP_CONFIG='{"agent_id":"test-001","capabilities":["general"],"models":["phi3"]}'
CONFIG_VALIDATION=$(make_request "POST" "/api/setup/validate" "$SETUP_CONFIG")
echo "   Config validation: $CONFIG_VALIDATION"

echo ""
echo "🛑 Stopping server..."
kill $SERVER_PID 2>/dev/null
wait $SERVER_PID 2>/dev/null

echo ""
echo "✅ Setup API test completed!"