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

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)
RESPONSE=$(curl -s --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"}}')
echo "Response: $RESPONSE"
echo "Has 'run': $(echo "$RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "Has 'success': $(echo "$RESPONSE" | grep -q '"success"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 2. Metrics Endpoint ==="
METRICS=$(curl -s "$GATEWAY_URL/metrics" | head -20)
echo "First 20 lines:"
echo "$METRICS"
echo "Has 'http_request': $(echo "$METRICS" | grep -q 'http_request' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 3. Queue Status Response ==="
QUEUE_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":"Queue Status Test","input":{"test":"queue"}}')
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 -s -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 "$GATEWAY_URL/api/runs/$QUEUE_RUN_ID" \
        -H "x-api-key: $API_KEY")
    echo "Status response: $STATUS_RESPONSE"
    echo "Has 'status': $(echo "$STATUS_RESPONSE" | grep -q '"status"' && echo 'YES' || echo 'NO')"
fi
echo ""

echo "=== 4. E2E Create Run Response ==="
E2E_RESPONSE=$(curl -s -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"}}')
echo "Response: $E2E_RESPONSE"
echo "Has 'run': $(echo "$E2E_RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "Has 'id': $(echo "$E2E_RESPONSE" | grep -q '"id"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 5. Auth Required (no API key) ==="
AUTH_RESPONSE=$(curl -s "$GATEWAY_URL/api/runs")
echo "Response: $AUTH_RESPONSE"
echo "Status code: $(curl -s -o /dev/null -w '%{http_code}' "$GATEWAY_URL/api/runs")"
echo "Has '401': $(echo "$AUTH_RESPONSE" | grep -q '401' && echo 'YES' || echo 'NO')"
echo "Has '403': $(echo "$AUTH_RESPONSE" | grep -q '403' && echo 'YES' || echo 'NO')"
echo "Has 'Unauthorized': $(echo "$AUTH_RESPONSE" | grep -qi 'unauthorized' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 6. 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}}")
    echo "Concurrent $i response length: ${#RESPONSE}"
    echo "Concurrent $i has 'run': $(echo "$RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
    if [ ! -z "$RESPONSE" ] && (echo "$RESPONSE" | grep -q '"run"\|"success"'); then
        CONCURRENT_SUCCESS=$((CONCURRENT_SUCCESS + 1))
    fi
done
echo "Concurrent successes: $CONCURRENT_SUCCESS"
echo ""

