/**
 * Test AI leverage calculation standalone
 */

async function testLeverageCalculation() {
  console.log('🧪 Testing AI Leverage Calculation...\n');
  
  try {
    // Get real account balance
    const baseUrl = 'http://localhost:9001';
    const balanceResponse = await fetch(`${baseUrl}/api/drift/balance`);
    const balanceData = await balanceResponse.json();
    
    console.log('💰 Account Balance Data:', balanceData);
    
    if (!balanceData.success) {
      throw new Error('Failed to get account balance');
    }
    
    const accountValue = balanceData.accountValue;
    const availableBalance = balanceData.availableBalance;
    
    // Import AI Leverage Calculator
    const { AILeverageCalculator } = await import('./lib/ai-leverage-calculator.js');
    
    // Test scenarios based on recent automation trades
    const scenarios = [
      {
        name: 'BUY Scenario (Entry: $178, SL: $165)',
        entryPrice: 178,
        stopLossPrice: 165,
        side: 'long'
      },
      {
        name: 'SELL Scenario (Entry: $178, SL: $185)', 
        entryPrice: 178,
        stopLossPrice: 185,
        side: 'short'
      },
      {
        name: 'Tight SL Scenario (Entry: $178, SL: $175)',
        entryPrice: 178,
        stopLossPrice: 175,
        side: 'long'
      }
    ];
    
    console.log(`📊 Testing with Account: $${accountValue.toFixed(2)} total, $${availableBalance.toFixed(2)} available\n`);
    
    scenarios.forEach((scenario, index) => {
      console.log(`${index + 1}. ${scenario.name}`);
      
      const leverageResult = AILeverageCalculator.calculateOptimalLeverage({
        accountValue,
        availableBalance,
        entryPrice: scenario.entryPrice,
        stopLossPrice: scenario.stopLossPrice,
        side: scenario.side,
        maxLeverageAllowed: 10,
        safetyBuffer: 0.10
      });
      
      console.log(`   🎯 Recommended: ${leverageResult.recommendedLeverage.toFixed(1)}x leverage`);
      console.log(`   💰 Position Size: $${leverageResult.positionSize.toFixed(2)}`);
      console.log(`   🛡️ Liquidation: $${leverageResult.liquidationPrice.toFixed(2)}`);
      console.log(`   ⚖️ Risk: ${leverageResult.riskAssessment}`);
      console.log(`   📝 Reasoning: ${leverageResult.reasoning}\n`);
    });
    
    console.log('✅ AI Leverage Calculator is working correctly!');
    console.log('❌ The issue is that automation is not calling this calculator');
    
  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

// Run the test
testLeverageCalculation().catch(console.error);
