#!/usr/bin/env python3
"""
Script pour lire et afficher le contenu des fichiers .pkl (signatures).
"""

import sys
import pickle
import json
from pathlib import Path
import numpy as np


def read_pkl_file(pkl_path: Path):
    """Lit et affiche le contenu d'un fichier .pkl."""
    print(f"\n{'='*60}")
    print(f"📄 Fichier: {pkl_path.name}")
    print(f"{'='*60}")
    
    try:
        with open(pkl_path, 'rb') as f:
            data = pickle.load(f)
        
        if isinstance(data, list):
            print(f"\n📊 Type: Liste de {len(data)} éléments\n")
            
            for i, item in enumerate(data, 1):
                print(f"--- Action {i} ---")
                
                if isinstance(item, dict):
                    for key, value in item.items():
                        # Afficher différemment selon le type
                        if key == 'embedding' and isinstance(value, np.ndarray):
                            print(f"  {key}: numpy array shape {value.shape}, dtype {value.dtype}")
                            print(f"    Premiers éléments: {value[:5]}")
                        elif key == 'screenshot' and value is not None:
                            if isinstance(value, np.ndarray):
                                print(f"  {key}: numpy array shape {value.shape}")
                            else:
                                print(f"  {key}: {type(value).__name__}")
                        elif key == 'bbox' and isinstance(value, (list, tuple)):
                            print(f"  {key}: {value}")
                        elif isinstance(value, (str, int, float, bool)):
                            print(f"  {key}: {value}")
                        elif value is None:
                            print(f"  {key}: None")
                        else:
                            print(f"  {key}: {type(value).__name__}")
                else:
                    print(f"  {item}")
                
                print()
        
        elif isinstance(data, dict):
            print(f"\n📊 Type: Dictionnaire avec {len(data)} clés\n")
            
            for key, value in data.items():
                if isinstance(value, np.ndarray):
                    print(f"  {key}: numpy array shape {value.shape}")
                elif isinstance(value, (list, dict)):
                    print(f"  {key}: {type(value).__name__} ({len(value)} éléments)")
                else:
                    print(f"  {key}: {value}")
        
        else:
            print(f"\n📊 Type: {type(data).__name__}")
            print(f"\n{data}")
    
    except Exception as e:
        print(f"\n❌ Erreur lors de la lecture: {e}")
        import traceback
        traceback.print_exc()


def main():
    """Fonction principale."""
    if len(sys.argv) < 2:
        print("Usage: python3 read_pkl.py <fichier.pkl>")
        print("\nOu pour lire une tâche spécifique:")
        print("python3 read_pkl.py task_fc1d3e52")
        print("\nOu pour lister toutes les tâches:")
        print("python3 read_pkl.py --list")
        return
    
    arg = sys.argv[1]
    
    if arg == "--list":
        # Lister toutes les tâches
        profiles_dir = Path("geniusia2/data/user_profiles")
        task_dirs = sorted([d for d in profiles_dir.iterdir() if d.is_dir() and d.name.startswith("task_")])
        
        print(f"\n📋 Tâches disponibles ({len(task_dirs)}):\n")
        
        for i, task_dir in enumerate(task_dirs, 1):
            metadata_file = task_dir / "metadata.json"
            signatures_file = task_dir / "signatures.pkl"
            
            if metadata_file.exists():
                with open(metadata_file, 'r') as f:
                    metadata = json.load(f)
                task_name = metadata.get('task_name', 'N/A')
                obs_count = metadata.get('observation_count', 0)
            else:
                task_name = "N/A"
                obs_count = 0
            
            has_sig = "✅" if signatures_file.exists() else "❌"
            
            print(f"{i:3d}. {task_dir.name}")
            print(f"     Nom: {task_name}")
            print(f"     Observations: {obs_count}")
            print(f"     Signatures: {has_sig}")
            print()
        
        return
    
    # Déterminer le chemin du fichier
    if arg.startswith("task_"):
        # C'est un ID de tâche
        pkl_path = Path(f"geniusia2/data/user_profiles/{arg}/signatures.pkl")
        
        # Afficher aussi les métadonnées
        metadata_path = Path(f"geniusia2/data/user_profiles/{arg}/metadata.json")
        if metadata_path.exists():
            print(f"\n{'='*60}")
            print(f"📋 Métadonnées de {arg}")
            print(f"{'='*60}\n")
            
            with open(metadata_path, 'r') as f:
                metadata = json.load(f)
            
            print(json.dumps(metadata, indent=2, ensure_ascii=False))
    else:
        # C'est un chemin de fichier
        pkl_path = Path(arg)
    
    if not pkl_path.exists():
        print(f"❌ Fichier non trouvé: {pkl_path}")
        return
    
    read_pkl_file(pkl_path)


if __name__ == "__main__":
    main()
