#!/bin/bash

# Test MCP SDK Integration
# This script verifies the server works with standard MCP clients

set -e

echo "🧪 Testing MCP SDK Integration..."
echo ""

# Build the project
echo "📦 Building project..."
npm run build
echo "✅ Build successful"
echo ""

# Start the server in background
echo "🚀 Starting server..."
node dist/examples/basic-setup.js &
SERVER_PID=$!

# Wait for server to start
sleep 3

# Function to cleanup
cleanup() {
  echo ""
  echo "🧹 Cleaning up..."
  kill $SERVER_PID 2>/dev/null || true
}
trap cleanup EXIT

echo "✅ Server started (PID: $SERVER_PID)"
echo ""

# Test 1: Health check
echo "Test 1: Health check"
RESPONSE=$(curl -s http://localhost:3000/health)
if echo "$RESPONSE" | grep -q "ok"; then
  echo "✅ Health check passed"
else
  echo "❌ Health check failed: $RESPONSE"
  exit 1
fi
echo ""

# Test 2: Standard MCP endpoint - List tools
echo "Test 2: List tools via standard MCP endpoint"
RESPONSE=$(curl -s -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/list"
  }')

if echo "$RESPONSE" | grep -q "echo"; then
  echo "✅ Tools list successful"
  echo "   Found tools: $(echo "$RESPONSE" | grep -o '"name":"[^"]*"' | cut -d'"' -f4 | tr '\n' ', ')"
else
  echo "❌ Tools list failed: $RESPONSE"
  exit 1
fi
echo ""

# Test 3: Call a tool
echo "Test 3: Call echo tool"
RESPONSE=$(curl -s -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/call",
    "params": {
      "name": "echo",
      "arguments": {
        "message": "Hello from MCP SDK!"
      }
    }
  }')

if echo "$RESPONSE" | grep -q "Hello from MCP SDK"; then
  echo "✅ Tool call successful"
else
  echo "❌ Tool call failed: $RESPONSE"
  exit 1
fi
echo ""

# Test 4: List resources
echo "Test 4: List resources"
RESPONSE=$(curl -s -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "resources/list"
  }')

if echo "$RESPONSE" | grep -q "Greeting Resource"; then
  echo "✅ Resources list successful"
else
  echo "❌ Resources list failed: $RESPONSE"
  exit 1
fi
echo ""

# Test 5: Read a resource
echo "Test 5: Read a resource"
RESPONSE=$(curl -s -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 4,
    "method": "resources/read",
    "params": {
      "uri": "example://greeting/world"
    }
  }')

if echo "$RESPONSE" | grep -q "Hello"; then
  echo "✅ Resource read successful"
else
  echo "❌ Resource read failed: $RESPONSE"
  exit 1
fi
echo ""

# Test 6: Webhook subscription endpoint (extension)
echo "Test 6: Webhook subscription endpoint (extension)"
RESPONSE=$(curl -s -X POST http://localhost:3000/mcp/resources/subscribe \
  -H "Content-Type: application/json" \
  -d '{
    "uri": "example://greeting/alice",
    "callbackUrl": "https://example.com/webhook",
    "callbackSecret": "test-secret"
  }')

if echo "$RESPONSE" | grep -q "subscriptionId"; then
  echo "✅ Webhook subscription successful"
  SUBSCRIPTION_ID=$(echo "$RESPONSE" | grep -o '"subscriptionId":"[^"]*"' | cut -d'"' -f4)
  echo "   Subscription ID: $SUBSCRIPTION_ID"
else
  echo "❌ Webhook subscription failed: $RESPONSE"
  exit 1
fi
echo ""

echo "🎉 All tests passed!"
echo ""
echo "✨ MCP SDK Integration is working correctly!"
echo ""
echo "You can now:"
echo "  - Use with MCP Inspector: npx @modelcontextprotocol/inspector http://localhost:3000/mcp"
echo "  - Configure Claude Desktop to use: http://localhost:3000/mcp"
echo "  - Integrate with any standard MCP client"
echo ""
