#!/bin/bash
# Debug script to inspect 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 "========================================="
echo "Debug: API Response Inspection"
echo "========================================="
echo ""

echo "--- Test 1: Create run with idempotency key ---"
IDEMPOTENCY_KEY=$(generate_uuid)
echo "Idempotency Key: $IDEMPOTENCY_KEY"
RESPONSE=$(curl -s --max-time 5 -w "\nHTTP_CODE:%{http_code}" -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Debug Test","input":{"test":"debug"}}' 2>&1)
HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | grep -v "HTTP_CODE:")

echo "HTTP Code: [$HTTP_CODE]"
echo "Response Length: ${#BODY}"
echo "Response Body:"
echo "$BODY"
echo ""
echo "Pattern checks:"
echo "  Has 'run': $(echo "$BODY" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "  Has 'success': $(echo "$BODY" | grep -q '"success"' && echo 'YES' || echo 'NO')"
echo "  Has 'id': $(echo "$BODY" | grep -q '"id"' && echo 'YES' || echo 'NO')"
echo ""

echo "--- Test 2: Concurrent run creation ---"
for i in {1..2}; do
    echo "Concurrent test $i:"
    RESPONSE=$(curl -s --max-time 5 -w "\nHTTP_CODE:%{http_code}" -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)
    HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
    BODY=$(echo "$RESPONSE" | grep -v "HTTP_CODE:")
    
    echo "  HTTP Code: [$HTTP_CODE]"
    echo "  Response Length: ${#BODY}"
    echo "  Response Body: $BODY"
    echo "  Has 'run': $(echo "$BODY" | grep -q '"run"' && echo 'YES' || echo 'NO')"
    echo "  Has 'id': $(echo "$BODY" | grep -q '"id"' && echo 'YES' || echo 'NO')"
    echo ""
done

echo "--- Test 3: Create and get run status ---"
CREATE_RESPONSE=$(curl -s --max-time 5 -w "\nHTTP_CODE:%{http_code}" -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Status Test","input":{"test":"status"}}' 2>&1)
CREATE_HTTP_CODE=$(echo "$CREATE_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
CREATE_BODY=$(echo "$CREATE_RESPONSE" | grep -v "HTTP_CODE:")

echo "Create HTTP Code: [$CREATE_HTTP_CODE]"
echo "Create Response: $CREATE_BODY"

RUN_ID=$(echo "$CREATE_BODY" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
echo "Extracted Run ID: [$RUN_ID]"
echo ""

if [ ! -z "$RUN_ID" ]; then
    echo "Getting run status..."
    STATUS_RESPONSE=$(curl -s --max-time 5 -w "\nHTTP_CODE:%{http_code}" "$GATEWAY_URL/api/runs/$RUN_ID" \
        -H "x-api-key: $API_KEY" 2>&1)
    STATUS_HTTP_CODE=$(echo "$STATUS_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
    STATUS_BODY=$(echo "$STATUS_RESPONSE" | grep -v "HTTP_CODE:")
    
    echo "Status HTTP Code: [$STATUS_HTTP_CODE]"
    echo "Status Response Length: ${#STATUS_BODY}"
    echo "Status Response: $STATUS_BODY"
    echo "Has 'id': $(echo "$STATUS_BODY" | grep -q '"id"' && echo 'YES' || echo 'NO')"
    echo "Has 'status': $(echo "$STATUS_BODY" | grep -q '"status"' && echo 'YES' || echo 'NO')"
fi

echo ""
echo "========================================="
echo "Debug Complete"
echo "========================================="

