#!/usr/bin/env python3
"""
Genesis Parallel Agent Monitoring System
Real-time status tracking, progress monitoring, and performance analytics
"""

import json
import time
import psutil
import requests
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Any
import subprocess
import threading

class GenesisAgentMonitor:
    def __init__(self):
        self.agents = {
            'genesis-memory-1': {
                'range': '1-73',
                'focus': 'ChromaDB semantic extraction',
                'port': 8001,
                'work_tree': '/mnt/e/genesis-system/parallel-deployment/genesis-memory-1'
            },
            'genesis-memory-2': {
                'range': '74-146', 
                'focus': 'Pinecone vector optimization',
                'port': 8002,
                'work_tree': '/mnt/e/genesis-system/parallel-deployment/genesis-memory-2'
            },
            'genesis-memory-3': {
                'range': '147-219',
                'focus': 'Qdrant knowledge graphs', 
                'port': 8003,
                'work_tree': '/mnt/e/genesis-system/parallel-deployment/genesis-memory-3'
            },
            'genesis-memory-4': {
                'range': '220-292',
                'focus': 'Mem0 intelligent compression',
                'port': 8004, 
                'work_tree': '/mnt/e/genesis-system/parallel-deployment/genesis-memory-4'
            },
            'genesis-memory-5': {
                'range': '293-365',
                'focus': 'Redis real-time coordination',
                'port': 8005,
                'work_tree': '/mnt/e/genesis-system/parallel-deployment/genesis-memory-5'
            }
        }
        
        self.monitoring_data = {
            'start_time': datetime.now().isoformat(),
            'agent_status': {},
            'performance_metrics': {},
            'progress_tracking': {}
        }
        
    def check_agent_status(self, agent_id: str) -> Dict[str, Any]:
        """Check individual agent status"""
        agent_config = self.agents[agent_id]
        work_tree_path = Path(agent_config['work_tree'])
        
        status = {
            'agent_id': agent_id,
            'timestamp': datetime.now().isoformat(),
            'work_tree_exists': work_tree_path.exists(),
            'config_files': {
                'mcp_config': (work_tree_path / 'genesis-specialized-mcp.json').exists(),
                'extraction_script': (work_tree_path / 'extract-conversations.py').exists(),
                'startup_script': (work_tree_path / 'start-agent.sh').exists()
            },
            'results': {
                'output_dir': (work_tree_path / 'results').exists(),
                'report_file': (work_tree_path / 'results' / 'GENESIS_CHRONOLOGICAL_SUPREMACY_REPORT.md').exists(),
                'log_files': len(list(work_tree_path.glob('*.log'))) if work_tree_path.exists() else 0
            },
            'process_status': self.check_process_running(agent_id),
            'resource_usage': self.get_resource_usage(agent_id)
        }
        
        # Calculate progress
        if status['results']['report_file']:
            status['progress'] = 'Complete'
            status['completion_percentage'] = 100
        elif status['process_status']['running']:
            status['progress'] = 'Processing'
            status['completion_percentage'] = self.estimate_progress(agent_id)
        else:
            status['progress'] = 'Not Started'
            status['completion_percentage'] = 0
            
        return status
    
    def check_process_running(self, agent_id: str) -> Dict[str, Any]:
        """Check if agent process is running"""
        try:
            # Look for python processes running agent scripts
            for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
                try:
                    if proc.info['name'] == 'python3':
                        cmdline = ' '.join(proc.info['cmdline'] or [])
                        if agent_id in cmdline and 'extract-conversations.py' in cmdline:
                            return {
                                'running': True,
                                'pid': proc.info['pid'],
                                'start_time': datetime.fromtimestamp(proc.create_time()).isoformat(),
                                'cpu_percent': proc.cpu_percent(),
                                'memory_mb': proc.memory_info().rss / 1024 / 1024
                            }
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    continue
            
            return {'running': False}
        except Exception as e:
            return {'running': False, 'error': str(e)}
    
    def get_resource_usage(self, agent_id: str) -> Dict[str, Any]:
        """Get system resource usage for agent"""
        try:
            # Get overall system stats
            cpu_percent = psutil.cpu_percent(interval=1)
            memory = psutil.virtual_memory()
            disk = psutil.disk_usage('/')
            
            return {
                'system_cpu_percent': cpu_percent,
                'system_memory_percent': memory.percent,
                'system_memory_available_gb': round(memory.available / 1024**3, 2),
                'disk_usage_percent': disk.percent,
                'disk_free_gb': round(disk.free / 1024**3, 2)
            }
        except Exception as e:
            return {'error': str(e)}
    
    def estimate_progress(self, agent_id: str) -> float:
        """Estimate agent progress based on log files and output"""
        try:
            agent_config = self.agents[agent_id]
            work_tree_path = Path(agent_config['work_tree'])
            
            # Check if results directory has any files
            results_dir = work_tree_path / 'results'
            if results_dir.exists():
                result_files = list(results_dir.glob('*'))
                if result_files:
                    return 75.0  # Has some output, likely 75% complete
            
            # Check for log files with content
            log_files = list(work_tree_path.glob('*.log'))
            if log_files:
                for log_file in log_files:
                    if log_file.stat().st_size > 1000:  # Has substantial content
                        return 50.0  # Processing actively
                        
            return 25.0  # Started but minimal progress
        except Exception:
            return 0.0
    
    def get_all_agent_status(self) -> Dict[str, Any]:
        """Get status of all agents"""
        all_status = {}
        summary = {
            'total_agents': len(self.agents),
            'agents_running': 0,
            'agents_complete': 0,
            'agents_not_started': 0,
            'overall_progress': 0.0
        }
        
        total_progress = 0.0
        
        for agent_id in self.agents.keys():
            status = self.check_agent_status(agent_id)
            all_status[agent_id] = status
            
            if status['progress'] == 'Complete':
                summary['agents_complete'] += 1
            elif status['progress'] == 'Processing':
                summary['agents_running'] += 1
            else:
                summary['agents_not_started'] += 1
                
            total_progress += status['completion_percentage']
        
        summary['overall_progress'] = round(total_progress / len(self.agents), 1)
        
        return {
            'timestamp': datetime.now().isoformat(),
            'summary': summary,
            'agents': all_status
        }
    
    def generate_status_report(self) -> str:
        """Generate human-readable status report"""
        status = self.get_all_agent_status()
        summary = status['summary']
        
        report = f"""
🤖 GENESIS PARALLEL AGENT STATUS REPORT
=======================================
Timestamp: {status['timestamp']}

📊 SUMMARY
----------
Total Agents: {summary['total_agents']}
Running: {summary['agents_running']} | Complete: {summary['agents_complete']} | Not Started: {summary['agents_not_started']}
Overall Progress: {summary['overall_progress']}%

🔍 AGENT DETAILS
----------------"""
        
        for agent_id, agent_status in status['agents'].items():
            focus = self.agents[agent_id]['focus']
            conv_range = self.agents[agent_id]['range']
            
            report += f"""
{agent_id}: {agent_status['progress']} ({agent_status['completion_percentage']}%)
  Range: Conversations {conv_range}
  Focus: {focus}
  Process: {'Running' if agent_status['process_status']['running'] else 'Stopped'}"""
            
            if agent_status['process_status']['running']:
                proc_info = agent_status['process_status']
                report += f"""
  PID: {proc_info['pid']} | CPU: {proc_info.get('cpu_percent', 0):.1f}% | Memory: {proc_info.get('memory_mb', 0):.1f}MB"""
                
        return report

# Real-time monitoring
if __name__ == "__main__":
    monitor = GenesisAgentMonitor()
    
    print("🤖 Starting Genesis Agent Monitoring...")
    print("📊 Real-time status updates every 30 seconds")
    print("💡 Press Ctrl+C to stop monitoring")
    print("")
    
    try:
        while True:
            report = monitor.generate_status_report()
            
            # Clear screen and show report
            print("\033[2J\033[H")  # Clear screen
            print(report)
            
            time.sleep(30)
    except KeyboardInterrupt:
        print("\n👋 Monitoring stopped")
