#!/usr/bin/env python3
"""
Script pour faire table rase des historiques et repartir de zéro
pour tester l'efficacité des nouveaux patterns
"""

import os
import shutil
from datetime import datetime

# Fichiers à archiver et vider
FILES_TO_CLEAN = [
    "trade_logs/trades_log.jsonl",
    "trade_logs/signals_log.jsonl",
    "ai_opportunities.json",
    "ai_training_stats.json",
    "bot_analysis.json",
    "trade_history.json",
    "performance_stats.json",
    "ia_surveillance_cache.json"
]

def clean_all_history():
    """Archive tous les historiques et repart de zéro"""
    
    # Créer le dossier d'archives
    archive_dir = "trade_logs/archives_reset"
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    archive_path = os.path.join(archive_dir, f"reset_{timestamp}")
    os.makedirs(archive_path, exist_ok=True)
    
    print("=" * 70)
    print("🗑️  NETTOYAGE COMPLET DES HISTORIQUES")
    print("=" * 70)
    print(f"\n📦 Création archive: {archive_path}\n")
    
    cleaned_count = 0
    
    for file_path in FILES_TO_CLEAN:
        if os.path.exists(file_path):
            # Archiver le fichier
            archive_file = os.path.join(archive_path, os.path.basename(file_path))
            shutil.copy2(file_path, archive_file)
            print(f"✅ Archivé: {file_path} -> {archive_file}")
            
            # Vider le fichier
            if file_path.endswith('.jsonl'):
                # Pour les fichiers JSONL, créer un fichier vide
                with open(file_path, 'w', encoding='utf-8') as f:
                    pass
            elif file_path.endswith('.json'):
                # Pour les fichiers JSON, mettre un objet vide ou array vide
                with open(file_path, 'w', encoding='utf-8') as f:
                    if 'opportunities' in file_path:
                        f.write('[]')
                    else:
                        f.write('{}')
            
            print(f"🧹 Vidé: {file_path}")
            cleaned_count += 1
        else:
            print(f"⚠️  Non trouvé: {file_path}")
    
    print("\n" + "=" * 70)
    print(f"✅ NETTOYAGE TERMINÉ !")
    print(f"   - {cleaned_count} fichiers archivés et vidés")
    print(f"   - Archive sauvegardée dans: {archive_path}")
    print("=" * 70)
    print("\n🎯 Vous pouvez maintenant relancer le bot pour tester les nouveaux patterns")
    print("   Les performances seront mesurées à partir de zéro.\n")

if __name__ == '__main__':
    print("\n⚠️  ATTENTION: Cette opération va:")
    print("   1. Archiver tous les historiques de trades et signaux")
    print("   2. Vider complètement ces fichiers")
    print("   3. Réinitialiser les stats IA")
    print("\n   Vous repartez de ZÉRO pour tester les nouveaux patterns.\n")
    
    response = input("❓ Confirmer le nettoyage complet? [o/N]: ")
    
    if response.lower() in ['o', 'oui', 'y', 'yes']:
        clean_all_history()
    else:
        print("\n❌ Opération annulée")
