#!/bin/bash

# Debug script to understand why tests are failing

GATEWAY_URL="http://localhost:3000"
API_KEY="test-key-ce67dabb0d4e27e3d72877c921b89cae"

echo "=== Testing Idempotency Response ==="
IDEMPOTENCY_KEY=$(uuidgen 2>/dev/null || cat /proc/sys/kernel/random/uuid 2>/dev/null)
echo "IDEMPOTENCY_KEY: $IDEMPOTENCY_KEY"

IDEMPOTENT_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 length: ${#IDEMPOTENT_RESPONSE}"
echo "Response (first 200 chars): ${IDEMPOTENT_RESPONSE:0:200}"
echo ""

# Test the extraction
FIRST_RUN_ID=$(echo "$IDEMPOTENT_RESPONSE" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
if [ -z "$FIRST_RUN_ID" ]; then
    FIRST_RUN_ID=$(echo "$IDEMPOTENT_RESPONSE" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
fi
echo "FIRST_RUN_ID extracted: [$FIRST_RUN_ID]"
echo "FIRST_RUN_ID length: ${#FIRST_RUN_ID}"
echo "FIRST_RUN_ID is empty check: $([ -z "$FIRST_RUN_ID" ] && echo "YES" || echo "NO")"
echo "FIRST_RUN_ID is not empty check: $([ ! -z "$FIRST_RUN_ID" ] && echo "YES" || echo "NO")"
echo "FIRST_RUN_ID -n check: $([ -n "$FIRST_RUN_ID" ] && echo "YES" || echo "NO")"
echo ""

# Test the grep checks
echo "Testing grep checks:"
echo "  grep -q '\"success\"': $(echo "$IDEMPOTENT_RESPONSE" | grep -q '"success"' && echo "FOUND" || echo "NOT FOUND")"
echo "  grep -q '\"run\"': $(echo "$IDEMPOTENT_RESPONSE" | grep -q '"run"' && echo "FOUND" || echo "NOT FOUND")"
echo "  grep -q '\"id\"': $(echo "$IDEMPOTENT_RESPONSE" | grep -q '"id"' && echo "FOUND" || echo "NOT FOUND")"
echo ""

# Test the variable assignment pattern
echo "Testing variable assignment pattern:"
echo "$IDEMPOTENT_RESPONSE" | grep -q '"success"' && TEST_VAR=1 || TEST_VAR=0
echo "  TEST_VAR after grep: $TEST_VAR"
echo "  TEST_VAR == 1 check: $([ "$TEST_VAR" = "1" ] && echo "YES" || echo "NO")"
echo ""

# Test the actual if statement
echo "Testing actual if statement:"
if echo "$IDEMPOTENT_RESPONSE" | grep -q '"success"'; then
    echo "  Direct if with grep: PASSED"
else
    echo "  Direct if with grep: FAILED"
fi

if [ ! -z "$FIRST_RUN_ID" ]; then
    echo "  If with FIRST_RUN_ID check: PASSED"
else
    echo "  If with FIRST_RUN_ID check: FAILED"
fi

echo ""
echo "=== Testing Queue Status Response ==="
# Create a run first
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 Test","input":{"test":"queue"}}')
QUEUE_RUN_ID=$(echo "$QUEUE_RESPONSE" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
if [ -z "$QUEUE_RUN_ID" ]; then
    QUEUE_RUN_ID=$(echo "$QUEUE_RESPONSE" | grep -o '"id":"[a-f0-9-]\{36\}' | cut -d'"' -f4 | head -1)
fi
echo "QUEUE_RUN_ID: [$QUEUE_RUN_ID]"

sleep 2
STATUS_RESPONSE=$(curl -s --max-time 5 "$GATEWAY_URL/api/runs/$QUEUE_RUN_ID" \
    -H "x-api-key: $API_KEY")
echo "Status response length: ${#STATUS_RESPONSE}"
echo "Status response (first 200 chars): ${STATUS_RESPONSE:0:200}"
echo "  grep -q '\"id\"': $(echo "$STATUS_RESPONSE" | grep -q '"id"' && echo "FOUND" || echo "NOT FOUND")"

