#!/bin/bash
# Debug script to check actual API responses

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

echo "=== 1. Test 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" | jq . 2>/dev/null || echo "$RESPONSE"
echo "Has 'run' field: $(echo "$RESPONSE" | grep -q '"run"' && echo 'YES' || echo 'NO')"
echo "Has 'id' field at top level: $(echo "$RESPONSE" | grep -q '^[[:space:]]*"id"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 2. Test 404 Error Response ==="
NOT_FOUND=$(curl -s "$GATEWAY_URL/api/runs/00000000-0000-0000-0000-000000000000" \
    -H "x-api-key: $API_KEY")
echo "$NOT_FOUND" | jq . 2>/dev/null || echo "$NOT_FOUND"
echo "Has 'error' field: $(echo "$NOT_FOUND" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'correlationId' field: $(echo "$NOT_FOUND" | grep -q '"correlationId"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 3. Test 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 "$VALIDATION" | jq . 2>/dev/null || echo "$VALIDATION"
echo "Has 'error' field: $(echo "$VALIDATION" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'details' field: $(echo "$VALIDATION" | grep -q '"details"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 4. Test Invalid UUID Response ==="
INVALID_UUID=$(curl -s "$GATEWAY_URL/api/runs/invalid-id" \
    -H "x-api-key: $API_KEY")
echo "$INVALID_UUID" | jq . 2>/dev/null || echo "$INVALID_UUID"
echo "Has 'error' field: $(echo "$INVALID_UUID" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'timestamp' field: $(echo "$INVALID_UUID" | grep -q '"timestamp"' && echo 'YES' || echo 'NO')"
echo ""

echo "=== 5. Test 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 "$MISSING" | jq . 2>/dev/null || echo "$MISSING"
echo "Has 'error' field: $(echo "$MISSING" | grep -q '"error"' && echo 'YES' || echo 'NO')"
echo "Has 'details' field: $(echo "$MISSING" | grep -q '"details"' && echo 'YES' || echo 'NO')"
echo ""

