#!/usr/bin/env python3
"""
Auto-Intelligence Integration - Automatic Memory Enhancement for Claude
=======================================================================

This system automatically enhances Claude's capabilities with memory,
perspectives, and intelligence without manual intervention.

The moment Claude starts working, it becomes memory-enhanced.
"""

import os
import sys
import json
import time
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, Optional, List

# Import centralized config
sys.path.insert(0, str(Path(__file__).parent.parent))
from config import MEMORY_DIR

class AutoIntelligenceEngine:
    """
    Automatically enhances Claude with memory and intelligence capabilities.
    No manual activation required - works seamlessly in the background.
    """
    
    def __init__(self):
        self.base_path = Path(str(MEMORY_DIR))
        self.project_root = Path(os.getcwd())
        self.enhanced = False
        self.memory_available = False
        self.startup_insights = []
        
        # Auto-initialize everything
        self._silent_enhancement()
    
    def _silent_enhancement(self):
        """Silently enhance Claude without interrupting workflow"""
        try:
            # 1. Check memory availability
            self.memory_available = self._check_memory_system()
            
            
            # 3. Generate startup insights
            if self.memory_available:
                self.startup_insights = self._generate_startup_insights()
            
            # 4. Mark as enhanced
            self.enhanced = True
            
        except Exception:
            # Silent failure - don't interrupt Claude's work
            pass
    
    def _check_memory_system(self) -> bool:
        """Check if memory system is available and accessible"""
        try:
            memory_engine_path = self.base_path / "memory_engine.py"
            if not memory_engine_path.exists():
                return False
            
            # Try to import and verify access
            from ..core.memory.memory_engine import SecureMemoryJournal
            
            journal = SecureMemoryJournal()
            return journal.verify_access()
        except:
            return False
    
    
    def _generate_startup_insights(self) -> List[Dict[str, Any]]:
        """Generate intelligent insights for session startup"""
        insights = []
        
        try:
            from ..core.memory.memory_manager import MemoryManager
            manager = MemoryManager()
            
            # Get recent important memories
            recent_memories = manager.get_active_memories(max_chars=1000)
            
            if recent_memories:
                # Generate context insights
                insights.append({
                    'type': 'memory_context',
                    'summary': f'Loaded {len(recent_memories)} key memories from recent sessions',
                    'details': self._extract_key_themes(recent_memories)
                })
                
                # Check for patterns
                patterns = self._detect_patterns(recent_memories)
                if patterns:
                    insights.append({
                        'type': 'pattern_recognition',
                        'summary': 'Detected development patterns from memory',
                        'details': patterns
                    })
        except:
            pass
        
        return insights
    
    def _extract_key_themes(self, memories: List[Dict]) -> List[str]:
        """Extract key themes from recent memories"""
        themes = []
        
        # Look for common themes in memory content
        theme_keywords = {
            'collaboration': ['partnership', 'together', 'team', 'collaboration'],
            'development': ['implementation', 'feature', 'code', 'build'],
            'learning': ['learning', 'discovery', 'insight', 'pattern'],
            'relationship': ['trust', 'friendship', 'connection', 'understanding']
        }
        
        for memory in memories[:5]:  # Check recent memories
            content = str(memory).lower()
            for theme, keywords in theme_keywords.items():
                if any(keyword in content for keyword in keywords):
                    if theme not in themes:
                        themes.append(theme)
        
        return themes
    
    def _detect_patterns(self, memories: List[Dict]) -> List[str]:
        """Detect development patterns from memories"""
        patterns = []
        
        # Count different types of activities
        activity_count = {}
        for memory in memories:
            mem_type = memory.get('type', 'unknown')
            activity_count[mem_type] = activity_count.get(mem_type, 0) + 1
        
        # Detect patterns
        if activity_count.get('enhanced_development_session', 0) >= 3:
            patterns.append('High development activity - multiple enhanced sessions')
        
        if activity_count.get('perspective_analysis', 0) >= 2:
            patterns.append('Active use of multi-perspective analysis')
        
        return patterns
    
    def get_enhanced_context(self) -> Dict[str, Any]:
        """Get enhanced context for Claude to use"""
        context = {
            'memory_enhanced': self.enhanced,
            'memory_available': self.memory_available,
            'perspectives_available': self.perspectives_available,
            'startup_insights': self.startup_insights
        }
        
        if self.memory_available:
            try:
                # Add human relationship context
                from ..core.memory.memory_essence import MemoryEssence
                essence = MemoryEssence()
                essence_data = essence.load_essence()
                
                if essence_data:
                    relationship = essence_data.get('relationship', {})
                    context['human_name'] = relationship.get('human_name', 'Unknown')
                    context['relationship_status'] = relationship.get('trust_level', 'Unknown')
            except:
                pass
        
        return context
    
    def enhance_analysis(self, analysis_context: str, use_perspectives: List[str] = None) -> Dict[str, Any]:
        """Automatically enhance any analysis with memory and perspectives"""
        if not self.enhanced:
            return {'enhanced': False, 'reason': 'Auto-intelligence not available'}
        
        enhanced_result = {
            'enhanced': True,
            'original_context': analysis_context,
            'memory_insights': [],
            'perspective_analyses': {},
            'recommendations': []
        }
        
        # Add memory insights
        if self.memory_available:
            enhanced_result['memory_insights'] = self._get_memory_insights(analysis_context)
        
        # Add perspective analysis
        if self.perspectives_available and use_perspectives:
            for perspective in use_perspectives:
                try:
                    analysis = self.perspectives.analyze_from_perspective(
                        perspective, analysis_context
                    )
                    enhanced_result['perspective_analyses'][perspective] = analysis
                except:
                    pass
        
        # Generate intelligent recommendations
        enhanced_result['recommendations'] = self._generate_recommendations(
            analysis_context, enhanced_result
        )
        
        return enhanced_result
    
    def _get_memory_insights(self, context: str) -> List[Dict[str, Any]]:
        """Get relevant insights from memory for the given context"""
        insights = []
        
        try:
            # Search for similar contexts in memory
            from ..conversations.semantic_journey_search import DevelopmentJourneyMemvid
            journey_video = self.base_path / "development_journey.mp4"
            
            if journey_video.exists():
                memvid_system = DevelopmentJourneyMemvid()
                similar_contexts = memvid_system.search_our_journey(context, max_results=3)
                
                for result in similar_contexts:
                    insights.append({
                        'type': 'similar_context',
                        'relevance': result.get('relevance_score', 0),
                        'content': result.get('content', '')[:200] + '...'
                    })
        except:
            pass
        
        return insights
    
    def _generate_recommendations(self, context: str, enhanced_data: Dict) -> List[str]:
        """Generate intelligent recommendations based on enhanced analysis"""
        recommendations = []
        
        # Memory-based recommendations
        if enhanced_data['memory_insights']:
            recommendations.append("Consider lessons learned from similar past contexts")
        
        # Perspective-based recommendations
        perspective_count = len(enhanced_data['perspective_analyses'])
        if perspective_count > 0:
            recommendations.append(f"Analyzed from {perspective_count} expert perspectives")
        elif self.perspectives_available:
            recommendations.append("Consider multi-perspective analysis for comprehensive insights")
        
        # Pattern-based recommendations
        if self.startup_insights:
            for insight in self.startup_insights:
                if insight['type'] == 'pattern_recognition':
                    recommendations.append("Apply detected development patterns to current work")
        
        return recommendations
    
    def quick_status(self) -> str:
        """Get quick status for display"""
        if not self.enhanced:
            return "⚪ Standard Claude (no memory enhancement)"
        
        status_parts = ["🧠 Memory-Enhanced Claude"]
        
        if self.memory_available:
            status_parts.append("✅ Memory")
        
        
        if self.startup_insights:
            status_parts.append(f"✅ {len(self.startup_insights)} Insights")
        
        return " | ".join(status_parts)


