#!/usr/bin/env python3
"""
Simple test to verify Socket.IO fixes work without full server.
"""

import json
import os
import subprocess
import sys
from pathlib import Path
from datetime import datetime

# Add project to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root / "src"))

def test_hook_handler_improvements():
    """Test hook handler with the fixes applied."""
    print("🧪 Testing improved hook handler")
    print("=" * 50)
    
    # Set up environment to enable hook emission
    env = os.environ.copy()
    env['CLAUDE_MPM_HOOK_DEBUG'] = 'true'
    env['PYTHONPATH'] = str(project_root / "src")
    # Note: NOT setting CLAUDE_MPM_SOCKETIO_PORT to test the POOL_AVAILABLE condition
    
    # Create test hook event
    hook_event = {
        "hook_event_name": "UserPromptSubmit",
        "prompt": "Test prompt for improved hook handler",
        "session_id": "improvement_test",
        "cwd": str(Path.cwd()),
        "timestamp": datetime.now().isoformat()
    }
    
    hook_json = json.dumps(hook_event)
    hook_handler_path = project_root / "src" / "claude_mpm" / "hooks" / "claude_hooks" / "hook_handler.py"
    
    print(f"📤 Testing hook event: {hook_event['hook_event_name']}")
    
    try:
        result = subprocess.run(
            [sys.executable, str(hook_handler_path)],
            input=hook_json,
            text=True,
            capture_output=True,
            env=env,
            timeout=10
        )
        
        print(f"📤 Exit code: {result.returncode}")
        print(f"📤 Stdout: {result.stdout}")
        
        if result.stderr:
            print(f"📤 Stderr (should show hook processing):")
            stderr_lines = result.stderr.strip().split('\n')
            for line in stderr_lines:
                if line:
                    print(f"   {line}")
        
        # Check for improvements
        improvements_detected = []
        
        if result.returncode == 0:
            improvements_detected.append("Hook handler executed successfully")
        
        if "Socket.IO connection pool" in result.stderr:
            improvements_detected.append("Connection pool initialization detected")
        
        if "Emitted pooled Socket.IO event" in result.stderr:
            improvements_detected.append("Socket.IO event emission detected")
        elif "Failed to emit batch" in result.stderr:
            improvements_detected.append("Socket.IO emission attempted (failed due to no server)")
        
        print(f"\n✅ Improvements detected:")
        for improvement in improvements_detected:
            print(f"   • {improvement}")
        
        return len(improvements_detected) >= 2
        
    except Exception as e:
        print(f"❌ Test failed: {e}")
        return False

def test_connection_pool_fixes():
    """Test connection pool with fixes."""
    print(f"\n🧪 Testing connection pool fixes")
    print("=" * 50)
    
    try:
        from claude_mpm.core.socketio_pool import get_connection_pool, SOCKETIO_AVAILABLE
        
        if not SOCKETIO_AVAILABLE:
            print("❌ Socket.IO packages not available")
            return False
        
        print("✅ Socket.IO packages available")
        
        # Get connection pool (should not crash)
        pool = get_connection_pool()
        print("✅ Connection pool created successfully")
        
        # Check if pool is running
        print(f"   Pool running: {pool._running}")
        
        # Check batch thread
        if pool.batch_thread and pool.batch_thread.is_alive():
            print("✅ Batch processing thread is running")
        else:
            print("⚠️ Batch processing thread not running")
        
        # Test event emission (will fail to connect but shouldn't crash)
        test_data = {
            'event_type': 'fix_test',
            'message': 'Testing connection pool fixes',
            'timestamp': datetime.now().isoformat()
        }
        
        print("🧪 Testing event emission (expect connection failure)...")
        pool.emit_event('/hook', 'fix_test', test_data)
        
        import time
        time.sleep(2)  # Wait for batch processing
        
        # Get stats
        stats = pool.get_stats()
        print(f"📊 Pool stats:")
        for key, value in stats.items():
            print(f"   {key}: {value}")
        
        # Success if pool was created and is running
        return pool._running and pool.batch_thread.is_alive()
        
    except Exception as e:
        print(f"❌ Connection pool test failed: {e}")
        import traceback
        print(f"Stack trace: {traceback.format_exc()}")
        return False

def main():
    """Test the Socket.IO fixes."""
    print("🔧 Testing Socket.IO Hook Integration Fixes")
    print("=" * 60)
    
    # Test 1: Hook handler improvements
    hook_success = test_hook_handler_improvements()
    
    # Test 2: Connection pool fixes  
    pool_success = test_connection_pool_fixes()
    
    print(f"\n" + "=" * 60)
    print(f"🎯 Fix Verification Results:")
    print(f"   Hook handler improvements: {'✅ Working' if hook_success else '❌ Issues'}")
    print(f"   Connection pool fixes:     {'✅ Working' if pool_success else '❌ Issues'}")
    
    overall_success = hook_success and pool_success
    
    print(f"\n🎉 OVERALL: {'✅ FIXES SUCCESSFUL' if overall_success else '❌ FIXES INCOMPLETE'}")
    
    if overall_success:
        print(f"\n✅ Socket.IO hook integration fixes are working!")
        print(f"\nThe fixes applied:")
        print(f"  1. ✅ Connection pool namespace issue resolved")
        print(f"  2. ✅ Hook handler environment check improved")  
        print(f"  3. ✅ Server handler signature fixed (when server runs)")
        print(f"\nTo complete testing:")
        print(f"  1. Start Socket.IO server: python -m claude_mpm.services.socketio_server")
        print(f"  2. Run hook tests with server running")
        print(f"  3. Test with actual Claude --monitor command")
    else:
        print(f"\n❌ Some fixes need more work:")
        if not hook_success:
            print(f"   - Hook handler improvements not working properly")
        if not pool_success:
            print(f"   - Connection pool fixes have issues")
    
    return overall_success

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)