const { MandatoryRiskManager } = require('./lib/mandatory-risk-manager');

async function testMandatoryRiskManagement() {
  console.log('🧪 TESTING MANDATORY RISK MANAGEMENT SYSTEM');
  console.log('=' .repeat(60));
  
  const riskManager = new MandatoryRiskManager();
  
  // Test 1: Trade with no SL/TP (should auto-calculate)
  console.log('\n📋 TEST 1: Trade with missing SL/TP');
  try {
    const result1 = await riskManager.enforceRiskManagement({
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 50,
      currentPrice: 185,
      stopLoss: null,
      takeProfit: null,
      leverage: 1
    });
    console.log('✅ Test 1 PASSED - Auto-calculated SL/TP');
    console.log(`   SL: $${result1.stopLoss.toFixed(2)}, TP: $${result1.takeProfit.toFixed(2)}`);
  } catch (error) {
    console.log('❌ Test 1 FAILED:', error.message);
  }
  
  // Test 2: Trade with proper SL/TP (should pass validation)
  console.log('\n📋 TEST 2: Trade with proper SL/TP');
  try {
    const result2 = await riskManager.enforceRiskManagement({
      symbol: 'SOLUSD',
      side: 'SHORT',
      amount: 50,
      currentPrice: 185,
      stopLoss: 195, // 5.4% above current (good for SHORT)
      takeProfit: 175, // 5.4% below current (good for SHORT)
      leverage: 1
    });
    console.log('✅ Test 2 PASSED - Proper risk management');
    console.log(`   Risk/Reward: 1:${result2.riskValidation.riskRewardRatio.toFixed(2)}`);
  } catch (error) {
    console.log('❌ Test 2 FAILED:', error.message);
  }
  
  // Test 3: Trade with wrong SL direction (should fail)
  console.log('\n📋 TEST 3: Trade with wrong SL direction');
  try {
    const result3 = await riskManager.enforceRiskManagement({
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 50,
      currentPrice: 185,
      stopLoss: 195, // WRONG: SL above price for BUY
      takeProfit: 175, // WRONG: TP below price for BUY
      leverage: 1
    });
    console.log('❌ Test 3 FAILED - Should have been blocked');
  } catch (error) {
    console.log('✅ Test 3 PASSED - Correctly blocked:', error.message);
  }
  
  // Test 4: High risk trade (should fail)
  console.log('\n📋 TEST 4: High risk trade with 20x leverage');
  try {
    const result4 = await riskManager.enforceRiskManagement({
      symbol: 'SOLUSD',
      side: 'BUY',
      amount: 50,
      currentPrice: 185,
      stopLoss: 175, // 5.4% risk
      takeProfit: 195,
      leverage: 20 // 20x leverage = 108% potential loss!
    });
    console.log('❌ Test 4 FAILED - Should have been blocked');
  } catch (error) {
    console.log('✅ Test 4 PASSED - Correctly blocked high risk:', error.message);
  }
  
  // Test 5: Poor risk/reward ratio (should fail)
  console.log('\n📋 TEST 5: Poor risk/reward ratio');
  try {
        const test2 = {
      type: 'long',
      symbol: 'SOLUSD',
      entryPrice: 185,
      quantity: 0.5,
      stopLoss: 182,
      takeProfit: 192,
      leverage: 5
    };

    try {
      await riskManager.enforceRiskManagement(test5);
      console.log('❌ Test 5 FAILED - Should have been blocked');
  } catch (error) {
    console.log('✅ Test 5 PASSED - Correctly blocked poor ratio:', error.message);
  }
  
  console.log('\n🎯 MANDATORY RISK MANAGEMENT TESTING COMPLETE');
  console.log('💡 System will now BLOCK trades without proper SL/TP');
}

testMandatoryRiskManagement();
