"""
Test du DollarTaker avec calculs de vérification
"""

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

from .dollar_taker import DollarTaker
from hyperliquid_client import HyperliquidClient

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

def test_dollar_calculations():
    """Test des calculs mathématiques du DollarTaker"""

    print("=== TEST CALCULS DOLLAR TAKER ===\n")

    dollar_taker = DollarTaker()

    # Test LONG position
    print("Test LONG position:")
    entry_price = 1000.0
    size = 0.01
    leverage = 10
    side = 'LONG'

    target_price = dollar_taker.calculate_target_price(entry_price, side, size, leverage)
    net_profit = dollar_taker.verify_dollar_calculation(entry_price, target_price, side, size)

    print(f"  Entry: ${entry_price}")
    print(f"  Size: {size}")
    print(f"  Target: ${target_price:.6f}")
    print(f"  Net profit: ${net_profit:.6f}")
    print(f"  Différence avec 1$: ${abs(net_profit - 1.0):.6f}")

    # Test SHORT position
    print("\nTest SHORT position:")
    side = 'SHORT'

    target_price = dollar_taker.calculate_target_price(entry_price, side, size, leverage)
    net_profit = dollar_taker.verify_dollar_calculation(entry_price, target_price, side, size)

    print(f"  Entry: ${entry_price}")
    print(f"  Size: {size}")
    print(f"  Target: ${target_price:.6f}")
    print(f"  Net profit: ${net_profit:.6f}")
    print(f"  Différence avec 1$: ${abs(net_profit - 1.0):.6f}")

    # Test avec différentes tailles
    print("\nTest avec différentes tailles de position:")
    for test_size in [0.001, 0.01, 0.1, 1.0]:
        target_long = dollar_taker.calculate_target_price(1000.0, 'LONG', test_size, 10)
        net_long = dollar_taker.verify_dollar_calculation(1000.0, target_long, 'LONG', test_size)

        print(f"  Size {test_size}: Target ${target_long:.4f}, Net ${net_long:.6f}")

def test_dollar_taker_real():
    """Test du DollarTaker avec des données réelles"""

    print("\n=== TEST AVEC DONNÉES RÉELLES ===\n")

    try:
        hyperliquid_client = HyperliquidClient()
        dollar_taker = DollarTaker(hyperliquid_client=hyperliquid_client)

        print("Client Hyperliquid initialisé")
        print("DollarTaker créé")

        # Tester avec les positions actuelles
        dollar_taker.update_tracked_positions()

        print(f"Positions surveillées: {len(dollar_taker.tracked_positions)}")

        if dollar_taker.tracked_positions:
            print("\nPositions actuelles avec targets calculés:")
            for key, pos in dollar_taker.tracked_positions.items():
                current_price = hyperliquid_client.get_current_market_price(pos['symbol'])
                distance = abs(current_price - pos['target_price']) if current_price else 0

                print(f"  {pos['symbol']} {pos['side']}:")
                print(f"    Entry: ${pos['entry_price']:.4f}")
                print(f"    Target: ${pos['target_price']:.4f}")
                print(f"    Current: ${current_price:.4f}")
                print(f"    Distance: ${distance:.4f}")

    except Exception as e:
        print(f"Erreur: {e}")

def main():
    print("=== DOLLAR TAKER TESTING ===\n")

    # Test des calculs
    test_dollar_calculations()

    # Test avec données réelles
    test_dollar_taker_real()

    print("\n=== TESTS TERMINÉS ===")

if __name__ == "__main__":
    main()