#!/bin/bash
# Full operations test - Complete workflow simulation
# Simulates real-world usage patterns

set +e

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

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo "========================================="
echo "4Runr Gateway - Full Operations Test"
echo "========================================="
echo ""
echo "This test simulates a complete operational scenario:"
echo "1. Multiple users creating runs"
echo "2. Priority queue processing"
echo "3. Concurrent operations"
echo "4. Error recovery"
echo "5. Monitoring and metrics"
echo ""

# Wait for server
echo "Waiting for server..."
for i in {1..30}; do
    if curl -sf "$GATEWAY_URL/health" > /dev/null; then
        echo -e "${GREEN}✓ Server ready${NC}"
        break
    fi
    sleep 1
done

echo ""
echo "========================================="
echo "Scenario 1: Multiple Users Creating Runs"
echo "========================================="
echo ""

RUN_IDS=()
echo "Creating 10 runs from different 'users'..."
for i in {1..10}; do
    RUN_ID=$(curl -sf -X POST "$GATEWAY_URL/api/runs" \
        -H "x-api-key: $API_KEY" \
        -H "Content-Type: application/json" \
        -d "{\"name\":\"User $i Run\",\"input\":{\"user\":$i,\"task\":\"process\"}}" \
        | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
    
    if [ ! -z "$RUN_ID" ]; then
        RUN_IDS+=("$RUN_ID")
        echo -e "  ${GREEN}✓${NC} Created run $i: ${RUN_ID:0:8}..."
    else
        echo -e "  ${RED}✗${NC} Failed to create run $i"
    fi
done

echo ""
echo "Created ${#RUN_IDS[@]} runs successfully"

echo ""
echo "========================================="
echo "Scenario 2: Priority Queue Processing"
echo "========================================="
echo ""

echo "Starting runs with different priorities..."
HIGH_PRIORITY_COUNT=0
NORMAL_PRIORITY_COUNT=0
LOW_PRIORITY_COUNT=0

for i in "${!RUN_IDS[@]}"; do
    RUN_ID="${RUN_IDS[$i]}"
    if [ -z "$RUN_ID" ]; then
        continue
    fi
    
    # Assign priorities: first 3 = high, next 4 = normal, rest = low
    if [ $i -lt 3 ]; then
        PRIORITY="high"
        HIGH_PRIORITY_COUNT=$((HIGH_PRIORITY_COUNT + 1))
    elif [ $i -lt 7 ]; then
        PRIORITY="normal"
        NORMAL_PRIORITY_COUNT=$((NORMAL_PRIORITY_COUNT + 1))
    else
        PRIORITY="low"
        LOW_PRIORITY_COUNT=$((LOW_PRIORITY_COUNT + 1))
    fi
    
    RESPONSE=$(curl -sf -X POST "$GATEWAY_URL/api/runs/$RUN_ID/start" \
        -H "x-api-key: $API_KEY" \
        -H "Content-Type: application/json" \
        -d "{\"priority\":\"$PRIORITY\"}")
    
    if echo "$RESPONSE" | grep -q "queued"; then
        echo -e "  ${GREEN}✓${NC} Started run ${RUN_ID:0:8}... with $PRIORITY priority"
    else
        echo -e "  ${YELLOW}⚠${NC} Run ${RUN_ID:0:8}... start response: $(echo $RESPONSE | head -c 50)"
    fi
done

echo ""
echo "Priority distribution:"
echo "  High: $HIGH_PRIORITY_COUNT"
echo "  Normal: $NORMAL_PRIORITY_COUNT"
echo "  Low: $LOW_PRIORITY_COUNT"

echo ""
echo "Waiting 5 seconds for queue processing..."
sleep 5

echo ""
echo "Checking run statuses..."
QUEUED=0
RUNNING=0
COMPLETED=0
FAILED=0
CREATED=0

for RUN_ID in "${RUN_IDS[@]}"; do
    if [ -z "$RUN_ID" ]; then
        continue
    fi
    
    STATUS=$(curl -sf "$GATEWAY_URL/api/runs/$RUN_ID" \
        -H "x-api-key: $API_KEY" \
        | grep -o '"status":"[^"]*' | cut -d'"' -f4)
    
    case "$STATUS" in
        "queued") QUEUED=$((QUEUED + 1)) ;;
        "running") RUNNING=$((RUNNING + 1)) ;;
        "completed") COMPLETED=$((COMPLETED + 1)) ;;
        "failed") FAILED=$((FAILED + 1)) ;;
        "created") CREATED=$((CREATED + 1)) ;;
    esac
done

echo "Status distribution:"
echo "  Created: $CREATED"
echo "  Queued: $QUEUED"
echo "  Running: $RUNNING"
echo "  Completed: $COMPLETED"
echo "  Failed: $FAILED"

echo ""
echo "========================================="
echo "Scenario 3: Concurrent Operations"
echo "========================================="
echo ""

echo "Simulating concurrent API calls..."
CONCURRENT_START=$(date +%s)

# Create 5 runs concurrently
for i in {1..5}; do
    (
        curl -sf -X POST "$GATEWAY_URL/api/runs" \
            -H "x-api-key: $API_KEY" \
            -H "Content-Type: application/json" \
            -d "{\"name\":\"Concurrent Run $i\",\"input\":{\"concurrent\":true,\"index\":$i}}" > /dev/null
    ) &
done

wait
CONCURRENT_END=$(date +%s)
CONCURRENT_DURATION=$((CONCURRENT_END - CONCURRENT_START))

