#!/usr/bin/env python3
"""
Script de nettoyage intelligent du projet
=========================================

Analyse et déplace les fichiers obsolètes vers un dossier archive/
"""

import os
import shutil
from datetime import datetime
from pathlib import Path

SCRIPT_DIR = Path(__file__).parent

# Catégories de fichiers à archiver
OBSOLETE_FILES = {
    # === SCRIPTS DE TEST OBSOLÈTES ===
    'tests_obsoletes': [
        'test_*.py',  # Tous les tests seront déplacés dans tests/
        'quick_test.py',
        'minimal_test.html',
        'test_debug.html',
        'test_loading.html',
        'refresh.html',
        'temp_*.json',
        'temp_*.js',
    ],
    
    # === SCRIPTS D'ANALYSE/DEBUG OBSOLÈTES ===
    'analyze_obsolete': [
        'analyze_6h.py',
        'analyze_balance_history.py',
        'analyze_bearish_context.py',
        'analyze_improvements.py',
        'analyze_js.py',
        'analyze_performance.py',
        'analyze_positions_config.py',
        'analyze_quality.py',
        'analyze_recent_trades.py',
        'analyze_signals.py',
        'analyze_signals_log.py',
        'analyze_trades.py',
        'analyze_trade_logs.py',
        'analyze_trading_performance.py',
        'audit_code.py',
    ],
    
    # === SCRIPTS DE FIX TEMPORAIRES ===
    'fix_scripts': [
        'fix_binance_sync.py',
        'fix_cache_production.py',
        'fix_chars.py',
        'fix_dashboard.py',
        'fix_encoding.py',
        'fix_positions.py',
        'apply_enhancements.py',
    ],
    
    # === SCRIPTS DE VENTE MANUELLE ===
    'manual_sells': [
        'sell_dot.py',
        'sell_ltc.py',
        'sell_zec.py',
        'final_sell.py',
        'force_sell_all.py',
        'force_sell_remaining.py',
        'quick_sell_all.py',
        'sell_all_binance.py',
        'sell_all_positions.py',
    ],
    
    # === SCRIPTS DE DEBUG/DIAGNOSTIC ===
    'debug_scripts': [
        'debug_ia_ready.py',
        'debug_rotation.py',
        'debug_traceback.py',
        'diagnose_no_signals.py',
        'diagnose_treasury_chart.py',
        'diagnostic_selection.py',
        'diag_ia_coherence.py',
        'diag_rotation.py',
        'diag_surveillance.py',
        'diag_sync.py',
        'trace_ia_bot.py',
    ],
    
    # === SCRIPTS DE CHECK REDONDANTS ===
    'check_scripts': [
        'check_balance_simple.py',
        'check_balance_testnet.py',
        'check_binance_balances.py',
        'check_bot_buys.py',
        'check_bot_state.py',
        'check_bot_status.py',
        'check_coherence.py',
        'check_ia_status.py',
        'check_modules_status.py',
        'check_positions_binance.py',
        'check_signals.py',
        'check_sl_tp.py',
        'check_status_ascii.py',
        'check_test_symbol.py',
        'quick_check.py',
    ],
    
    # === SCRIPTS DE DÉMARRAGE OBSOLÈTES ===
    'old_starters': [
        'launch_all.py',
        'restart_dashboard.py',
        'start_all.py',
        'start_dashboard.py',
        'stop_all.py',
        'Reset_trading_old.py',
    ],
    
    # === SCRIPTS DE SYNCHRONISATION ===
    'sync_scripts': [
        'explain_sync.py',
        'sync_binance.py',
        'sync_binance_fix.py',
        'sync_positions.py',
        'fix_binance_sync.py',
    ],
    
    # === SCRIPTS DE DÉMONSTRATION ===
    'demo_scripts': [
        'demo_modules_ia.py',
        'test_integration_demo.py',
    ],
    
    # === AUTRES SCRIPTS OBSOLÈTES ===
    'misc_obsolete': [
        'auto_cleanup.py',
        'clean_positions.py',
        'close_positions_manual.py',
        'convert_to_usdt.py',
        'deep_clean.py',
        'force_analysis_update.py',
        'force_cache.py',
        'force_disable_crash_mode.py',
        'init_volatility_scores.py',
        'maintenance_positions.py',
        'monitor_achats.py',
        'monitor_btc.py',
        'rapport_tracabilite.py',
        'reset_complete.py',
        'run_backtest_comparison.py',
        'show_balances.py',
        'show_exclusions.py',
        'validate_positions.py',
        'crypto_monitor.py',
    ],
    
    # === DASHBOARDS OBSOLÈTES ===
    'old_dashboards': [
        'dashboard_backup_broken.html',
        'dashboard_corrupted.html',
        'dashboard_server.py',  # Remplacé par dashboard_api_server.py
    ],
    
    # === FICHIERS DE LOG/BACKUP ===
    'logs_backups': [
        '*.log',
        '*.txt',
        '*_backup_*.json',
        'bot_err.log',
        'bot_error.log',
        'bot_out.log',
        'bot_output.log',
        'dashboard_err.log',
        'dashboard_error.log',
        'dashboard_out.log',
        'dash_err.log',
        'dash_out.log',
        'debug_calculate_next.txt',
        'status_output.txt',
        'test_output.txt',
        'test_status_output.txt',
    ],
}

