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

set -e

PASSED=0
FAILED=0

echo "=== mlgym_projects_get Comprehensive Test Suite ==="
echo ""

# Test 1: Get project without authentication
echo "Test 1: Get project without authentication"
rm -f ~/.mlgym/mcp_config.json
REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_projects_get","arguments":{"project_id":1}},"id":1}'
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')

if [ "$STATUS" = "error" ] && 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 and project
echo ""
echo "[SETUP] Creating test user and project"
TEST_EMAIL="projectget-$(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 Get 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

# Create test project
PROJECT_NAME="get-test-$(date +%s | tail -c 6)"
PROJ_REQ="{\"jsonrpc\":\"2.0\",\"method\":\"tools/call\",\"params\":{\"name\":\"mlgym_project_init\",\"arguments\":{\"name\":\"$PROJECT_NAME\",\"description\":\"Test project for get testing\",\"enable_deployment\":false}},\"id\":3}"
PROJ_RESP=$(echo "$PROJ_REQ" | node index.js 2>/dev/null | tail -1)
PROJ_CONTENT=$(echo "$PROJ_RESP" | jq -r '.result.content[0].text')
PROJECT_ID=$(echo "$PROJ_CONTENT" | jq -r '.project_id' 2>/dev/null)

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

# Test 2: Missing project_id parameter
echo ""
echo "Test 2: Missing project_id parameter"
REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_projects_get","arguments":{}},"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')

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

# Test 3: Get valid project
echo "Test 3: Get valid project"
GET_REQ="{\"jsonrpc\":\"2.0\",\"method\":\"tools/call\",\"params\":{\"name\":\"mlgym_projects_get\",\"arguments\":{\"project_id\":$PROJECT_ID}},\"id\":5}"
GET_RESP=$(echo "$GET_REQ" | node index.js 2>/dev/null | tail -1)
GET_CONTENT=$(echo "$GET_RESP" | jq -r '.result.content[0].text')
GET_STATUS=$(echo "$GET_CONTENT" | jq -r '.status')

if [ "$GET_STATUS" = "success" ]; then
    echo "✅ Test 3: Get project successful"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 3: Get project should return success"
    FAILED=$((FAILED + 1))
fi

# Test 4: Response format validation
echo "Test 4: Response format validation"
HAS_STATUS=$(echo "$GET_CONTENT" | jq -e '.status' > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_PROJECT=$(echo "$GET_CONTENT" | jq -e '.project' > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_ID=$(echo "$GET_CONTENT" | jq -e '.project.id' > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_NAME=$(echo "$GET_CONTENT" | jq -e '.project.name' > /dev/null 2>&1 && echo "yes" || echo "no")

if [ "$HAS_STATUS" = "yes" ] && [ "$HAS_PROJECT" = "yes" ] && [ "$HAS_ID" = "yes" ] && [ "$HAS_NAME" = "yes" ]; then
    echo "✅ Test 4: Response format valid"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 4: Response format invalid (status:$HAS_STATUS, project:$HAS_PROJECT, id:$HAS_ID, name:$HAS_NAME)"
    FAILED=$((FAILED + 1))
fi

# Test 5: Project data accuracy
echo "Test 5: Project data accuracy"
RETURNED_ID=$(echo "$GET_CONTENT" | jq -r '.project.id')
RETURNED_NAME=$(echo "$GET_CONTENT" | jq -r '.project.name')

if [ "$RETURNED_ID" = "$PROJECT_ID" ] && [ "$RETURNED_NAME" = "$PROJECT_NAME" ]; then
    echo "✅ Test 5: Project data accurate (ID: $RETURNED_ID, Name: $RETURNED_NAME)"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 5: Project data mismatch"
    FAILED=$((FAILED + 1))
fi

# Test 6: Project details completeness
echo "Test 6: Project details completeness"
PROJ_DATA=$(echo "$GET_CONTENT" | jq -r '.project')
HAS_SSH=$(echo "$PROJ_DATA" | jq -e '.ssh_url' > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_WEB=$(echo "$PROJ_DATA" | jq -e '.web_url' > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_VISIBILITY=$(echo "$PROJ_DATA" | jq -e '.visibility' > /dev/null 2>&1 && echo "yes" || echo "no")
HAS_CREATED=$(echo "$PROJ_DATA" | jq -e '.created_at' > /dev/null 2>&1 && echo "yes" || echo "no")

if [ "$HAS_SSH" = "yes" ] && [ "$HAS_WEB" = "yes" ] && [ "$HAS_VISIBILITY" = "yes" ]; then
    echo "✅ Test 6: Project details complete (ssh:$HAS_SSH, web:$HAS_WEB, visibility:$HAS_VISIBILITY, created:$HAS_CREATED)"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 6: Project details incomplete"
    FAILED=$((FAILED + 1))
fi

# Test 7: Non-existent project
echo "Test 7: Get non-existent project"
INVALID_REQ='{"jsonrpc":"2.0","method":"tools/call","params":{"name":"mlgym_projects_get","arguments":{"project_id":999999}},"id":6}'
INVALID_RESP=$(echo "$INVALID_REQ" | node index.js 2>/dev/null | tail -1)
INVALID_CONTENT=$(echo "$INVALID_RESP" | jq -r '.result.content[0].text')
INVALID_STATUS=$(echo "$INVALID_CONTENT" | jq -r '.status')

if [ "$INVALID_STATUS" = "error" ]; then
    echo "✅ Test 7: Non-existent project rejected"
    PASSED=$((PASSED + 1))
else
    echo "❌ Test 7: Should reject non-existent project"
    FAILED=$((FAILED + 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
