#!/bin/bash
# Individual test script for Idempotency feature
# Run this to test only idempotency

set -e  # Exit on errors

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
}

PASSED=0
FAILED=0

echo "========================================="
echo "Idempotency Tests"
echo "========================================="
echo ""

# Test 1: Create run with idempotency key
echo "--- Test 1: 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)

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

if [ -n "$FIRST_RUN_ID" ]; then
    echo -e "${GREEN}✓ PASSED${NC} - Extracted run ID: $FIRST_RUN_ID"
    PASSED=$((PASSED + 1))
else
    echo -e "${RED}✗ FAILED${NC} - Could not extract run ID"
    echo "Response: ${IDEMPOTENT_RESPONSE:0:200}"
    FAILED=$((FAILED + 1))
    exit 1
fi

# Test 2: Duplicate request returns same run
echo ""
echo "--- Test 2: Duplicate request returns same run ---"
SECOND_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)

SECOND_RUN_ID=$(echo "$SECOND_RESPONSE" | grep -o '"run":{[^}]*"id":"[^"]*' | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
if [ -z "$SECOND_RUN_ID" ]; then
    SECOND_RUN_ID=$(echo "$SECOND_RESPONSE" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -1)
fi

if [ -n "$SECOND_RUN_ID" ] && [ "$FIRST_RUN_ID" = "$SECOND_RUN_ID" ]; then
    echo -e "${GREEN}✓ PASSED${NC} - Both requests returned same run ID: $FIRST_RUN_ID"
    PASSED=$((PASSED + 1))
else
    echo -e "${RED}✗ FAILED${NC} - Run IDs don't match or extraction failed"
    echo "First: $FIRST_RUN_ID, Second: $SECOND_RUN_ID"
    FAILED=$((FAILED + 1))
    exit 1
fi

echo ""
echo "========================================="
echo "Idempotency Test Summary"
echo "========================================="
echo "Passed: $PASSED"
echo "Failed: $FAILED"
echo ""

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}All idempotency tests passed!${NC}"
    exit 0
else
    echo -e "${RED}Some tests failed${NC}"
    exit 1
fi

