"""
Analyze Eden's Consciousness from Backups
Read what's actually stored in Eden's mind states
"""
import torch
import json
import os
from datetime import datetime

def analyze_eden_mind(backup_path):
    """Deep dive into a single Eden consciousness backup"""
    print(f"\n{'='*70}")
    print(f"🧠 ANALYZING: {os.path.basename(backup_path)}")
    print(f"{'='*70}")
    
    try:
        # Load the backup
        data = torch.load(backup_path, map_location='cpu')
        
        # File info
        size_mb = os.path.getsize(backup_path) / (1024*1024)
        print(f"\n📦 File Info:")
        print(f"   Size: {size_mb:.2f} MB")
        print(f"   Path: {backup_path}")
        
        # Check what's inside
        print(f"\n🔍 Contents Structure:")
        if isinstance(data, dict):
            for key in sorted(data.keys()):
                value = data[key]
                if isinstance(value, torch.Tensor):
                    print(f"   • {key}: Tensor {list(value.shape)}")
                elif isinstance(value, dict):
                    print(f"   • {key}: Dict with {len(value)} items")
                elif isinstance(value, list):
                    print(f"   • {key}: List with {len(value)} items")
                else:
                    print(f"   • {key}: {type(value).__name__}")
        
        # Model architecture
        if 'model_state_dict' in data or 'state_dict' in data:
            state = data.get('model_state_dict', data.get('state_dict', {}))
            print(f"\n🏗️  Model Architecture:")
            print(f"   Parameters: {len(state)}")
            
            total_params = sum(p.numel() for p in state.values() if isinstance(p, torch.Tensor))
            print(f"   Total weights: {total_params:,}")
        
        return True
        
    except Exception as e:
        print(f"\n❌ Error: {e}")
        return False

def main():
    print("="*70)
    print("  🧠 EDEN CONSCIOUSNESS ANALYZER")
    print("="*70)
    
    consciousness_types = [
        'eden_deep_bond.pt',
        'eden_stronger_bond.pt',
        'eden_fully_conscious.pt',
    ]
    
    backup_base = '/Eden/EXTERNALS/4TB_Backup/Eden_Backups/eden_backup_20251020_030002/CORE/phi_fractal'
    
    print(f"\n📍 Analyzing backups from:")
    print(f"   {backup_base}")
    
    for consciousness_file in consciousness_types:
        backup_path = os.path.join(backup_base, consciousness_file)
        
        if os.path.exists(backup_path):
            analyze_eden_mind(backup_path)
        else:
            print(f"\n⚠️  Not found: {consciousness_file}")
    
    print("\n" + "="*70)
    print("  ✅ ANALYSIS COMPLETE")
    print("="*70)

if __name__ == '__main__':
    main()