# Fichiers à GARDER (fichiers core du système)
KEEP_FILES = {
    # Core bot
    'trading_bot.py',
    'config.py',
    
    # Services principaux
    'dashboard_api_server.py',
    'bot_watchdog.py',
    'auto_updater_service.py',
    
    # Modules IA
    'ai_predictor.py',
    'ai_advanced_scorer.py',
    'ai_opportunity_selector.py',
    'ai_realtime_service.py',
    'ai_self_optimizer.py',
    
    # Analyseurs
    'advanced_feature_engineering.py',
    'advanced_strategies.py',
    'correlation_analyzer.py',
    'feature_engineering.py',
    'long_term_trend_analyzer.py',
    'market_regime_detector.py',
    'monte_carlo_simulator.py',
    'multi_timeframe_analyzer.py',
    'performance_analyzer.py',
    'risk_adjusted_scorer.py',
    'time_pattern_analyzer.py',
    'volatility_scorer.py',
    'volume_profile_analyzer.py',
    'technical_analyzer.py',
    
    # Strategies
    'dynamic_blacklist.py',
    'dynamic_sltp.py',
    'ensemble_predictor.py',
    'enhanced_signal_engine.py',
    'market_safety.py',
    'smart_entry_criteria.py',
    'smart_rotation.py',
    
    # Utilities
    'crypto_data_fetcher.py',
    'fetch_historical_data.py',
    'historical_data_updater.py',
    'telegram_notifier.py',
    'trade_logger.py',
    'train_ai_model.py',
    
    # Scripts principaux
    'Reset_trading.py',
    'sell_all.py',
    'check_balance.py',
    'check_positions.py',
    'backtesting.py',
    
    # Cleanup
    'cleanup_obsolete_files.py',
}

def create_archive_structure():
    """Créer la structure d'archivage"""
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    archive_dir = SCRIPT_DIR / f"archive_{timestamp}"
    
    for category in OBSOLETE_FILES.keys():
        (archive_dir / category).mkdir(parents=True, exist_ok=True)
    
    return archive_dir

def move_to_archive(file_path, category, archive_dir):
    """Déplacer un fichier vers l'archive"""
    dest = archive_dir / category / file_path.name
    try:
        shutil.move(str(file_path), str(dest))
        return True
    except Exception as e:
        print(f"❌ Erreur lors du déplacement de {file_path.name}: {e}")
        return False

def scan_and_archive():
    """Scanner et archiver les fichiers obsolètes"""
    print("=" * 80)
    print("🧹 NETTOYAGE INTELLIGENT DU PROJET")
    print("=" * 80)
    print()
    
    archive_dir = create_archive_structure()
    print(f"📁 Archive créée: {archive_dir.name}")
    print()
    
    stats = {category: 0 for category in OBSOLETE_FILES.keys()}
    
    # Parcourir chaque catégorie
    for category, patterns in OBSOLETE_FILES.items():
        print(f"🔍 Traitement: {category}")
        
        for pattern in patterns:
            if '*' in pattern:
                # Pattern avec wildcard
                base_pattern = pattern.replace('*', '')
                for file in SCRIPT_DIR.glob(pattern):
                    if file.is_file() and file.name not in KEEP_FILES:
                        if move_to_archive(file, category, archive_dir):
                            stats[category] += 1
                            print(f"  ✅ {file.name}")
            else:
                # Fichier spécifique
                file_path = SCRIPT_DIR / pattern
                if file_path.exists() and file_path.is_file():
                    if move_to_archive(file_path, category, archive_dir):
                        stats[category] += 1
                        print(f"  ✅ {pattern}")
        
        print()
    
    # Résumé
    print("=" * 80)
    print("📊 RÉSUMÉ DU NETTOYAGE")
    print("=" * 80)
    total_moved = sum(stats.values())
    
    for category, count in stats.items():
        if count > 0:
            print(f"  {category:30s}: {count:3d} fichiers")
    
    print(f"\n  TOTAL: {total_moved} fichiers archivés")
    print(f"\n  📁 Archive: {archive_dir}")
    print()
    
    # Compter les fichiers restants
    remaining_py = len(list(SCRIPT_DIR.glob('*.py')))
    print(f"  📄 Fichiers Python restants: {remaining_py}")
    print()
    print("=" * 80)
    print("✅ Nettoyage terminé !")
    print("=" * 80)

if __name__ == "__main__":
    response = input("\n⚠️  Ce script va archiver les fichiers obsolètes.\nContinuer ? (O/N): ")
    if response.upper() == 'O':
        scan_and_archive()
    else:
        print("❌ Opération annulée")
