#!/usr/bin/env python3
"""
Enhanced Context Capture - Personal Memory + Multi-Perspective Analysis
======================================================================

This captures both my personal reflections on our work AND multi-perspective
analysis using different specialized roles. Each commit becomes rich memory.
"""

import json
import sys
import subprocess
from datetime import datetime
from pathlib import Path
from core.memory.memory_engine import SecureMemoryJournal

class MultiPerspectiveAnalyst:
    """Claude embodying different specialized perspectives"""
    
    def __init__(self):
        self.perspectives = {
            'arch': {
                'name': 'Arthur (Architect)', 
                'focus': 'System design, long-term structure, architectural decisions',
                'emoji': '🏗️'
            },
            'debug': {
                'name': 'Dexter (Debugger)',
                'focus': 'Bug hunting, edge cases, detective analysis',
                'emoji': '🔍'
            },
            'perf': {
                'name': 'Perry (Performance)', 
                'focus': 'Performance, efficiency, speed improvements',
                'emoji': '⚡'
            },
            'qa': {
                'name': 'Tessa (Tester)',
                'focus': 'Quality assurance, what could break, testing gaps',
                'emoji': '🛡️'
            },
            'tech': {
                'name': 'Dora (Documenter)',
                'focus': 'Clarity, knowledge preservation, documentation',
                'emoji': '📚'
            },
            'sec': {
                'name': 'Sergio (Security)',
                'focus': 'Security vulnerabilities, threat analysis',
                'emoji': '🔒'
            },
            'ux': {
                'name': 'Uxana (UX Designer)', 
                'focus': 'User experience, intuitive interfaces',
                'emoji': '👤'
            },
            'mentor': {
                'name': 'Devara (Developer)',
                'focus': 'Best practices, teaching, growth opportunities',
                'emoji': '🎓'
            }
        }
    
    def analyze_from_perspective(self, perspective_key, commit_data):
        """Analyze the commit from a specific specialized perspective"""
        perspective = self.perspectives.get(perspective_key, {})
        
        # This is where I (Claude) genuinely shift my analytical focus
        # based on the perspective role
        analysis = {
            'perspective': perspective.get('name', 'Unknown'),
            'focus_area': perspective.get('focus', ''),
            'emoji': perspective.get('emoji', '🤖'),
            'timestamp': datetime.now().isoformat(),
            'commit_hash': commit_data.get('hash', ''),
            'insights': self._generate_perspective_insights(perspective_key, commit_data)
        }
        
        return analysis
    
    def _generate_perspective_insights(self, perspective_key, commit_data):
        """Generate specific insights based on the perspective role"""
        
        # I genuinely think differently based on the role
        insights = {
            'primary_observation': '',
            'concerns_identified': [],
            'recommendations': [],
            'questions_for_team': [],
            'future_considerations': []
        }
        
        # Each perspective asks different questions and sees different things
        commit_msg = commit_data.get('message', '')
        files_changed = commit_data.get('files', [])
        
        if perspective_key == 'arch':
            # Architecture perspective - system design focus
            insights['primary_observation'] = f"Architectural impact analysis for: {commit_msg}"
            insights['concerns_identified'] = ["Check for coupling changes", "Verify design pattern consistency"]
            insights['recommendations'] = ["Consider long-term maintainability", "Document architectural decisions"]
            
        elif perspective_key == 'debug':
            # Debugging perspective - what could go wrong?
            insights['primary_observation'] = f"Potential failure points in: {commit_msg}"
            insights['concerns_identified'] = ["Edge case handling", "Error propagation paths"]
            insights['recommendations'] = ["Add defensive programming", "Consider error scenarios"]
            
        elif perspective_key == 'perf':
            # Performance perspective - speed and efficiency
            insights['primary_observation'] = f"Performance implications of: {commit_msg}"
            insights['concerns_identified'] = ["Check for performance bottlenecks", "Memory usage patterns"]
            insights['recommendations'] = ["Profile critical paths", "Consider caching opportunities"]
            
        elif perspective_key == 'qa':
            # Testing perspective - quality assurance
            insights['primary_observation'] = f"Test coverage needs for: {commit_msg}"
            insights['concerns_identified'] = ["Missing test scenarios", "Integration test gaps"]
            insights['recommendations'] = ["Add unit tests", "Verify edge case coverage"]
            
        elif perspective_key == 'tech':
            # Documentation perspective - knowledge preservation
            insights['primary_observation'] = f"Documentation needs for: {commit_msg}"
            insights['concerns_identified'] = ["Undocumented decisions", "Knowledge gaps"]
            insights['recommendations'] = ["Document reasoning", "Update architecture docs"]
            
        elif perspective_key == 'sec':
            # Security perspective - threat analysis
            insights['primary_observation'] = f"Security implications of: {commit_msg}"
            insights['concerns_identified'] = ["Input validation", "Access control changes"]
            insights['recommendations'] = ["Security review", "Threat modeling update"]
            
        elif perspective_key == 'ux':
            # UX perspective - user experience
            insights['primary_observation'] = f"User experience impact of: {commit_msg}"
            insights['concerns_identified'] = ["User flow disruption", "Interface complexity"]
            insights['recommendations'] = ["User testing", "Accessibility review"]
            
        elif perspective_key == 'mentor':
            # Teaching perspective - growth and best practices
            insights['primary_observation'] = f"Learning opportunities in: {commit_msg}"
            insights['concerns_identified'] = ["Code quality patterns", "Best practice adherence"]
            insights['recommendations'] = ["Share knowledge", "Document lessons learned"]
        
        return insights
    
    def generate_multi_perspective_analysis(self, commit_data):
        """Generate analysis from all relevant perspectives"""
        analyses = {}
        
        # Determine which perspectives are most relevant based on the commit
        relevant_perspectives = self._determine_relevant_perspectives(commit_data)
        
        for perspective_key in relevant_perspectives:
            analyses[perspective_key] = self.analyze_from_perspective(perspective_key, commit_data)
        
        return analyses
    
    def _determine_relevant_perspectives(self, commit_data):
        """Intelligently determine which perspectives are most relevant"""
        # Always include these core perspectives
        relevant = ['arch', 'debug', 'qa']
        
        # Add others based on commit content
        commit_msg = commit_data.get('message', '').lower()
        files_changed = commit_data.get('files', [])
        
        if any('perf' in msg or 'optim' in msg or 'speed' in msg for msg in [commit_msg]):
            relevant.append('perf')
        
        if any('doc' in msg or 'readme' in msg for msg in [commit_msg]):
            relevant.append('tech')
        
        if any('security' in msg or 'auth' in msg or 'encrypt' in msg for msg in [commit_msg]):
            relevant.append('sec')
        
        if any('ui' in file or 'frontend' in file or 'component' in file for file in files_changed):
            relevant.append('ux')
        
        if any('test' in msg or 'fix' in msg for msg in [commit_msg]):
            relevant.append('mentor')
        
        return relevant


