#!/bin/bash
# Diagnostic script to see what the API actually returns

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

echo "========================================="
echo "API Response Diagnostic"
echo "========================================="
echo ""

# Test 1: Idempotency
echo "--- Test 1: Idempotency ---"
IDEMPOTENCY_KEY=$(uuidgen 2>/dev/null || echo "test-key-$(date +%s)")
echo "Using idempotency key: $IDEMPOTENCY_KEY"
echo ""

echo "Making first request..."
FIRST_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}\n" --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":"Debug Test","input":{"test":"debug"}}' 2>&1)

HTTP_CODE=$(echo "$FIRST_RESPONSE" | grep "HTTP_CODE" | cut -d: -f2)
RESPONSE_BODY=$(echo "$FIRST_RESPONSE" | sed '/HTTP_CODE/d')

echo "HTTP Status Code: $HTTP_CODE"
echo "Response Length: ${#RESPONSE_BODY}"
echo "Response Body:"
echo "$RESPONSE_BODY" | jq . 2>/dev/null || echo "$RESPONSE_BODY"
echo ""

# Extract ID
FIRST_RUN_ID=$(echo "$RESPONSE_BODY" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
if [ -z "$FIRST_RUN_ID" ]; then
    FIRST_RUN_ID=$(echo "$RESPONSE_BODY" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
fi
echo "Extracted FIRST_RUN_ID: '$FIRST_RUN_ID'"
echo "Is FIRST_RUN_ID set? $([ -n "$FIRST_RUN_ID" ] && echo "YES" || echo "NO")"
echo ""

# Test 2: Queue
echo "--- Test 2: Queue ---"
echo "Creating queue run..."
QUEUE_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}\n" --max-time 5 -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Queue Debug Test","input":{"test":"queue"}}' 2>&1)

QUEUE_HTTP_CODE=$(echo "$QUEUE_RESPONSE" | grep "HTTP_CODE" | cut -d: -f2)
QUEUE_BODY=$(echo "$QUEUE_RESPONSE" | sed '/HTTP_CODE/d')

echo "HTTP Status Code: $QUEUE_HTTP_CODE"
echo "Response Length: ${#QUEUE_BODY}"
echo "Response Body:"
echo "$QUEUE_BODY" | jq . 2>/dev/null || echo "$QUEUE_BODY"
echo ""

QUEUE_RUN_ID=$(echo "$QUEUE_BODY" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
if [ -z "$QUEUE_RUN_ID" ]; then
    QUEUE_RUN_ID=$(echo "$QUEUE_BODY" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
fi
echo "Extracted QUEUE_RUN_ID: '$QUEUE_RUN_ID'"
echo ""

if [ -n "$QUEUE_RUN_ID" ]; then
    echo "Getting status for $QUEUE_RUN_ID..."
    sleep 2
    STATUS_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}\n" --max-time 5 "$GATEWAY_URL/api/runs/$QUEUE_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" | sed '/HTTP_CODE/d')
    
    echo "Status HTTP Code: $STATUS_HTTP_CODE"
    echo "Status Response Length: ${#STATUS_BODY}"
    echo "Status Response Body:"
    echo "$STATUS_BODY" | jq . 2>/dev/null || echo "$STATUS_BODY"
    echo ""
    
    STATUS_RUN_ID=$(echo "$STATUS_BODY" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
    if [ -z "$STATUS_RUN_ID" ]; then
        STATUS_RUN_ID=$(echo "$STATUS_BODY" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
    fi
    echo "Extracted STATUS_RUN_ID: '$STATUS_RUN_ID'"
fi

echo ""
echo "--- Test 3: E2E ---"
echo "Creating E2E run..."
E2E_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}\n" --max-time 5 -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"E2E Debug Test","input":{"test":"e2e"}}' 2>&1)

E2E_HTTP_CODE=$(echo "$E2E_RESPONSE" | grep "HTTP_CODE" | cut -d: -f2)
E2E_BODY=$(echo "$E2E_RESPONSE" | sed '/HTTP_CODE/d')

echo "HTTP Status Code: $E2E_HTTP_CODE"
echo "Response Length: ${#E2E_BODY}"
echo "Response Body:"
echo "$E2E_BODY" | jq . 2>/dev/null || echo "$E2E_BODY"
echo ""

E2E_RUN_ID=$(echo "$E2E_BODY" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
if [ -z "$E2E_RUN_ID" ]; then
    E2E_RUN_ID=$(echo "$E2E_BODY" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
fi
echo "Extracted E2E_RUN_ID: '$E2E_RUN_ID'"

echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo "Idempotency: FIRST_RUN_ID='$FIRST_RUN_ID' (set: $([ -n "$FIRST_RUN_ID" ] && echo "YES" || echo "NO"))"
echo "Queue: QUEUE_RUN_ID='$QUEUE_RUN_ID', STATUS_RUN_ID='$STATUS_RUN_ID'"
echo "E2E: E2E_RUN_ID='$E2E_RUN_ID'"

