#!/bin/bash

set +e

GATEWAY_URL="http://localhost:3000"
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
    fi
}

echo "========================================="
echo "Testing ONLY: Idempotency"
echo "========================================="
echo ""

# Make the API call
IDEMPOTENCY_KEY=$(generate_uuid)
echo "Making API call with 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>/dev/null)

echo "Response received:"
echo "  Length: ${#IDEMPOTENT_RESPONSE}"
echo "  Content (first 150 chars): ${IDEMPOTENT_RESPONSE:0:150}"
echo ""

# Test 1: Check if response is non-empty and has reasonable length
echo -n "Test 1: Response is valid (length > 200)... "
if [ ! -z "$IDEMPOTENT_RESPONSE" ] && [ ${#IDEMPOTENT_RESPONSE} -gt 200 ]; then
    echo -e "${GREEN}✓ PASSED${NC}"
    TEST1_PASS=1
else
    echo -e "${RED}✗ FAILED${NC}"
    echo "  Response length: ${#IDEMPOTENT_RESPONSE}"
    TEST1_PASS=0
fi

# Extract run ID (same logic used by duplicate test)
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 ""
echo "Extracted FIRST_RUN_ID: [$FIRST_RUN_ID]"
echo ""

# Test 2: Duplicate request with same idempotency key
if [ ! -z "$FIRST_RUN_ID" ]; then
    echo "Making duplicate request with same idempotency key..."
    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>/dev/null)
    
    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
    
    echo "Extracted SECOND_RUN_ID: [$SECOND_RUN_ID]"
    echo ""
    
    echo -n "Test 2: Duplicate request returns same run... "
    if [ ! -z "$SECOND_RUN_ID" ] && [ "$FIRST_RUN_ID" = "$SECOND_RUN_ID" ]; then
        echo -e "${GREEN}✓ PASSED${NC}"
        TEST2_PASS=1
    else
        echo -e "${RED}✗ FAILED${NC}"
        echo "  First ID:  [$FIRST_RUN_ID]"
        echo "  Second ID: [$SECOND_RUN_ID]"
        TEST2_PASS=0
    fi
else
    echo -e "${RED}Cannot test duplicate - no run ID extracted${NC}"
    TEST2_PASS=0
fi

echo ""
echo "========================================="
echo "Results"
echo "========================================="
echo "Test 1 (Response valid): $([ "$TEST1_PASS" = "1" ] && echo "PASSED" || echo "FAILED")"
echo "Test 2 (Duplicate works): $([ "$TEST2_PASS" = "1" ] && echo "PASSED" || echo "FAILED")"
echo ""

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