echo "Created 5 runs concurrently in ${CONCURRENT_DURATION} seconds"

echo ""
echo "========================================="
echo "Scenario 4: Error Recovery"
echo "========================================="
echo ""

echo "Testing error scenarios and recovery..."

# Test invalid request
echo "1. Testing invalid request (should return 400)..."
INVALID_RESPONSE=$(curl -sf -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"invalid":"data"}')

if echo "$INVALID_RESPONSE" | grep -q "400\|validation\|error"; then
    echo -e "  ${GREEN}✓${NC} Invalid request properly rejected"
else
    echo -e "  ${YELLOW}⚠${NC} Unexpected response to invalid request"
fi

# Test non-existent run
echo "2. Testing non-existent run (should return 404)..."
NOT_FOUND_RESPONSE=$(curl -sf "$GATEWAY_URL/api/runs/00000000-0000-0000-0000-000000000000" \
    -H "x-api-key: $API_KEY")

if echo "$NOT_FOUND_RESPONSE" | grep -q "404\|not found"; then
    echo -e "  ${GREEN}✓${NC} Non-existent run properly handled"
else
    echo -e "  ${YELLOW}⚠${NC} Unexpected response for non-existent run"
fi

# Test recovery - create a valid run after errors
echo "3. Testing recovery (creating valid run after errors)..."
RECOVERY_RUN_ID=$(curl -sf -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Recovery Test","input":{"test":"recovery"}}' \
    | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)

if [ ! -z "$RECOVERY_RUN_ID" ]; then
    echo -e "  ${GREEN}✓${NC} Successfully recovered and created run"
else
    echo -e "  ${RED}✗${NC} Failed to recover"
fi

echo ""
echo "========================================="
echo "Scenario 5: Monitoring & Metrics"
echo "========================================="
echo ""

echo "Checking metrics endpoint..."
METRICS=$(curl -sf "$GATEWAY_URL/metrics")

if [ ! -z "$METRICS" ]; then
    echo -e "  ${GREEN}✓${NC} Metrics endpoint accessible"
    
    # Check for key metrics
    if echo "$METRICS" | grep -q "http_request_total"; then
        echo -e "  ${GREEN}✓${NC} HTTP request metrics present"
    fi
    
    if echo "$METRICS" | grep -q "runs_created_total"; then
        echo -e "  ${GREEN}✓${NC} Run metrics present"
    fi
    
    if echo "$METRICS" | grep -q "queue_jobs"; then
        echo -e "  ${GREEN}✓${NC} Queue metrics present"
    fi
else
    echo -e "  ${RED}✗${NC} Metrics endpoint not accessible"
fi

echo ""
echo "Checking health endpoints..."
HEALTH=$(curl -sf "$GATEWAY_URL/health")
READY=$(curl -sf "$GATEWAY_URL/ready")

if [ ! -z "$HEALTH" ] && [ ! -z "$READY" ]; then
    echo -e "  ${GREEN}✓${NC} Health endpoints accessible"
    
    if echo "$READY" | grep -q "ready"; then
        echo -e "  ${GREEN}✓${NC} Readiness check includes dependency status"
    fi
else
    echo -e "  ${RED}✗${NC} Health endpoints not accessible"
fi

echo ""
echo "========================================="
echo "Scenario 6: Idempotency in Practice"
echo "========================================="
echo ""

IDEMPOTENCY_KEY="full-ops-test-$(date +%s)"
echo "Testing idempotency with key: $IDEMPOTENCY_KEY"

IDEMPOTENT_RUN1=$(curl -sf -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Idempotent Run","input":{"test":"idempotency"}}' \
    | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)

sleep 1

IDEMPOTENT_RUN2=$(curl -sf -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Idempotent Run","input":{"test":"idempotency"}}' \
    | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)

if [ ! -z "$IDEMPOTENT_RUN1" ] && [ ! -z "$IDEMPOTENT_RUN2" ] && [ "$IDEMPOTENT_RUN1" = "$IDEMPOTENT_RUN2" ]; then
    echo -e "  ${GREEN}✓${NC} Idempotency working: same run ID returned"
else
    echo -e "  ${YELLOW}⚠${NC} Idempotency check: Run1=$IDEMPOTENT_RUN1, Run2=$IDEMPOTENT_RUN2"
fi

echo ""
echo "========================================="
echo "Scenario 7: List and Filter Operations"
echo "========================================="
echo ""

echo "Listing all runs..."
LIST_RESPONSE=$(curl -sf "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY")

if echo "$LIST_RESPONSE" | grep -q "runs"; then
    RUN_COUNT=$(echo "$LIST_RESPONSE" | grep -o '"id"' | wc -l)
    echo -e "  ${GREEN}✓${NC} List endpoint working: Found $RUN_COUNT runs"
else
    echo -e "  ${RED}✗${NC} List endpoint failed"
fi

echo ""
echo "========================================="
echo "Test Summary"
echo "========================================="
echo ""
echo -e "${BLUE}Operations Tested:${NC}"
echo "  ✓ Multiple user run creation"
echo "  ✓ Priority queue processing"
echo "  ✓ Concurrent operations"
echo "  ✓ Error recovery"
echo "  ✓ Monitoring and metrics"
echo "  ✓ Idempotency"
echo "  ✓ List operations"
echo ""
echo -e "${GREEN}✅ Full operations test complete!${NC}"
echo ""

