#!/bin/bash

# VectorLite API Endpoint Test Script
# Comprehensive testing of all API endpoints

set -e

BASE_URL="http://localhost:3002"
COLLECTION_NAME="api_test"

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

echo -e "${BLUE}🧪 VectorLite API Test Suite${NC}"
echo "================================="

# Function to test endpoint with error handling
test_endpoint() {
    local method=$1
    local endpoint=$2
    local data=$3
    local description=$4
    
    echo -e "\n${YELLOW}Testing: $description${NC}"
    echo "Endpoint: $method $endpoint"
    
    if [ -n "$data" ]; then
        response=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X $method "$BASE_URL$endpoint" \
            -H "Content-Type: application/json" \
            -d "$data")
    else
        response=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X $method "$BASE_URL$endpoint")
    fi
    
    http_code=$(echo "$response" | grep "HTTP_CODE:" | cut -d: -f2)
    body=$(echo "$response" | sed '/HTTP_CODE:/d')
    
    if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 300 ]; then
        echo -e "${GREEN}✅ Success (HTTP $http_code)${NC}"
        echo "$body" | jq . 2>/dev/null || echo "$body"
    else
        echo -e "${RED}❌ Failed (HTTP $http_code)${NC}"
        echo "$body" | jq . 2>/dev/null || echo "$body"
    fi
}

# 1. Health Check
test_endpoint "GET" "/health" "" "Health Check"

# 2. List Collections (initial)
test_endpoint "GET" "/collections" "" "List Collections (Initial)"

# 3. Create Collection
test_endpoint "POST" "/collections" '{"name": "'$COLLECTION_NAME'", "index_type": "hnsw"}' "Create Collection"

# 4. List Collections (after creation)
test_endpoint "GET" "/collections" "" "List Collections (After Creation)"

# 5. Get Collection Info
test_endpoint "GET" "/collections/$COLLECTION_NAME" "" "Get Collection Info"

# 6. Add Text Documents
test_endpoint "POST" "/collections/$COLLECTION_NAME/text" '{"text": "Machine learning is a subset of artificial intelligence"}' "Add Text Document 1"

test_endpoint "POST" "/collections/$COLLECTION_NAME/text" '{"text": "Vector databases enable fast similarity search"}' "Add Text Document 2"

test_endpoint "POST" "/collections/$COLLECTION_NAME/text" '{"text": "Neural networks are inspired by biological neurons"}' "Add Text Document 3"

# 7. Search by Text
test_endpoint "POST" "/collections/$COLLECTION_NAME/search/text" '{"query": "machine learning", "k": 2, "similarity_metric": "cosine"}' "Search by Text"

# 8. Get Vector by ID (using first text ID)
test_endpoint "GET" "/collections/$COLLECTION_NAME/vectors/0" "" "Get Vector by ID"

# 9. Get Updated Collection Info
test_endpoint "GET" "/collections/$COLLECTION_NAME" "" "Get Updated Collection Info"

# 10. Save Collection
test_endpoint "POST" "/collections/$COLLECTION_NAME/save" '{"file_path": "./test_save.vlc"}' "Save Collection"

# 11. Create Another Collection
test_endpoint "POST" "/collections/test_flat" '{"name": "test_flat", "index_type": "flat"}' "Create Flat Collection"

# 12. Load Collection
test_endpoint "POST" "/collections/load" '{"file_path": "./test_save.vlc", "collection_name": "loaded_test"}' "Load Collection"

# 13. Final Collection List
test_endpoint "GET" "/collections" "" "Final Collection List"

# 14. Delete Vector
test_endpoint "DELETE" "/collections/$COLLECTION_NAME/vectors/0" "" "Delete Vector"

# 15. Delete Collection
test_endpoint "DELETE" "/collections/test_flat" "" "Delete Collection"

# 16. Error Cases
echo -e "\n${YELLOW}Testing Error Cases:${NC}"
test_endpoint "GET" "/collections/nonexistent" "" "Get Nonexistent Collection"
test_endpoint "POST" "/collections/$COLLECTION_NAME/search/text" '{"query": "test", "k": 0}' "Search with Invalid K"

# Cleanup
echo -e "\n${BLUE}🧹 Cleanup${NC}"
rm -f test_save.vlc
echo -e "${GREEN}✅ Test completed!${NC}"
