#!/bin/bash
# Quick debug to see actual API responses

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

echo "=== 1. Idempotency Response ==="
IDEMPOTENCY_KEY="debug-$(date +%s)"
RESPONSE=$(curl -s -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"}}')
echo "Response length: ${#RESPONSE}"
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. 404 Error Response ==="
NOT_FOUND=$(curl -s "$GATEWAY_URL/api/runs/00000000-0000-0000-0000-000000000000" \
    -H "x-api-key: $API_KEY")
echo "Response length: ${#NOT_FOUND}"
echo "Response: $NOT_FOUND"
echo "Has 'error': $(echo "$NOT_FOUND" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'correlationId': $(echo "$NOT_FOUND" | grep -q '"correlationId"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 3. Validation Error Response ==="
VALIDATION=$(curl -s -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"input":{}}')
echo "Response length: ${#VALIDATION}"
echo "Response: $VALIDATION"
echo "Has 'error': $(echo "$VALIDATION" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'details': $(echo "$VALIDATION" | grep -q '"details"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 4. Invalid UUID Response ==="
INVALID_UUID=$(curl -s "$GATEWAY_URL/api/runs/invalid-id" \
    -H "x-api-key: $API_KEY")
echo "Response length: ${#INVALID_UUID}"
echo "Response: $INVALID_UUID"
echo "Has 'error': $(echo "$INVALID_UUID" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'timestamp': $(echo "$INVALID_UUID" | grep -q '"timestamp"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 5. Missing Field Response ==="
MISSING=$(curl -s -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"input":{}}')
echo "Response length: ${#MISSING}"
echo "Response: $MISSING"
echo "Has 'error': $(echo "$MISSING" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'details': $(echo "$MISSING" | grep -q '"details"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 6. Concurrent Test Response ==="
CONCURRENT=$(curl -s -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Concurrent Test","input":{"index":1}}')
echo "Response length: ${#CONCURRENT}"
echo "Response: $CONCURRENT"
echo "Has 'run': $(echo "$CONCURRENT" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "Has 'success': $(echo "$CONCURRENT" | grep -q '"success"' && echo 'YES' || echo 'NO')"
echo ""

