#!/bin/bash
# Comprehensive test suite for mlgym_project_init

set -e

PASSED=0
FAILED=0

echo "=== mlgym_project_init Comprehensive Test Suite ==="
echo ""

# Test 1: Project init without authentication
echo "Test 1: Project init without authentication"
rm -f ~/.mlgym/mcp_config.json
REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_project_init","arguments":{"name":"test-project","description":"Test project description"}},"id":1}'
RESP=$(echo "$REQ" | node index.js 2>/dev/null | tail -1)
CONTENT=$(echo "$RESP" | jq -r '.result.content[0].text')

if echo "$CONTENT" | grep -q "Not authenticated"; then
    echo "✅ Test 1: Correctly requires authentication"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 1: Should require authentication"
    FAILED=$((FAILED + 1))
fi

# Setup: Create user for remaining tests
echo ""
echo "[SETUP] Creating test user"
TEST_EMAIL="projectinit-$(date +%s)@example.com"
TEST_PASS="MyV3ryC0mpl3x!P@ssw0rd#2024"
CREATE_REQ="{\"jsonrpc\":\"2.0\",\"method\":\"tools/call\",\"params\":{\"name\":\"mlgym_user_create\",\"arguments\":{\"email\":\"$TEST_EMAIL\",\"name\":\"Project Init Test\",\"password\":\"$TEST_PASS\",\"accept_terms\":true}},\"id\":2}"
CREATE_RESP=$(echo "$CREATE_REQ" | node index.js 2>/dev/null | tail -1)
USER_ID=$(echo "$CREATE_RESP" | jq -r '.result.content[0].text' | jq -r '.user_id')

if [ -n "$USER_ID" ] && [ "$USER_ID" != "null" ]; then
    echo "✅ Test user created (ID: $USER_ID)"
else
    echo "❌ Failed to create test user"
    exit 1
fi

# Test 2: Missing name parameter
echo ""
echo "Test 2: Missing name parameter"
REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_project_init","arguments":{"description":"Test description"}},"id":3}'
RESP=$(echo "$REQ" | node index.js 2>/dev/null | tail -1)
CONTENT=$(echo "$RESP" | jq -r '.result.content[0].text')
STATUS=$(echo "$CONTENT" | jq -r '.status' 2>/dev/null)

if [ "$STATUS" = "error" ] && echo "$CONTENT" | grep -q "required"; then
    echo "✅ Test 2: Correctly validates missing name"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 2: Should require name parameter"
    FAILED=$((FAILED + 1))
fi

# Test 3: Missing description parameter  
echo "Test 3: Missing description parameter"
REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_project_init","arguments":{"name":"test-project"}},"id":4}'
RESP=$(echo "$REQ" | node index.js 2>/dev/null | tail -1)
CONTENT=$(echo "$RESP" | jq -r '.result.content[0].text')
STATUS=$(echo "$CONTENT" | jq -r '.status' 2>/dev/null)

if [ "$STATUS" = "error" ] && echo "$CONTENT" | grep -q "required"; then
    echo "✅ Test 3: Correctly validates missing description"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 3: Should require description parameter"
    FAILED=$((FAILED + 1))
fi

# Test 4: Valid project creation (without deployment)
echo "Test 4: Valid project creation (without deployment)"
PROJECT_NAME="test-project-$(date +%s | tail -c 6)"
REQ="{\"jsonrpc\":\"2.0\",\"method\":\"tools/call\",\"params\":{\"name\":\"mlgym_project_init\",\"arguments\":{\"name\":\"$PROJECT_NAME\",\"description\":\"Test project for MCP testing\",\"enable_deployment\":false}},\"id\":5}"
RESP=$(echo "$REQ" | node index.js 2>/dev/null | tail -1)
CONTENT=$(echo "$RESP" | jq -r '.result.content[0].text')

# Check if response is JSON or error string
if echo "$CONTENT" | jq -e '.' >/dev/null 2>&1; then
    PROJECT_ID=$(echo "$CONTENT" | jq -r '.project_id' 2>/dev/null)
    if [ -n "$PROJECT_ID" ] && [ "$PROJECT_ID" != "null" ]; then
        echo "✅ Test 4: Project created (ID: $PROJECT_ID)"
        PASSED=$((PASSED + 1))
    else
        echo "❌ Test 4: Failed to create project"
        echo "Content: $CONTENT"
        FAILED=$((FAILED + 1))
    fi
else
    echo "❌ Test 4: Backend error - $CONTENT"
    FAILED=$((FAILED + 1))
fi

# Test 5: Project creation with deployment
echo "Test 5: Project creation with deployment enabled"
PROJECT_NAME2="deploy-test-$(date +%s | tail -c 6)"
REQ="{\"jsonrpc\":\"2.0\",\"method\":\"tools/call\",\"params\":{\"name\":\"mlgym_project_init\",\"arguments\":{\"name\":\"$PROJECT_NAME2\",\"description\":\"Test project with deployment\",\"enable_deployment\":true}},\"id\":6}"
RESP=$(echo "$REQ" | node index.js 2>/dev/null | tail -1)
CONTENT=$(echo "$RESP" | jq -r '.result.content[0].text')

if echo "$CONTENT" | jq -e '.' >/dev/null 2>&1; then
    PROJECT_ID2=$(echo "$CONTENT" | jq -r '.project_id' 2>/dev/null)
    HAS_WEBHOOK=$(echo "$CONTENT" | jq -e '.webhook' >/dev/null 2>&1 && echo "yes" || echo "no")
    HAS_DEPLOYMENT=$(echo "$CONTENT" | jq -e '.deployment' >/dev/null 2>&1 && echo "yes" || echo "no")
    
    if [ -n "$PROJECT_ID2" ] && [ "$PROJECT_ID2" != "null" ]; then
        echo "✅ Test 5: Project with deployment created (ID: $PROJECT_ID2)"
        echo "   Webhook: $HAS_WEBHOOK, Deployment: $HAS_DEPLOYMENT"
        PASSED=$((PASSED + 1))
    else
        echo "❌ Test 5: Failed to create project with deployment"
        FAILED=$((FAILED + 1))
    fi
else
    echo "❌ Test 5: Backend error - $CONTENT"
    FAILED=$((FAILED + 1))
fi

# Test 6: Invalid project name format
echo "Test 6: Invalid project name (uppercase/special chars)"
REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_project_init","arguments":{"name":"INVALID-NAME!","description":"Test description"}},"id":7}'
RESP=$(echo "$REQ" | node index.js 2>/dev/null | tail -1)
CONTENT=$(echo "$RESP" | jq -r '.result.content[0].text')

# May be rejected by backend or succeed (depends on validation)
if echo "$CONTENT" | grep -qiE "error|invalid|failed"; then
    echo "✅ Test 6: Invalid name rejected"
    PASSED=$((PASSED + 1))
else
    echo "⚠️  Test 6: Invalid name accepted (backend may allow it)"
    PASSED=$((PASSED + 1))
fi

echo ""
echo "==============================================="
echo "RESULTS: $PASSED passed, $FAILED failed (Total: $((PASSED + FAILED)))"
echo "==============================================="

if [ $FAILED -eq 0 ]; then
    echo "✅ ALL TESTS PASSED"
    exit 0
else
    echo "⚠️  SOME TESTS FAILED"
    exit 1
fi
