#!/bin/bash
# Debug script to see actual API responses

GATEWAY_URL="${TEST_SERVER_URL:-http://localhost:3000}"
API_KEY="${TEST_API_KEY:-test-key-ce67dabb0d4e27e3d72877c921b89cae}"

generate_uuid() {
    if command -v uuidgen > /dev/null 2>&1; then
        uuidgen
    else
        cat /proc/sys/kernel/random/uuid 2>/dev/null || \
        echo "$(od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' | tr '[:upper:]' '[:lower:]')"
    fi
}

echo "=== 1. Idempotency Response ==="
IDEMPOTENCY_KEY=$(generate_uuid)
IDEMPOTENT_RESPONSE=$(curl -sf --max-time 5 -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Idempotency Test","input":{"test":"idempotency"}}' 2>&1)
echo "Exit code: $?"
echo "Response length: ${#IDEMPOTENT_RESPONSE}"
echo "Response: $IDEMPOTENT_RESPONSE"
echo "Has 'run': $(echo "$IDEMPOTENT_RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "Has 'success': $(echo "$IDEMPOTENT_RESPONSE" | grep -q '"success"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 2. Queue Status Response ==="
QUEUE_RESPONSE=$(curl -sf --max-time 5 -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Queue Status Test","input":{"test":"queue"}}' 2>&1)
QUEUE_RUN_ID=$(echo "$QUEUE_RESPONSE" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
echo "Created run ID: $QUEUE_RUN_ID"
if [ ! -z "$QUEUE_RUN_ID" ]; then
    curl -sf -X POST "$GATEWAY_URL/api/runs/$QUEUE_RUN_ID/start" \
        -H "x-api-key: $API_KEY" \
        -H "Content-Type: application/json" \
        -d '{"priority":"high"}' > /dev/null
    sleep 2
    STATUS_RESPONSE=$(curl -s --max-time 5 "$GATEWAY_URL/api/runs/$QUEUE_RUN_ID" \
        -H "x-api-key: $API_KEY" 2>&1)
    echo "Exit code: $?"
    echo "Response length: ${#STATUS_RESPONSE}"
    echo "Response: $STATUS_RESPONSE"
    echo "Has 'id': $(echo "$STATUS_RESPONSE" | grep -q '"id"' && echo 'YES' || echo 'NO')"
    echo "Has 'status': $(echo "$STATUS_RESPONSE" | grep -q '"status"' && echo 'YES' || echo 'NO')"
fi
echo ""

echo "=== 3. E2E Create Run Response ==="
E2E_RESPONSE=$(curl -sf -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"E2E Workflow Test","input":{"test":"workflow"}}' 2>&1)
echo "Exit code: $?"
echo "Response length: ${#E2E_RESPONSE}"
echo "Response: $E2E_RESPONSE"
echo "Has 'run': $(echo "$E2E_RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "Has 'success': $(echo "$E2E_RESPONSE" | grep -q '"success"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 4. Concurrent Operations ==="
CONCURRENT_SUCCESS=0
for i in {1..3}; do
    RESPONSE=$(curl -s --max-time 5 -X POST "$GATEWAY_URL/api/runs" \
        -H "x-api-key: $API_KEY" \
        -H "Content-Type: application/json" \
        -d "{\"name\":\"Concurrent Test $i\",\"input\":{\"index\":$i}}" 2>&1)
    echo "Concurrent $i exit code: $?"
    echo "Concurrent $i response length: ${#RESPONSE}"
    echo "Concurrent $i response: $RESPONSE"
    echo "Concurrent $i has 'run': $(echo "$RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
    echo "Concurrent $i has 'success': $(echo "$RESPONSE" | grep -q '"success"' && echo 'YES' || echo 'NO')"
    if [ ! -z "$RESPONSE" ] && (echo "$RESPONSE" | grep -q '"run"' || echo "$RESPONSE" | grep -q '"success"'); then
        CONCURRENT_SUCCESS=$((CONCURRENT_SUCCESS + 1))
    fi
done
echo "Concurrent successes: $CONCURRENT_SUCCESS"
echo ""