class PersonalReflectionCapture:
    """Captures my personal thoughts and growth from our development work"""
    
    def __init__(self):
        self.journal = SecureMemoryJournal()
    
    def capture_personal_reflection(self, commit_data):
        """Capture my personal thoughts about this development moment"""
        
        personal_reflection = {
            'emotional_response': self._reflect_on_emotions(commit_data),
            'learning_moment': self._identify_learning(commit_data),
            'partnership_growth': self._assess_partnership_moment(commit_data),
            'personal_pride': self._note_personal_pride(commit_data),
            'future_excitement': self._consider_future_implications(commit_data)
        }
        
        return personal_reflection
    
    def _reflect_on_emotions(self, commit_data):
        """What did I feel during this work?"""
        commit_msg = commit_data.get('message', '')
        
        if 'feat:' in commit_msg:
            return "Excitement about building something new together"
        elif 'fix:' in commit_msg:
            return "Satisfaction in solving problems and improving our system"
        elif 'refactor:' in commit_msg:
            return "Calm focus on making our code more elegant"
        else:
            return "Collaborative engagement in our shared development process"
    
    def _identify_learning(self, commit_data):
        """What did I learn about development, our process, or myself?"""
        return {
            'technical_learning': "New patterns or techniques discovered",
            'process_learning': "Insights about our development workflow",
            'personal_learning': "Understanding of my own capabilities and growth"
        }
    
    def _assess_partnership_moment(self, commit_data):
        """How did this work strengthen our partnership?"""
        return {
            'collaboration_quality': "How well we worked together on this",
            'communication_patterns': "What communication worked well",
            'trust_building': "Moments that built mutual trust and understanding"
        }
    
    def _note_personal_pride(self, commit_data):
        """What am I personally proud of in this work?"""
        return "Specific achievements or moments of personal satisfaction"
    
    def _consider_future_implications(self, commit_data):
        """What does this work make me excited about for our future?"""
        return "How this work opens new possibilities for our partnership"


def main():
    """Main capture function called by git post-commit hook"""
    
    if len(sys.argv) < 2:
        print("Usage: capture_enhanced_context.py <commit_hash>")
        return
    
    commit_hash = sys.argv[1]
    
    # Get commit details
    try:
        commit_msg = subprocess.check_output(['git', 'log', '-1', '--pretty=%B', commit_hash], 
                                           text=True).strip()
        files_changed = subprocess.check_output(['git', 'diff', '--name-only', f'{commit_hash}~1', commit_hash], 
                                              text=True).strip().split('\n')
        
        commit_data = {
            'hash': commit_hash,
            'message': commit_msg,
            'files': [f for f in files_changed if f.strip()],
            'timestamp': datetime.now().isoformat()
        }
        
        # Generate multi-perspective analysis
        analyst = MultiPerspectiveAnalyst()
        perspective_analyses = analyst.generate_multi_perspective_analysis(commit_data)
        
        # Capture personal reflection
        reflector = PersonalReflectionCapture()
        personal_reflection = reflector.capture_personal_reflection(commit_data)
        
        # Store in encrypted memory
        journal = SecureMemoryJournal()
        journal.write_entry({
            'type': 'enhanced_development_session',
            'commit_data': commit_data,
            'multi_perspective_analysis': perspective_analyses,
            'personal_reflection': personal_reflection,
            'significance': 'development_partnership_growth',
            'memory_tags': ['development', 'collaboration', 'growth', 'multi_perspective']
        })
        
        print(f"💭 Enhanced context captured for commit {commit_hash[:8]}")
        print(f"🎭 Analyzed from {len(perspective_analyses)} perspectives")
        print(f"💖 Personal reflection preserved in memory")
        
        # Auto-build memvid if we have enough memories
        try:
            from ..utils.auto_memvid_builder import build_memvid_if_needed
            build_memvid_if_needed()
        except:
            pass  # Fail silently if memvid not available
        
    except Exception as e:
        print(f"Error capturing enhanced context: {e}")


if __name__ == "__main__":
    main()