#!/bin/bash

set -x  # Enable debug mode to see every command

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

echo "Starting minimal test..."

# Generate a valid UUID v4 (same as test script)
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
}

IDEMPOTENCY_KEY=$(generate_uuid)

echo "Making curl request..."
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":"Test","input":{"test":"test"}}')

echo "Response received"
echo "Length: ${#RESPONSE}"
echo "First 200 chars: ${RESPONSE:0:200}"

# Test the condition
echo ""
echo "=== Testing conditions ==="
echo "Response is not empty: $([ ! -z "$RESPONSE" ] && echo "YES" || echo "NO")"
echo "Response length > 50: $([ ${#RESPONSE} -gt 50 ] && echo "YES" || echo "NO")"

if [ ! -z "$RESPONSE" ] && [ ${#RESPONSE} -gt 50 ]; then
    echo "Response is not empty and > 50 chars"
    
    # Test each pattern individually
    HAS_RUN=$(echo "$RESPONSE" | grep -q "\"run\"" && echo "YES" || echo "NO")
    HAS_SUCCESS=$(echo "$RESPONSE" | grep -q "\"success\"" && echo "YES" || echo "NO")
    HAS_ID=$(echo "$RESPONSE" | grep -q "\"id\"" && echo "YES" || echo "NO")
    
    echo "Has 'run': $HAS_RUN"
    echo "Has 'success': $HAS_SUCCESS"
    echo "Has 'id': $HAS_ID"
    
    # Test the combined condition
    if echo "$RESPONSE" | grep -q "\"run\"" || echo "$RESPONSE" | grep -q "\"success\"" || echo "$RESPONSE" | grep -q "\"id\""; then
        echo "✓ Combined condition TRUE - TEST SHOULD PASS"
    else
        echo "✗ Combined condition FALSE - TEST WILL FAIL"
    fi
else
    echo "Response is empty or <= 50 chars"
fi

