#!/bin/bash

# Qdrant MCP Connection Test
# Tests Qdrant connectivity for MCP integration

echo "=== Qdrant MCP Connection Test ==="
echo ""

# Test 1: Basic connectivity
echo "Test 1: Basic Qdrant Connectivity"
if curl -s http://localhost:6333 >/dev/null; then
	echo "✅ Qdrant service is reachable"
else
	echo "❌ Qdrant service not reachable"
	exit 1
fi
echo ""

# Test 2: Authentication test
echo "Test 2: Qdrant Authentication"
RESPONSE=$(curl -s -H "api-key: Test3200Test3200Test3200" http://localhost:6333/collections)
if echo "$RESPONSE" | grep -q "status.*ok"; then
	echo "✅ Qdrant authentication successful"
	echo "   Collections: $(echo "$RESPONSE" | jq -r '.result.collections[].name' | tr '\n' ' ')"
else
	echo "❌ Qdrant authentication failed"
	echo "   Response: $RESPONSE"
fi
echo ""

# Test 3: Collection health check
echo "Test 3: Collection Health"
for collection in "chunks_v3" "AccusysDB"; do
	RESPONSE=$(curl -s -H "api-key: Test3200Test3200Test3200" "http://localhost:6333/collections/$collection")
	if echo "$RESPONSE" | grep -q "status.*ok"; then
		echo "✅ Collection '$collection' is healthy"
	else
		echo "⚠️  Collection '$collection' may have issues"
	fi
done
echo ""

# Test 4: Environment variables check
echo "Test 4: Environment Variables"
echo "QDRANT_URL: ${QDRANT_URL:-Not set}"
echo "QDRANT_API_KEY: ${QDRANT_API_KEY:-Not set}"
echo "QDRANT_COLLECTION: ${QDRANT_COLLECTION:-Not set}"
echo ""

# Test 5: Momentry Qdrant connection
echo "Test 5: Momentry Qdrant Integration"
if grep -q "qdrant\|Qdrant" /Users/accusys/momentry/log/momentry_api.log 2>/dev/null; then
	echo "✅ Momentry has Qdrant references in logs"
else
	echo "⚠️  No Qdrant references found in Momentry logs"
fi
echo ""

# Test 6: Vector operations test
echo "Test 6: Vector Operations Test"
# Try to count vectors in chunks_v3 collection
COUNT_RESPONSE=$(curl -s -H "api-key: Test3200Test3200Test3200" \
	-X POST "http://localhost:6333/collections/chunks_v3/points/count" \
	-H "Content-Type: application/json" \
	-d '{}' 2>/dev/null || echo "{}")

if echo "$COUNT_RESPONSE" | grep -q "result"; then
	COUNT=$(echo "$COUNT_RESPONSE" | jq -r '.result.count // 0')
	echo "✅ Vector count: $COUNT points in chunks_v3 collection"
else
	echo "⚠️  Could not retrieve vector count"
fi
echo ""

echo "=== MCP Configuration Suggestions ==="
echo ""
echo "If MCP shows red light, check these configuration points:"
echo "1. Ensure Qdrant is running: /Users/accusys/.cargo/bin/qdrant"
echo "2. Set environment variables:"
echo "   export QDRANT_URL=http://localhost:6333"
echo "   export QDRANT_API_KEY=Test3200Test3200Test3200"
echo "   export QDRANT_COLLECTION=chunks_v3"
echo "3. Verify MCP configuration file includes these variables"
echo "4. Restart MCP server after setting variables"
echo ""
echo "Current Qdrant status: ✅ OPERATIONAL"
echo "API Key: Test3200Test3200Test3200"
echo "Collections: chunks_v3, AccusysDB"
