#!/usr/bin/env python3
"""
Test mathématique direct des calculs SL/TP
"""

def test_degen_scalping_math():
    """Test calcul DegenScalping directement"""

    print("="*80)
    print("🧪 TEST MATHÉMATIQUE - DEGEN SCALPING")
    print("="*80)

    # Paramètres réels
    entry_price = 107622.62
    stop_loss_pct = 0.00037
    take_profit_pct = 0.00291

    print(f"\nEntry Price: ${entry_price:.2f}")
    print(f"SL %: {stop_loss_pct*100:.4f}%")
    print(f"TP %: {take_profit_pct*100:.4f}%")

    # Calcul LONG
    print("\n--- LONG ---")
    sl_long = entry_price * (1 - stop_loss_pct)
    tp_long = entry_price * (1 + take_profit_pct)

    print(f"SL: ${sl_long:.2f} ({((sl_long/entry_price-1)*100):+.4f}%)")
    print(f"TP: ${tp_long:.2f} ({((tp_long/entry_price-1)*100):+.4f}%)")

    # Vérifications LONG
    issues_long = []
    if sl_long >= entry_price:
        issues_long.append(f"❌ SL (${sl_long:.2f}) >= Entry")
    if tp_long <= entry_price:
        issues_long.append(f"❌ TP (${tp_long:.2f}) <= Entry")
    if tp_long == entry_price:
        issues_long.append(f"🚨 TP == Entry")

    if issues_long:
        print("🚨 PROBLÈMES:")
        for issue in issues_long:
            print(f"   {issue}")
    else:
        print("✅ Correct")

    # Calcul SHORT
    print("\n--- SHORT ---")
    sl_short = entry_price * (1 + stop_loss_pct)
    tp_short = entry_price * (1 - take_profit_pct)

    print(f"SL: ${sl_short:.2f} ({((sl_short/entry_price-1)*100):+.4f}%)")
    print(f"TP: ${tp_short:.2f} ({((tp_short/entry_price-1)*100):+.4f}%)")

    # Vérifications SHORT
    issues_short = []
    if sl_short <= entry_price:
        issues_short.append(f"❌ SL (${sl_short:.2f}) <= Entry")
    if tp_short >= entry_price:
        issues_short.append(f"❌ TP (${tp_short:.2f}) >= Entry")
    if tp_short == entry_price:
        issues_short.append(f"🚨 TP == Entry")

    if issues_short:
        print("🚨 PROBLÈMES:")
        for issue in issues_short:
            print(f"   {issue}")
    else:
        print("✅ Correct")

    return len(issues_long) == 0 and len(issues_short) == 0

def test_v10_range_math():
    """Test calcul V10 Range"""

    print("\n" + "="*80)
    print("🧪 TEST MATHÉMATIQUE - V10 RANGE")
    print("="*80)

    entry_price = 107622.62
    atr = 500  # ATR hypothétique
    sl_mult = 1.64
    tp_mult = 3.28

    print(f"\nEntry Price: ${entry_price:.2f}")
    print(f"ATR: ${atr:.2f}")
    print(f"SL multiplier: {sl_mult}")
    print(f"TP multiplier: {tp_mult}")

    # Calcul LONG
    print("\n--- LONG ---")
    sl_distance = atr * sl_mult
    tp_distance = atr * tp_mult

    sl_long = entry_price - sl_distance
    tp_long = entry_price + tp_distance

    print(f"SL: ${sl_long:.2f} (entry - {sl_distance:.2f})")
    print(f"TP: ${tp_long:.2f} (entry + {tp_distance:.2f})")

    issues_long = []
    if sl_long >= entry_price:
        issues_long.append(f"❌ SL >= Entry")
    if tp_long <= entry_price:
        issues_long.append(f"❌ TP <= Entry")

    if issues_long:
        print("🚨 PROBLÈMES:")
        for issue in issues_long:
            print(f"   {issue}")
    else:
        print("✅ Correct")

    # Calcul SHORT
    print("\n--- SHORT ---")
    sl_short = entry_price + sl_distance
    tp_short = entry_price - tp_distance

    print(f"SL: ${sl_short:.2f} (entry + {sl_distance:.2f})")
    print(f"TP: ${tp_short:.2f} (entry - {tp_distance:.2f})")

    issues_short = []
    if sl_short <= entry_price:
        issues_short.append(f"❌ SL <= Entry")
    if tp_short >= entry_price:
        issues_short.append(f"❌ TP >= Entry")

    if issues_short:
        print("🚨 PROBLÈMES:")
        for issue in issues_short:
            print(f"   {issue}")
    else:
        print("✅ Correct")

    return len(issues_long) == 0 and len(issues_short) == 0

def test_conversion_to_contract():
    """Test conversion des prix pour le smart contract"""

    print("\n" + "="*80)
    print("🧪 TEST CONVERSION SMART CONTRACT")
    print("="*80)

    entry = 107622.62
    sl = 107662.48  # Entry + 0.037%
    tp = 107309.18  # Entry - 0.291%

    print(f"\nPrix float:")
    print(f"  Entry: ${entry:.2f}")
    print(f"  SL: ${sl:.2f}")
    print(f"  TP: ${tp:.2f}")

    # Conversion 1e8 (selon doc Aster)
    entry_int = int(entry * 1e8)
    sl_int = int(sl * 1e8)
    tp_int = int(tp * 1e8)

    print(f"\nConversion 1e8:")
    print(f"  Entry: {entry_int}")
    print(f"  SL: {sl_int}")
    print(f"  TP: {tp_int}")

    # Reconversion
    entry_back = entry_int / 1e8
    sl_back = sl_int / 1e8
    tp_back = tp_int / 1e8

    print(f"\nReconversion:")
    print(f"  Entry: ${entry_back:.2f}")
    print(f"  SL: ${sl_back:.2f}")
    print(f"  TP: ${tp_back:.2f}")

    # Vérifier qu'ils sont différents
    issues = []
    if entry_int == sl_int:
        issues.append("🚨 Entry_int == SL_int")
    if entry_int == tp_int:
        issues.append("🚨 Entry_int == TP_int")

    if issues:
        print("\n🚨 PROBLÈMES:")
        for issue in issues:
            print(f"   {issue}")
        return False
    else:
        print("\n✅ Conversions correctes")
        return True

if __name__ == '__main__':
    print("🔬 TESTS MATHÉMATIQUES SL/TP\n")

    results = {
        'Degen Scalping': test_degen_scalping_math(),
        'V10 Range': test_v10_range_math(),
        'Conversion Contract': test_conversion_to_contract()
    }

    print("\n" + "="*80)
    print("📊 RÉSUMÉ")
    print("="*80)

    for name, passed in results.items():
        status = "✅ PASS" if passed else "❌ FAIL"
        print(f"   {status} - {name}")

    if all(results.values()):
        print("\n✅ TOUS LES CALCULS MATHÉMATIQUES SONT CORRECTS")
    else:
        print("\n🚨 ERREURS DÉTECTÉES DANS LES CALCULS")
