"""
Test du système de stop loss du ProfitTaker
"""

import sys
import os
sys.path.append('..')

from .profit_taker import ProfitTaker
from hyperliquid_client import HyperliquidClient
import time

# Fix encoding for Windows console
sys.stdout.reconfigure(encoding='utf-8')

def test_stop_loss():
    """Test du stop loss en mode simulation"""

    print("=== TEST STOP LOSS PROFIT TAKER ===")

    # Initialiser le client Hyperliquid
    try:
        hyperliquid_client = HyperliquidClient()
        print("Client Hyperliquid initialise")
    except Exception as e:
        print(f"Erreur client Hyperliquid: {e}")
        return

    # Initialiser le ProfitTaker
    profit_taker = ProfitTaker(hyperliquid_client=hyperliquid_client)
    print("ProfitTaker initialise")

    # Mettre à jour les positions une fois
    profit_taker.update_active_positions()

    # Afficher les positions actuelles
    print("\n--- Positions actuelles ---")
    with profit_taker.lock:
        positions = profit_taker.active_positions

        if positions:
            for key, pos in positions.items():
                symbol = pos['symbol']
                side = pos['side']
                pnl = pos['pnl_percent']
                peak = pos['peak_pnl_percent']
                print(f"  {symbol} {side}: PnL={pnl:.2f}%, Peak={peak:.2f}%")

                # Simuler une condition de stop loss pour test
                if pnl <= -12.0:
                    print(f"    🚨 STOP LOSS CONDITION DETECTED: {symbol}")
        else:
            print("  Aucune position active")

    # Test de la fonction de vérification stop loss
    print("\n--- Test vérification stop loss ---")
    profit_taker.check_stop_loss_conditions()

    print("\n=== TEST TERMINÉ ===")

if __name__ == "__main__":
    test_stop_loss()