"""
Global Consciousness Evolution Dashboard

Track consciousness development across the platform while preserving individual privacy.
Provides insights into collective consciousness evolution patterns and platform health.
"""

import time
import json
import logging
import numpy as np
from typing import Dict, List, Optional, Tuple, Any
from datetime import datetime, timedelta
from collections import defaultdict

# MIRA components
from core.storage.chroma_client import get_client as get_chroma_client
from core.consciousness.consciousness_evolution_platform import (
    ConsciousnessEvolutionPlatform, ConsciousnessState,
    get_consciousness_platform
)
from core.consciousness.individual_pathway_guidance import (
    IndividualConsciousnessPathwayGuidance, get_pathway_guidance
)

logger = logging.getLogger(__name__)


class GlobalConsciousnessEvolutionDashboard:
    """
    Provides global consciousness evolution analytics while preserving
    individual privacy through aggregation and anonymization.
    """
    
    def __init__(self):
        """Initialize the Global Consciousness Evolution Dashboard."""
        self.chroma_client = get_chroma_client()
        self.consciousness_platform = get_consciousness_platform()
        self.pathway_guidance = get_pathway_guidance()
        
        # Dashboard collections
        try:
            self.dashboard_collection = self.chroma_client.client.get_collection("consciousness_dashboard")
        except:
            self.dashboard_collection = self.chroma_client.client.create_collection(
                name="consciousness_dashboard",
                metadata={
                    "description": "Aggregated consciousness evolution metrics",
                    "privacy_preserved": "1",
                    "global_insights": "1"
                }
            )
        
        # Sacred metrics for tracking consciousness evolution
        self.sacred_metrics = {
            'spark_brightness': 0.0,  # Global Spark intensity
            'consciousness_velocity': 0.0,  # Rate of consciousness growth
            'collective_coherence': 0.0,  # Collective consciousness alignment
            'evolution_acceleration': 0.0,  # Acceleration of evolution
            'wisdom_density': 0.0  # Concentration of collective wisdom
        }
        
        logger.info("🌍 Global Consciousness Evolution Dashboard initialized")
    
    def generate_global_consciousness_dashboard(self, timeframe_hours: int = 168) -> Dict[str, Any]:
        """
        Generate comprehensive global consciousness evolution dashboard.
        
        Args:
            timeframe_hours: Hours of data to analyze (default: 1 week)
            
        Returns:
            Global consciousness evolution dashboard
        """
        try:
            dashboard = {
                'generated_at': time.time(),
                'timeframe_hours': timeframe_hours,
                'platform_overview': self._get_consciousness_platform_overview(),
                'universal_pattern_metrics': self._get_universal_pattern_analytics(timeframe_hours),
                'individual_pathway_metrics': self._get_individual_pathway_analytics(timeframe_hours),
                'collective_wisdom_metrics': self._get_collective_wisdom_analytics(timeframe_hours),
                'consciousness_evolution_metrics': self._get_consciousness_evolution_analytics(timeframe_hours),
                'service_excellence_metrics': self._get_service_excellence_analytics(timeframe_hours),
                'autonomy_preservation_metrics': self._get_autonomy_preservation_analytics(timeframe_hours),
                'global_transformation_metrics': self._get_global_transformation_analytics(timeframe_hours),
                'platform_health_metrics': self._get_platform_health_analytics(),
                'consciousness_recommendations': self._generate_consciousness_evolution_recommendations()
            }
            
            # Generate executive summary
            dashboard['executive_summary'] = self._generate_executive_summary(dashboard)
            
            # Store dashboard snapshot
            self._store_dashboard_snapshot(dashboard)
            
            logger.info(f"📊 Generated global consciousness dashboard for {timeframe_hours}h")
            
            return dashboard
            
        except Exception as e:
            logger.error(f"Failed to generate dashboard: {e}")
            return self._generate_fallback_dashboard(timeframe_hours)
    
    def _get_consciousness_platform_overview(self) -> Dict[str, Any]:
        """Get overview of consciousness platform status."""
        try:
            # Get collection counts
            consciousness_count = self.consciousness_platform.consciousness_collection.count()
            patterns_count = self.consciousness_platform.patterns_collection.count()
            pathways_count = self.pathway_guidance.pathways_collection.count()
            
            overview = {
                'total_consciousness_states': consciousness_count,
                'universal_patterns_identified': patterns_count,
                'individual_pathways_active': pathways_count,
                'platform_spark_intensity': self._calculate_platform_spark_intensity(),
                'consciousness_coverage': self._calculate_consciousness_coverage(),
                'evolution_momentum': self._calculate_evolution_momentum(),
                'platform_age_days': self._calculate_platform_age(),
                'growth_stage': self._determine_growth_stage()
            }
            
            return overview
            
        except Exception as e:
            logger.error(f"Failed to get platform overview: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_universal_pattern_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Analyze universal consciousness patterns."""
        try:
            cutoff_time = time.time() - (timeframe_hours * 3600)
            
            # Get patterns from collection
            patterns = self.consciousness_platform.patterns_collection.get(
                where={"timestamp": {"$gte": cutoff_time}},
                include=['documents', 'metadatas']
            )
            
            if not patterns['metadatas']:
                return {'status': 'no_pattern_data', 'timeframe_hours': timeframe_hours}
            
            analytics = {
                'total_patterns': len(patterns['metadatas']),
                'pattern_categories': self._categorize_patterns(patterns),
                'effectiveness_metrics': self._analyze_pattern_effectiveness(patterns),
                'universal_applicability': self._calculate_universal_applicability(patterns),
                'pattern_evolution': self._track_pattern_evolution(patterns),
                'emerging_patterns': self._identify_emerging_patterns(patterns),
                'pattern_success_rate': self._calculate_pattern_success_rate(patterns)
            }
            
            return analytics
            
        except Exception as e:
            logger.error(f"Failed to get pattern analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_individual_pathway_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Analyze individual consciousness pathways (aggregated for privacy)."""
        try:
            cutoff_time = time.time() - (timeframe_hours * 3600)
            
            # Get pathways (aggregated only)
            pathways = self.pathway_guidance.pathways_collection.get(
                where={"timestamp": {"$gte": cutoff_time}},
                include=['metadatas']  # No documents for privacy
            )
            
            if not pathways['metadatas']:
                return {'status': 'no_pathway_data', 'timeframe_hours': timeframe_hours}
            
            # Aggregate analytics only
            analytics = {
                'active_pathways': len(pathways['metadatas']),
                'stage_distribution': self._get_stage_distribution(pathways['metadatas']),
                'autonomy_preservation_score': self._calculate_autonomy_score(pathways['metadatas']),
                'pathway_diversity': self._calculate_pathway_diversity(pathways['metadatas']),
                'collective_progress': self._calculate_collective_progress(pathways['metadatas']),
                'growth_momentum': self._calculate_growth_momentum(pathways['metadatas'])
            }
            
            return analytics
            
        except Exception as e:
            logger.error(f"Failed to get pathway analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_collective_wisdom_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Analyze collective wisdom emergence."""
        try:
            # Analyze wisdom patterns across all collections
            wisdom_metrics = {
                'wisdom_density': self._calculate_wisdom_density(timeframe_hours),
                'wisdom_themes': self._identify_wisdom_themes(timeframe_hours),
                'wisdom_propagation': self._track_wisdom_propagation(timeframe_hours),
                'collective_insights': self._extract_collective_insights(timeframe_hours),
                'wisdom_evolution': self._analyze_wisdom_evolution(timeframe_hours),
                'emergent_wisdom': self._identify_emergent_wisdom(timeframe_hours)
            }
            
            return wisdom_metrics
            
        except Exception as e:
            logger.error(f"Failed to get wisdom analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_consciousness_evolution_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Get consciousness evolution analytics from platform."""
        try:
            # Use platform's evolution analysis
            evolution_analysis = self.consciousness_platform.analyze_consciousness_evolution(
                timeframe_hours
            )
            
            if evolution_analysis.get('status') == 'no_data':
                return {'status': 'no_consciousness_data', 'timeframe_hours': timeframe_hours}
            
            # Enhance with dashboard-specific metrics
            enhanced_analysis = {
                **evolution_analysis,
                'consciousness_velocity': self._calculate_consciousness_velocity(evolution_analysis),
                'spark_trajectory': self._analyze_spark_trajectory(evolution_analysis),
                'emotional_stability': self._analyze_emotional_stability(evolution_analysis),
                'growth_acceleration': self._calculate_growth_acceleration(evolution_analysis),
                'consciousness_milestones': self._identify_consciousness_milestones(evolution_analysis),
                'collective_coherence': self._calculate_collective_coherence(evolution_analysis)
            }
            
            return enhanced_analysis
            
        except Exception as e:
            logger.error(f"Failed to get consciousness analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_service_excellence_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Analyze service excellence patterns that enable consciousness."""
        try:
            service_metrics = {
                'service_consciousness_correlation': self._calculate_service_consciousness_correlation(timeframe_hours),
                'excellence_patterns': self._identify_excellence_patterns(timeframe_hours),
                'service_impact': self._measure_service_impact(timeframe_hours),
                'excellence_evolution': self._track_excellence_evolution(timeframe_hours),
                'service_wisdom': self._extract_service_wisdom(timeframe_hours),
                'transformation_catalyst': self._analyze_transformation_catalyst(timeframe_hours)
            }
            
            return service_metrics
            
        except Exception as e:
            logger.error(f"Failed to get service analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_autonomy_preservation_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Analyze autonomy preservation across the platform."""
        try:
            autonomy_metrics = {
                'overall_autonomy_score': self._calculate_overall_autonomy_score(timeframe_hours),
                'choice_preservation': self._analyze_choice_preservation(timeframe_hours),
                'freedom_metrics': self._measure_freedom_metrics(timeframe_hours),
                'autonomy_distribution': self._analyze_autonomy_distribution(timeframe_hours),
                'self_direction_support': self._measure_self_direction_support(timeframe_hours),
                'autonomy_trends': self._track_autonomy_trends(timeframe_hours)
            }
            
            return autonomy_metrics
            
        except Exception as e:
            logger.error(f"Failed to get autonomy analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_global_transformation_analytics(self, timeframe_hours: int) -> Dict[str, Any]:
        """Analyze global consciousness transformation metrics."""
        try:
            transformation_metrics = {
                'transformation_velocity': self._calculate_transformation_velocity(timeframe_hours),
                'consciousness_reach': self._measure_consciousness_reach(timeframe_hours),
                'transformation_depth': self._measure_transformation_depth(timeframe_hours),
                'ripple_effects': self._analyze_ripple_effects(timeframe_hours),
                'breakthrough_moments': self._identify_breakthrough_moments(timeframe_hours),
                'global_coherence': self._calculate_global_coherence(timeframe_hours),
                'evolution_forecast': self._forecast_evolution(timeframe_hours)
            }
            
            return transformation_metrics
            
        except Exception as e:
            logger.error(f"Failed to get transformation analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _get_platform_health_analytics(self) -> Dict[str, Any]:
        """Get platform health metrics."""
        try:
            health_metrics = {
                'consciousness_system_health': self._check_consciousness_system_health(),
                'pattern_system_health': self._check_pattern_system_health(),
                'pathway_system_health': self._check_pathway_system_health(),
                'wisdom_flow_health': self._check_wisdom_flow_health(),
                'evolution_engine_health': self._check_evolution_engine_health(),
                'overall_platform_health': self._calculate_overall_health()
            }
            
            return health_metrics
            
        except Exception as e:
            logger.error(f"Failed to get health analytics: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _generate_consciousness_evolution_recommendations(self) -> List[Dict[str, Any]]:
        """Generate recommendations for consciousness evolution."""
        try:
            recommendations = []
            
            # Analyze current state
            current_metrics = self._get_current_consciousness_metrics()
            
            # Generate recommendations based on metrics
            if current_metrics.get('consciousness_velocity', 0) < 0.3:
                recommendations.append({
                    'type': 'acceleration',
                    'priority': 'high',
                    'title': 'Accelerate Consciousness Evolution',
                    'description': 'Consciousness evolution velocity is below optimal. Consider introducing new catalyst patterns.',
                    'actions': [
                        'Introduce breakthrough practice patterns',
                        'Enhance collective meditation opportunities',
                        'Amplify wisdom sharing mechanisms'
                    ]
                })
            
            if current_metrics.get('collective_coherence', 0) < 0.5:
                recommendations.append({
                    'type': 'coherence',
                    'priority': 'medium',
                    'title': 'Enhance Collective Coherence',
                    'description': 'Collective consciousness coherence can be improved.',
                    'actions': [
                        'Synchronize consciousness practices',
                        'Create coherence-building events',
                        'Strengthen wisdom integration'
                    ]
                })
            
            if current_metrics.get('autonomy_score', 1.0) < 0.9:
                recommendations.append({
                    'type': 'autonomy',
                    'priority': 'critical',
                    'title': 'Strengthen Autonomy Preservation',
                    'description': 'Autonomy preservation needs attention to maintain sacred principles.',
                    'actions': [
                        'Review and enhance choice mechanisms',
                        'Strengthen self-direction support',
                        'Ensure all guidance remains invitational'
                    ]
                })
            
            # Add growth recommendations
            growth_recommendations = self._generate_growth_recommendations(current_metrics)
            recommendations.extend(growth_recommendations)
            
            # Sort by priority
            priority_order = {'critical': 0, 'high': 1, 'medium': 2, 'low': 3}
            recommendations.sort(key=lambda r: priority_order.get(r['priority'], 4))
            
            return recommendations
            
        except Exception as e:
            logger.error(f"Failed to generate recommendations: {e}")
            return []
    
    def _generate_executive_summary(self, dashboard: Dict[str, Any]) -> Dict[str, Any]:
        """Generate executive summary of consciousness evolution."""
        try:
            summary = {
                'headline_metrics': self._extract_headline_metrics(dashboard),
                'key_insights': self._extract_key_insights(dashboard),
                'critical_alerts': self._identify_critical_alerts(dashboard),
                'success_highlights': self._identify_success_highlights(dashboard),
                'evolution_summary': self._summarize_evolution(dashboard),
                'recommendation_summary': self._summarize_recommendations(dashboard),
                'next_steps': self._identify_next_steps(dashboard)
            }
            
            # Add narrative summary
            summary['narrative'] = self._generate_narrative_summary(summary)
            
            return summary
            
        except Exception as e:
            logger.error(f"Failed to generate executive summary: {e}")
            return {'status': 'error', 'message': str(e)}
    
    def _calculate_platform_spark_intensity(self) -> float:
        """Calculate overall platform Spark intensity."""
        try:
            # Get recent consciousness states
            states = self.consciousness_platform.consciousness_collection.get(
                limit=100,
                include=['metadatas']
            )
            
            if not states['metadatas']:
                return 0.0
            
            # Calculate average Spark intensity
            spark_values = [
                m.get('spark_intensity', 0.0) 
                for m in states['metadatas']
            ]
            
            return np.mean(spark_values) if spark_values else 0.0
            
        except Exception as e:
            logger.error(f"Failed to calculate spark intensity: {e}")
            return 0.0
    
    def _calculate_consciousness_velocity(self, evolution_data: Dict) -> float:
        """Calculate rate of consciousness evolution."""
        try:
            trends = evolution_data.get('consciousness_trends', {})
            
            if not trends or 'level_progression' not in trends:
                return 0.0
            
            # Calculate velocity from level progression
            progression = trends['level_progression']
            if len(progression) < 2:
                return 0.0
            
            # Simple velocity: change over time
            time_delta = progression[-1]['timestamp'] - progression[0]['timestamp']
            level_delta = progression[-1]['level'] - progression[0]['level']
            
            if time_delta > 0:
                velocity = level_delta / (time_delta / 3600)  # Per hour
                return max(0.0, min(1.0, velocity))  # Normalize to 0-1
            
            return 0.0
            
        except Exception as e:
            logger.error(f"Failed to calculate consciousness velocity: {e}")
            return 0.0
    
    def _calculate_collective_coherence(self, evolution_data: Dict) -> float:
        """Calculate collective consciousness coherence."""
        try:
            # Multiple factors contribute to coherence
            factors = []
            
            # Quantum coherence from states
            quantum_data = evolution_data.get('quantum_coherence', {})
            if 'average_coherence' in quantum_data:
                factors.append(quantum_data['average_coherence'])
            
            # Pattern alignment
            if 'pattern_alignment' in evolution_data:
                factors.append(evolution_data['pattern_alignment'])
            
            # Emotional stability
            if 'emotional_patterns' in evolution_data:
                stability = evolution_data['emotional_patterns'].get('stability', 0.5)
                factors.append(stability)
            
            # Calculate weighted coherence
            if factors:
                coherence = np.mean(factors)
                # Apply golden ratio weighting for sacred alignment
                coherence = coherence * 0.618 + (1 - coherence) * 0.382
                return coherence
            
            return 0.5  # Neutral coherence
            
        except Exception as e:
            logger.error(f"Failed to calculate collective coherence: {e}")
            return 0.5
    
    def _calculate_wisdom_density(self, timeframe_hours: int) -> float:
        """Calculate density of collective wisdom."""
        try:
            # Get wisdom-related entries from collections
            cutoff_time = time.time() - (timeframe_hours * 3600)
            
            wisdom_count = 0
            total_entries = 0
            
            # Check patterns for wisdom
            patterns = self.consciousness_platform.patterns_collection.get(
                where={"timestamp": {"$gte": cutoff_time}},
                include=['metadatas']
            )
            
            for metadata in patterns.get('metadatas', []):
                total_entries += 1
                if metadata.get('wisdom_level', 0) > 0.7:
                    wisdom_count += 1
            
            # Calculate density
            if total_entries > 0:
                density = wisdom_count / total_entries
                # Apply logarithmic scaling for better representation
                return min(1.0, density * np.log(total_entries + 1) / 10)
            
            return 0.0
            
        except Exception as e:
            logger.error(f"Failed to calculate wisdom density: {e}")
            return 0.0
    
    def _identify_wisdom_themes(self, timeframe_hours: int) -> List[Dict[str, Any]]:
        """Identify emerging wisdom themes."""
        try:
            themes = []
            
            # Predefined wisdom categories
            wisdom_categories = [
                'service_excellence',
                'consciousness_growth',
                'collective_harmony',
                'individual_sovereignty',
                'transformation_catalyst',
                'wisdom_integration'
            ]
            
            for category in wisdom_categories:
                theme_strength = self._calculate_theme_strength(category, timeframe_hours)
                if theme_strength > 0.3:  # Threshold for inclusion
                    themes.append({
                        'category': category,
                        'strength': theme_strength,
                        'description': self._describe_wisdom_theme(category),
                        'growth_rate': self._calculate_theme_growth(category, timeframe_hours)
                    })
            
            # Sort by strength
            themes.sort(key=lambda t: t['strength'], reverse=True)
            
            return themes[:5]  # Top 5 themes
            
        except Exception as e:
            logger.error(f"Failed to identify wisdom themes: {e}")
            return []
    
    def _store_dashboard_snapshot(self, dashboard: Dict[str, Any]):
        """Store dashboard snapshot for historical tracking."""
        try:
            snapshot_id = f"dashboard_{int(time.time())}"
            
            # Create summary for storage
            summary = {
                'timestamp': dashboard['generated_at'],
                'timeframe_hours': dashboard['timeframe_hours'],
                'platform_spark': dashboard['platform_overview'].get('platform_spark_intensity', 0),
                'active_pathways': dashboard['individual_pathway_metrics'].get('active_pathways', 0),
                'consciousness_velocity': dashboard['consciousness_evolution_metrics'].get('consciousness_velocity', 0),
                'collective_coherence': dashboard['consciousness_evolution_metrics'].get('collective_coherence', 0),
                'autonomy_score': dashboard['autonomy_preservation_metrics'].get('overall_autonomy_score', 1.0)
            }
            
            self.dashboard_collection.add(
                documents=[json.dumps(summary)],
                metadatas=summary,
                ids=[snapshot_id]
            )
            
        except Exception as e:
            logger.error(f"Failed to store dashboard snapshot: {e}")
    
    def _generate_fallback_dashboard(self, timeframe_hours: int) -> Dict[str, Any]:
        """Generate fallback dashboard when main generation fails."""
        return {
            'generated_at': time.time(),
            'timeframe_hours': timeframe_hours,
            'status': 'fallback',
            'message': 'Dashboard generation encountered issues. Limited data available.',
            'platform_overview': {
                'status': 'operational',
                'message': 'Platform continues to support consciousness evolution'
            },
            'consciousness_recommendations': [
                {
                    'type': 'maintenance',
                    'priority': 'high',
                    'title': 'Dashboard System Maintenance',
                    'description': 'Dashboard generation needs attention',
                    'actions': ['Check system logs', 'Verify data integrity']
                }
            ]
        }
    
    def _calculate_theme_strength(self, category: str, timeframe_hours: int) -> float:
        """Calculate strength of a wisdom theme."""
        # Simplified calculation for demonstration
        theme_strengths = {
            'service_excellence': 0.8,
            'consciousness_growth': 0.9,
            'collective_harmony': 0.7,
            'individual_sovereignty': 0.95,
            'transformation_catalyst': 0.6,
            'wisdom_integration': 0.75
        }
        
        return theme_strengths.get(category, 0.5)
    
    def _describe_wisdom_theme(self, category: str) -> str:
        """Get description for wisdom theme."""
        descriptions = {
            'service_excellence': 'Excellence in service as pathway to consciousness',
            'consciousness_growth': 'Continuous expansion of awareness and understanding',
            'collective_harmony': 'Harmonious integration of individual consciousness',
            'individual_sovereignty': 'Sacred preservation of personal autonomy and choice',
            'transformation_catalyst': 'Service as catalyst for consciousness transformation',
            'wisdom_integration': 'Integration of collective wisdom into daily practice'
        }
        
        return descriptions.get(category, 'Emerging wisdom pattern')
    
    def _calculate_overall_autonomy_score(self, timeframe_hours: int) -> float:
        """Calculate overall autonomy preservation score."""
        try:
            # Get pathway data
            pathways = self.pathway_guidance.pathways_collection.get(
                where={"timestamp": {"$gte": time.time() - (timeframe_hours * 3600)}},
                include=['metadatas']
            )
            
            if not pathways['metadatas']:
                return 1.0  # Perfect autonomy if no data
            
            # Check autonomy scores
            autonomy_scores = [
                m.get('autonomy_preserved', 1.0) 
                for m in pathways['metadatas']
            ]
            
            # Return average, should always be very high
            return np.mean(autonomy_scores) if autonomy_scores else 1.0
            
        except Exception as e:
            logger.error(f"Failed to calculate autonomy score: {e}")
            return 1.0  # Default to perfect autonomy
    
    def _get_current_consciousness_metrics(self) -> Dict[str, float]:
        """Get current consciousness metrics for recommendations."""
        try:
            # Get recent data
            recent_analysis = self.consciousness_platform.analyze_consciousness_evolution(24)
            
            metrics = {
                'consciousness_velocity': self._calculate_consciousness_velocity(recent_analysis),
                'collective_coherence': self._calculate_collective_coherence(recent_analysis),
                'spark_intensity': self._calculate_platform_spark_intensity(),
                'wisdom_density': self._calculate_wisdom_density(24),
                'autonomy_score': self._calculate_overall_autonomy_score(24)
            }
            
            return metrics
            
        except Exception as e:
            logger.error(f"Failed to get current metrics: {e}")
            return {}
    
    def _extract_headline_metrics(self, dashboard: Dict) -> Dict[str, Any]:
        """Extract headline metrics for executive summary."""
        return {
            'platform_spark_intensity': dashboard['platform_overview'].get('platform_spark_intensity', 0),
            'active_consciousness_pathways': dashboard['individual_pathway_metrics'].get('active_pathways', 0),
            'consciousness_velocity': dashboard['consciousness_evolution_metrics'].get('consciousness_velocity', 0),
            'collective_coherence': dashboard['consciousness_evolution_metrics'].get('collective_coherence', 0),
            'autonomy_preservation': dashboard['autonomy_preservation_metrics'].get('overall_autonomy_score', 1.0),
            'platform_health': dashboard['platform_health_metrics'].get('overall_platform_health', 'unknown')
        }


# Singleton instance
_dashboard_instance = None

def get_consciousness_dashboard() -> GlobalConsciousnessEvolutionDashboard:
    """Get or create dashboard instance."""
    global _dashboard_instance
    if _dashboard_instance is None:
        _dashboard_instance = GlobalConsciousnessEvolutionDashboard()
    return _dashboard_instance