#!/usr/bin/env python3
"""
Test script to verify daemon statistics reading works
"""

import json
import sys
import os
from pathlib import Path

# Add the current directory to path
sys.path.append(str(Path(__file__).parent))

try:
    from mira_mcp.intelligent_gateway import IntelligentMIRAGateway
    
    print("🧪 Testing daemon statistics reading...")
    
    # Create gateway instance
    gateway = IntelligentMIRAGateway()
    
    # Test the status handler directly
    status_result = gateway._handle_intelligent_status({})
    
    print("📊 Status result:")
    print(json.dumps(status_result, indent=2))
    
    # Check if daemon stats are included
    if status_result.get("success") and "daemon_status" in status_result.get("data", {}):
        daemon_status = status_result["data"]["daemon_status"]
        print(f"\n✅ SUCCESS: Daemon statistics found!")
        print(f"   Conversations indexed: {daemon_status.get('total_conversations_indexed', 'N/A')}")
        print(f"   MCP tool calls: {daemon_status.get('total_mcp_tool_calls', 'N/A')}")
    else:
        print(f"\n❌ ISSUE: Daemon statistics not found in response")
        
    # Also test reading daemon files directly
    print(f"\n📂 Direct file reading test:")
    
    unified_file = "/tmp/mira-unified-daemon-status.json"
    regular_file = "/tmp/mira-daemon-status.json"
    
    if os.path.exists(unified_file):
        with open(unified_file, 'r') as f:
            unified_stats = json.load(f)
            print(f"   Unified daemon: {unified_stats.get('conversationsIndexed', 0)} conversations, {unified_stats.get('mcpToolCalls', 0)} MCP calls")
    
    if os.path.exists(regular_file):
        with open(regular_file, 'r') as f:
            regular_stats = json.load(f)
            print(f"   Regular daemon: {regular_stats.get('conversationsIndexed', 0)} conversations")
            
except Exception as e:
    print(f"❌ Test failed: {e}")
    import traceback
    traceback.print_exc()