/**
 * Test AI Leverage Calculator Logic
 * 
 * Tests the core leverage calculation algorithms
 */

function calculateMaxSafeLeverage(entryPrice, stopLossPrice, side, safetyBuffer = 0.10) {
  // Calculate where liquidation should be (beyond stop loss + safety buffer)
  let maxLiquidationPrice;
  
  if (side === 'long') {
    // For longs: liquidation should be below stop loss
    maxLiquidationPrice = stopLossPrice * (1 - safetyBuffer);
  } else {
    // For shorts: liquidation should be above stop loss  
    maxLiquidationPrice = stopLossPrice * (1 + safetyBuffer);
  }

  console.log(`🛡️ Max Safe Liquidation Price: $${maxLiquidationPrice.toFixed(4)} (${side} position)`);

  // Calculate max leverage that keeps liquidation at safe distance
  let maxLeverage;
  
  if (side === 'long') {
    // For longs: liq = entry * (1 - 1/leverage)
    maxLeverage = 1 / (1 - maxLiquidationPrice / entryPrice);
  } else {
    // For shorts: liq = entry * (1 + 1/leverage)
    maxLeverage = 1 / (maxLiquidationPrice / entryPrice - 1);
  }

  // Ensure reasonable bounds
  maxLeverage = Math.max(1, Math.min(maxLeverage, 50));

  console.log(`⚖️ Max Safe Leverage: ${maxLeverage.toFixed(2)}x`);
  
  return maxLeverage;
}

function calculateLiquidationPrice(entryPrice, leverage, side) {
  if (side === 'long') {
    return entryPrice * (1 - 1/leverage);
  } else {
    return entryPrice * (1 + 1/leverage);
  }
}

function testAILeverageLogic() {
  console.log('🧠 Testing AI Leverage Calculator Core Logic...\n');

  // Test Case 1: Conservative long position
  console.log('📊 Test Case 1: Conservative Long Position');
  console.log('Entry: $185, Stop Loss: $175 (5.4% drop)');
  
  const case1MaxLeverage = calculateMaxSafeLeverage(185, 175, 'long', 0.10);
  const case1LiqPrice = calculateLiquidationPrice(185, case1MaxLeverage, 'long');
  
  console.log(`✅ With ${case1MaxLeverage.toFixed(1)}x leverage:`);
  console.log(`   Liquidation: $${case1LiqPrice.toFixed(2)}`);
  console.log(`   Stop Loss: $175.00`);
  console.log(`   Safety Buffer: ${((175 - case1LiqPrice) / 175 * 100).toFixed(1)}%\n`);

  // Test Case 2: Aggressive short position  
  console.log('📊 Test Case 2: Aggressive Short Position');
  console.log('Entry: $185, Stop Loss: $195 (5.4% rise)');
  
  const case2MaxLeverage = calculateMaxSafeLeverage(185, 195, 'short', 0.10);
  const case2LiqPrice = calculateLiquidationPrice(185, case2MaxLeverage, 'short');
  
  console.log(`✅ With ${case2MaxLeverage.toFixed(1)}x leverage:`);
  console.log(`   Liquidation: $${case2LiqPrice.toFixed(2)}`);
  console.log(`   Stop Loss: $195.00`);
  console.log(`   Safety Buffer: ${((case2LiqPrice - 195) / 195 * 100).toFixed(1)}%\n`);

  // Test Case 3: Very tight stop loss
  console.log('📊 Test Case 3: Very Tight Stop Loss (2%)');
  console.log('Entry: $185, Stop Loss: $181.30 (2% drop)');
  
  const case3MaxLeverage = calculateMaxSafeLeverage(185, 181.30, 'long', 0.10);
  const case3LiqPrice = calculateLiquidationPrice(185, case3MaxLeverage, 'long');
  
  console.log(`✅ With ${case3MaxLeverage.toFixed(1)}x leverage:`);
  console.log(`   Liquidation: $${case3LiqPrice.toFixed(2)}`);
  console.log(`   Stop Loss: $181.30`);
  console.log(`   Safety Buffer: ${((181.30 - case3LiqPrice) / 181.30 * 100).toFixed(1)}%\n`);

  // Test Balance Strategies
  console.log('💰 Balance Strategy Tests:');
  
  const smallAccount = 500;
  const largeAccount = 2500;
  const availableSmall = 450;
  const availableLarge = 2000;
  
  console.log(`Small Account ($${smallAccount}): ${smallAccount < 1000 ? '100%' : '50%'} strategy = $${smallAccount < 1000 ? availableSmall : availableSmall * 0.5} used`);
  console.log(`Large Account ($${largeAccount}): ${largeAccount < 1000 ? '100%' : '50%'} strategy = $${largeAccount < 1000 ? availableLarge : availableLarge * 0.5} used`);

  console.log('\n✅ AI Leverage Calculator core logic tests completed!');
}

// Run the test
testAILeverageLogic();
