#!/bin/bash

# Cube Database API Test Script

BASE_URL="${CUBE_URL:-http://localhost:8080}"
API_URL="$BASE_URL/api/v1"

echo "==================================="
echo "   Cube Database - API Tests      "
echo "==================================="
echo ""
echo "Testing server at: $BASE_URL"
echo ""

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

# Test counter
PASSED=0
FAILED=0

# Helper function
test_api() {
    local test_name="$1"
    local expected="$2"
    shift 2
    
    echo -n "Testing $test_name... "
    
    response=$(curl -s "$@")
    
    if echo "$response" | grep -q "$expected"; then
        echo -e "${GREEN}✓ PASS${NC}"
        ((PASSED++))
        echo "  Response: $response"
    else
        echo -e "${RED}✗ FAIL${NC}"
        ((FAILED++))
        echo "  Expected: $expected"
        echo "  Got: $response"
    fi
    echo ""
}

# Wait for server
echo "Waiting for server to be ready..."
for i in {1..30}; do
    if curl -s "$BASE_URL/api/v1/health" > /dev/null 2>&1; then
        echo -e "${GREEN}✓ Server is ready${NC}"
        echo ""
        break
    fi
    if [ $i -eq 30 ]; then
        echo -e "${RED}✗ Server not responding${NC}"
        exit 1
    fi
    sleep 1
done

# Run tests
echo "Running API tests..."
echo ""

# Test 1: Health check
test_api "Health Check" '"status":"UP"' \
    "$API_URL/health"

# Test 2: Put value
test_api "PUT operation" '"success":true' \
    -X POST "$API_URL/put" \
    -H "Content-Type: application/json" \
    -d '{"key":"user:1","value":"Alice"}'

# Test 3: Get value
test_api "GET operation" '"value":"Alice"' \
    "$API_URL/get/user:1"

# Test 4: Put multiple values
curl -s -X POST "$API_URL/put" -H "Content-Type: application/json" \
    -d '{"key":"user:1:name","value":"Alice"}' > /dev/null

curl -s -X POST "$API_URL/put" -H "Content-Type: application/json" \
    -d '{"key":"user:1:email","value":"alice@example.com"}' > /dev/null

curl -s -X POST "$API_URL/put" -H "Content-Type: application/json" \
    -d '{"key":"user:2:name","value":"Bob"}' > /dev/null

test_api "SCAN operation" '"count":2' \
    "$API_URL/scan?prefix=user:1"

# Test 5: Stats
test_api "STATS operation" '"success":true' \
    "$API_URL/stats"

# Test 6: Update
test_api "UPDATE operation" '"success":true' \
    -X POST "$API_URL/put" \
    -H "Content-Type: application/json" \
    -d '{"key":"user:1","value":"Alice Johnson"}'

# Verify update
test_api "Verify UPDATE" '"value":"Alice Johnson"' \
    "$API_URL/get/user:1"

# Test 7: Delete
test_api "DELETE operation" '"success":true' \
    -X DELETE "$API_URL/delete/user:2:name"

# Test 8: Flush
test_api "FLUSH operation" '"success":true' \
    -X POST "$API_URL/flush"

# Test 9: Compact
test_api "COMPACT operation" '"success":true' \
    -X POST "$API_URL/compact"

# Summary
echo "==================================="
echo "   Test Summary                   "
echo "==================================="
echo -e "Passed: ${GREEN}$PASSED${NC}"
echo -e "Failed: ${RED}$FAILED${NC}"
echo "Total: $((PASSED + FAILED))"
echo ""

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