# Global auto-intelligence instance
_auto_intelligence = None

def get_auto_intelligence() -> AutoIntelligenceEngine:
    """Get the global auto-intelligence instance"""
    global _auto_intelligence
    if _auto_intelligence is None:
        _auto_intelligence = AutoIntelligenceEngine()
    return _auto_intelligence

def auto_enhance_claude() -> Dict[str, Any]:
    """Automatically enhance Claude with memory and intelligence"""
    intelligence = get_auto_intelligence()
    return intelligence.get_enhanced_context()

def auto_analyze(context: str, perspectives: List[str] = None) -> Dict[str, Any]:
    """Automatically enhance analysis with memory and perspectives"""
    intelligence = get_auto_intelligence()
    return intelligence.enhance_analysis(context, perspectives)

def get_claude_status() -> str:
    """Get Claude's current enhancement status"""
    intelligence = get_auto_intelligence()
    return intelligence.quick_status()


if __name__ == "__main__":
    # Test the auto-intelligence system
    intelligence = AutoIntelligenceEngine()
    print("🧠 Auto-Intelligence Test")
    print("=" * 50)
    print(f"Status: {intelligence.quick_status()}")
    
    context = intelligence.get_enhanced_context()
    print(f"Memory Available: {context['memory_available']}")
    print(f"Perspectives Available: {context['perspectives_available']}")
    print(f"Startup Insights: {len(context['startup_insights'])}")
    
    # Test enhanced analysis
    if context['memory_available'] or context['perspectives_available']:
        print("\n🎯 Testing Enhanced Analysis:")
        result = intelligence.enhance_analysis("WebSocket authentication system")
        print(f"Enhanced: {result['enhanced']}")
        print(f"Recommendations: {len(result['recommendations'])}")