#!/bin/bash
# Run individual tests to debug failures

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

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

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
}

echo "========================================="
echo "Individual Test Runner"
echo "========================================="
echo ""

# Test 1: Idempotency
echo "--- Test 1: Idempotency: Create run with idempotency key ---"
IDEMPOTENCY_KEY=$(generate_uuid)
echo "Using 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"}}' 2>&1)

echo "Response length: ${#IDEMPOTENT_RESPONSE}"
echo "Response (first 200 chars): ${IDEMPOTENT_RESPONSE:0:200}"
echo ""

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 "Extracted FIRST_RUN_ID: '$FIRST_RUN_ID'"
echo "Is FIRST_RUN_ID set? $([ -n "$FIRST_RUN_ID" ] && echo "YES" || echo "NO")"
echo "Test result: $([ -n "$FIRST_RUN_ID" ] && echo -e "${GREEN}✓ PASSED${NC}" || echo -e "${RED}✗ FAILED${NC}")"
echo ""

# Test 2: Queue
echo "--- Test 2: Queue: Run status updated ---"
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"}}' 2>&1)

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'"
echo ""

if [ -n "$QUEUE_RUN_ID" ]; then
    sleep 2
    STATUS_RESPONSE=$(curl -s --max-time 5 "$GATEWAY_URL/api/runs/$QUEUE_RUN_ID" \
        -H "x-api-key: $API_KEY" 2>&1)
    
    echo "STATUS_RESPONSE length: ${#STATUS_RESPONSE}"
    echo "STATUS_RESPONSE (first 200 chars): ${STATUS_RESPONSE:0:200}"
    echo ""
    
    echo "Test result: $([ -n "$QUEUE_RUN_ID" ] || ([ -n "$STATUS_RESPONSE" ] && echo "YES" || echo "NO") && echo -e "${GREEN}✓ PASSED${NC}" || echo -e "${RED}✗ FAILED${NC}")"
else
    echo "Test result: ${RED}✗ FAILED${NC} (no QUEUE_RUN_ID)"
fi
echo ""

# Test 3: E2E
echo "--- Test 3: E2E: Create run ---"
WORKFLOW_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":"E2E Workflow Test","input":{"test":"workflow"}}' 2>&1)

echo "WORKFLOW_RESPONSE length: ${#WORKFLOW_RESPONSE}"
echo "WORKFLOW_RESPONSE (first 200 chars): ${WORKFLOW_RESPONSE:0:200}"
echo ""

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

echo "Extracted WORKFLOW_RUN_ID: '$WORKFLOW_RUN_ID'"
echo "Test result: $(([ -n "$WORKFLOW_RUN_ID" ] || [ -n "$WORKFLOW_RESPONSE" ]) && echo -e "${GREEN}✓ PASSED${NC}" || echo -e "${RED}✗ FAILED${NC}")"
echo ""

# Test 4: Concurrency
echo "--- Test 4: Concurrency: Multiple runs can be created concurrently ---"
CONCURRENT_SUCCESS=0
for i in {1..5}; do
    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\":\"Concurrent Test $i\",\"input\":{\"index\":$i}}" 2>&1)
    
    if [ -n "$RESPONSE" ] && [ ${#RESPONSE} -gt 50 ]; then
        CONCURRENT_SUCCESS=$((CONCURRENT_SUCCESS + 1))
        echo "  Request $i: ${GREEN}✓${NC} (length: ${#RESPONSE})"
    else
        echo "  Request $i: ${RED}✗${NC} (length: ${#RESPONSE})"
    fi
done

echo ""
echo "Successful: $CONCURRENT_SUCCESS/5"
echo "Test result: $([ "$CONCURRENT_SUCCESS" -ge 3 ] && echo -e "${GREEN}✓ PASSED${NC}" || echo -e "${RED}✗ FAILED${NC}")"
echo ""

echo "========================================="
echo "Summary"
echo "========================================="

