#!/bin/bash

# Test script to demonstrate MCP server functionality for deploying hello-world-example

echo "============================================"
echo "MCP Server Deployment Test"
echo "Project: /home/chka/lab/hello-world-example"
echo "============================================"
echo

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

# Test user credentials (for demo)
TEST_EMAIL="test-mcp-$(date +%s)@example.com"
TEST_NAME="MCP Test User"
TEST_PASSWORD="Test123!MCP"
PROJECT_NAME="hello-world-mcp-$(date +%s)"

echo -e "${YELLOW}Step 1: Testing MCP Server Protocol${NC}"
echo "---------------------------------------"

# Test MCP initialization
INIT_REQUEST='{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"1.0.0","capabilities":{},"clientInfo":{"name":"deploy-test","version":"1.0.0"}},"id":1}'
echo "$INIT_REQUEST" | node /home/chka/NetBeansProjects/gitlab_backend/mcp-server/index.js 2>/dev/null | head -1 | grep -q "gitlab-backend-mcp"

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✅ MCP server initialized successfully${NC}"
else
    echo "❌ MCP server initialization failed"
    exit 1
fi

echo
echo -e "${YELLOW}Step 2: Creating User via MCP${NC}"
echo "---------------------------------------"
echo "Email: $TEST_EMAIL"
echo "Name: $TEST_NAME"
echo

# Create user using MCP tool
USER_REQUEST=$(cat <<EOF
{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"1.0.0","capabilities":{},"clientInfo":{"name":"deploy-test","version":"1.0.0"}},"id":1}
{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_user_create","arguments":{"email":"$TEST_EMAIL","name":"$TEST_NAME","password":"$TEST_PASSWORD"}},"id":2}
EOF
)

echo "Calling mlgym_user_create..."
USER_RESPONSE=$(echo "$USER_REQUEST" | node /home/chka/NetBeansProjects/gitlab_backend/mcp-server/index.js 2>/dev/null | tail -1)

if echo "$USER_RESPONSE" | grep -q '"content"'; then
    echo -e "${GREEN}✅ User created successfully via MCP${NC}"
    echo "$USER_RESPONSE" | python3 -m json.tool | head -20
else
    echo "❌ User creation failed"
    echo "$USER_RESPONSE"
fi

echo
echo -e "${YELLOW}Step 3: Initializing Project with Deployment${NC}"
echo "---------------------------------------"
echo "Project Name: $PROJECT_NAME"
echo "Local Path: /home/chka/lab/hello-world-example"
echo

# Initialize project with deployment
PROJECT_REQUEST=$(cat <<EOF
{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"1.0.0","capabilities":{},"clientInfo":{"name":"deploy-test","version":"1.0.0"}},"id":1}
{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_project_init","arguments":{"name":"$PROJECT_NAME","description":"Hello World Example deployed via MCP","enable_deployment":true,"local_path":"/home/chka/lab/hello-world-example"}},"id":3}
EOF
)

echo "Calling mlgym_project_init..."
PROJECT_RESPONSE=$(echo "$PROJECT_REQUEST" | node /home/chka/NetBeansProjects/gitlab_backend/mcp-server/index.js 2>/dev/null | tail -1)

if echo "$PROJECT_RESPONSE" | grep -q '"content"'; then
    echo -e "${GREEN}✅ Project initialized with deployment via MCP${NC}"
    echo "$PROJECT_RESPONSE" | python3 -m json.tool | head -30

    # Extract deployment URL if available
    DEPLOYMENT_URL=$(echo "$PROJECT_RESPONSE" | grep -o 'https://[^"]*' | grep -v gitlab | head -1)
    if [ ! -z "$DEPLOYMENT_URL" ]; then
        echo
        echo -e "${GREEN}🚀 Deployment URL: $DEPLOYMENT_URL${NC}"
    fi
else
    echo "❌ Project initialization failed"
    echo "$PROJECT_RESPONSE"
fi

echo
echo "============================================"
echo "MCP Deployment Test Complete"
echo "============================================"
echo
echo "To use with Claude in natural language:"
echo "1. 'Create a user account for john@example.com'"
echo "2. 'Deploy my hello-world project to GitLab and Coolify'"
echo "3. 'Initialize a new project with deployment enabled'"
echo
echo "The MCP server translates these requests into API calls!"