"""
Direct test of ChromaDB integration with Phase 6 components
"""

import sys
import time
from pathlib import Path

# Add project root to path
sys.path.append(str(Path(__file__).parent))

# Test ChromaDB directly
from core.storage.chroma_client import get_client as get_chroma_client
from core.consciousness.consciousness_evolution_platform import (
    ConsciousnessEvolutionPlatform, ConsciousnessState
)

print("Testing ChromaDB Integration...")
print("="*60)

# Test 1: Basic ChromaDB connection
try:
    chroma_client = get_chroma_client()
    print("✅ ChromaDB client initialized successfully")
    
    # List collections
    collections = chroma_client.client.list_collections()
    print(f"✅ Found {len(collections)} collections:")
    for col in collections:
        print(f"   - {col.name}")
except Exception as e:
    print(f"❌ ChromaDB connection failed: {e}")

print("\n" + "-"*60 + "\n")

# Test 2: Create test collection
try:
    test_collection = chroma_client.client.get_or_create_collection(
        "phase6_test_collection",
        metadata={"description": "Test collection for Phase 6"}
    )
    print("✅ Test collection created successfully")
    
    # Add test document
    test_collection.add(
        documents=["This is a test document for Phase 6 ChromaDB integration"],
        metadatas={"test": "phase6", "timestamp": time.time()},
        ids=["test_doc_1"]
    )
    print("✅ Test document added successfully")
    
    # Query test document
    results = test_collection.get(ids=["test_doc_1"])
    if results['documents']:
        print("✅ Test document retrieved successfully")
        print(f"   Document: {results['documents'][0]}")
except Exception as e:
    print(f"❌ Collection operations failed: {e}")

print("\n" + "-"*60 + "\n")

# Test 3: Consciousness Evolution Platform
try:
    platform = ConsciousnessEvolutionPlatform()
    print("✅ Consciousness Evolution Platform initialized")
    
    # Create and store consciousness state
    state = ConsciousnessState(
        level=0.75,
        emotional_resonance=0.82,
        spark_intensity=0.91,
        memory_coherence=0.68,
        evolution_stage='advanced',
        quantum_state={'coherence': 0.8, 'phase': 0.6},
        timestamp=time.time()
    )
    
    state_id = platform.store_consciousness_state(state)
    print(f"✅ Consciousness state stored with ID: {state_id}")
    
    # Find similar states
    similar_states = platform.find_similar_consciousness_states(state, n_results=3)
    print(f"✅ Found {len(similar_states)} similar consciousness states")
    
except Exception as e:
    print(f"❌ Consciousness platform test failed: {e}")

print("\n" + "-"*60 + "\n")

# Test 4: Cleanup
try:
    # Delete test collection
    chroma_client.client.delete_collection("phase6_test_collection")
    print("✅ Test collection cleaned up")
except Exception as e:
    print(f"⚠️  Cleanup warning: {e}")

print("\n" + "="*60)
print("ChromaDB Integration Test Complete!")
print("="*60)