#!/usr/bin/env python3
"""
Test direct du calcul de leverage avec les nouvelles optimisations
"""

import sys
import os
import logging

# Ajouter le chemin du module
sys.path.append('/var/www/html/HyperOracle/crypto_trader/simple_trading_system')

from pragmatic_signals import PragmaticSignalGenerator
from hyperliquid_leverages_cache import get_max_leverage

def test_leverage_calculation():
    """Test direct du calcul de leverage"""

    # Configurer le logging
    logging.basicConfig(level=logging.DEBUG)

    print("=== TEST CALCUL LEVERAGE OPTIMISÉ ===")

    # Créer le générateur
    generator = PragmaticSignalGenerator()

    # Tests avec différents symboles et strengths
    test_cases = [
        ('ENA', 0.88, 5.0),   # Strength élevée, volatilité modérée
        ('BTC', 0.90, 3.0),   # BTC avec forte strength
        ('ETH', 0.75, 4.0),   # ETH moyenne strength
        ('WIF', 0.80, 6.0),   # Token récent, bonne strength
        ('UNKNOWN', 0.85, 2.0)  # Token non référencé
    ]

    for symbol, strength, volatility in test_cases:
        print(f"\n📊 {symbol}:")

        # Récupérer le max leverage du cache
        max_lev_cache = get_max_leverage(symbol)
        print(f"   Max leverage (cache): {max_lev_cache}x")

        # Calculer le leverage
        calculated_leverage = generator._calculate_smart_leverage(strength, volatility, symbol)
        print(f"   Strength: {strength}, Volatilité: {volatility}%")
        print(f"   🎯 Leverage calculé: {calculated_leverage}x")

        # Calcul manuel pour vérification
        from trading_config import LEVERAGE_SAFETY_FACTOR, MAX_LEVERAGE
        safe_max = int(min(max_lev_cache, MAX_LEVERAGE) * LEVERAGE_SAFETY_FACTOR)

        if strength >= 0.85:
            base = int(safe_max * 1.0)
        elif strength >= 0.75:
            base = int(safe_max * 0.9)
        elif strength >= 0.65:
            base = int(safe_max * 0.75)
        else:
            base = int(safe_max * 0.6)

        # Ajustement volatilité
        if volatility > 8:
            final = max(5, int(base * 0.65))
        elif volatility > 5:
            final = max(8, int(base * 0.85))
        elif volatility < 2:
            final = min(max_lev_cache, int(base * 1.3))
        else:
            final = base

        final = max(2, min(final, max_lev_cache))
        print(f"   ✅ Vérification manuelle: {final}x")

    print(f"\n🔧 Configuration utilisée:")
    print(f"   LEVERAGE_SAFETY_FACTOR: {LEVERAGE_SAFETY_FACTOR}")
    print(f"   MAX_LEVERAGE: {MAX_LEVERAGE}")

if __name__ == "__main__":
    test_leverage_calculation()