#!/usr/bin/env python3
"""
Analyser pourquoi les shitcoins ont de petits leverages
"""

import sys
import logging
from hyperliquid_leverages_cache import get_max_leverage
from trading_config import HIGH_QUALITY_CRYPTOS, MEDIUM_QUALITY_CRYPTOS

# Configuration du logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def analyze_shitcoin_leverages():
    """Analyser les leverages des positions actuelles"""

    positions_actuelles = [
        {'symbol': 'W', 'leverage': 5, 'side': 'SHORT'},
        {'symbol': 'PENGU', 'leverage': 2, 'side': 'LONG'},
        {'symbol': 'S', 'leverage': 2, 'side': 'LONG'},
        {'symbol': 'PROMPT', 'leverage': 5, 'side': 'SHORT'},
        {'symbol': 'ZORA', 'leverage': 3, 'side': 'LONG'},
        {'symbol': 'LAUNCHCOIN', 'leverage': 3, 'side': 'LONG'},
        {'symbol': 'WLFI', 'leverage': 2, 'side': 'LONG'},
        {'symbol': 'LINEA', 'leverage': 2, 'side': 'LONG'},
    ]

    print("=" * 80)
    print("ANALYSE DES LEVERAGES SHITCOINS")
    print("=" * 80)

    def get_quality(symbol):
        clean_symbol = symbol.replace('USDT', '').upper()
        if clean_symbol in HIGH_QUALITY_CRYPTOS:
            return 'HIGH'
        elif clean_symbol in MEDIUM_QUALITY_CRYPTOS:
            return 'MEDIUM'
        else:
            return 'SHITCOIN'

    print("Symbol | Side | Leverage | Max HL | Quality | Problème ?")
    print("-" * 65)

    shitcoins_long = []
    leverages_faibles = []

    for pos in positions_actuelles:
        symbol = pos['symbol']
        leverage = pos['leverage']
        side = pos['side']

        max_hl = get_max_leverage(symbol)
        quality = get_quality(symbol)

        problemes = []

        # Identifier les problèmes
        if quality == 'SHITCOIN' and side == 'LONG':
            problemes.append("SHITCOIN_LONG")
            shitcoins_long.append(symbol)

        if leverage < 5 and max_hl >= 10:
            problemes.append("LEVERAGE_FAIBLE")
            leverages_faibles.append(symbol)

        probleme_str = ", ".join(problemes) if problemes else "OK"

        print(f"{symbol:6} | {side:4} | {leverage:8}x | {max_hl:6}x | {quality:8} | {probleme_str}")

    print("\n" + "=" * 80)
    print("RÉSUMÉ DES PROBLÈMES")
    print("=" * 80)

    print(f"\n🔻 SHITCOINS en LONG (devraient être SHORT): {len(shitcoins_long)}")
    for symbol in shitcoins_long:
        print(f"  - {symbol}")

    print(f"\n📉 LEVERAGES FAIBLES (< 5x alors que max > 10x): {len(leverages_faibles)}")
    for symbol in leverages_faibles:
        max_hl = get_max_leverage(symbol)
        current_pos = next(p for p in positions_actuelles if p['symbol'] == symbol)
        potential = min(max_hl * 0.8, 20)  # Potentiel réaliste
        print(f"  - {symbol}: {current_pos['leverage']}x → potentiel {potential:.0f}x")

    print(f"\n🎯 SOLUTIONS:")
    print("1. Positions shitcoin LONG → attendre fermeture naturelle ou forcer SHORT")
    print("2. Leverages faibles → corrélation BTC trop agressive ou calculs bridés")
    print("3. Nouvelles positions → biais SHORT doit fonctionner")

    print("\n" + "=" * 80)
    print("RECOMMANDATIONS")
    print("=" * 80)
    print("- Surveiller que les NOUVEAUX signaux respectent le biais SHORT")
    print("- Ajuster la corrélation BTC pour permettre des leverages plus élevés")
    print("- Considérer fermer manuellement PENGU, ZORA, LAUNCHCOIN (shitcoins LONG)")

if __name__ == "__main__":
    analyze_shitcoin_leverages()