/**
 * Test AI Leverage Calculator
 * 
 * Quick test to verify the AI leverage calculation system works correctly
 */

const AILeverageCalculator = require('./lib/ai-leverage-calculator.ts').default

async function testAILeverageCalculator() {
  console.log('🧠 Testing AI Leverage Calculator...\n')

  // Test Case 1: Small account (<$1k) - Should use 100% balance aggressively
  console.log('📊 Test Case 1: Small Account Strategy (<$1k)')
  const smallAccountResult = AILeverageCalculator.calculateOptimalLeverage({
    accountValue: 500,
    availableBalance: 450,
    entryPrice: 185,
    stopLossPrice: 175, // Long position, 5.4% stop loss
    side: 'long',
    maxLeverageAllowed: 20,
    safetyBuffer: 0.10
  })
  
  console.log('Small Account Result:', {
    leverage: `${smallAccountResult.recommendedLeverage.toFixed(1)}x`,
    riskLevel: smallAccountResult.riskAssessment,
    liquidation: `$${smallAccountResult.liquidationPrice.toFixed(2)}`,
    reasoning: smallAccountResult.reasoning
  })
  console.log('')

  // Test Case 2: Large account (>$1k) - Should use 50% balance conservatively  
  console.log('📊 Test Case 2: Large Account Strategy (>$1k)')
  const largeAccountResult = AILeverageCalculator.calculateOptimalLeverage({
    accountValue: 2500,
    availableBalance: 2000,
    entryPrice: 185,
    stopLossPrice: 175, // Long position, 5.4% stop loss
    side: 'long',
    maxLeverageAllowed: 20,
    safetyBuffer: 0.10
  })
  
  console.log('Large Account Result:', {
    leverage: `${largeAccountResult.recommendedLeverage.toFixed(1)}x`,
    riskLevel: largeAccountResult.riskAssessment,
    liquidation: `$${largeAccountResult.liquidationPrice.toFixed(2)}`,
    reasoning: largeAccountResult.reasoning
  })
  console.log('')

  // Test Case 3: Short position with tight stop loss
  console.log('📊 Test Case 3: Short Position with Tight Stop')
  const shortResult = AILeverageCalculator.calculateOptimalLeverage({
    accountValue: 800,
    availableBalance: 750,
    entryPrice: 185,
    stopLossPrice: 195, // Short position, 5.4% stop loss above entry
    side: 'short',
    maxLeverageAllowed: 20,
    safetyBuffer: 0.10
  })
  
  console.log('Short Position Result:', {
    leverage: `${shortResult.recommendedLeverage.toFixed(1)}x`,
    riskLevel: shortResult.riskAssessment,
    liquidation: `$${shortResult.liquidationPrice.toFixed(2)}`,
    reasoning: shortResult.reasoning
  })
  console.log('')

  // Test Case 4: Very tight stop loss - should limit leverage
  console.log('📊 Test Case 4: Very Tight Stop Loss (2%)')
  const tightStopResult = AILeverageCalculator.calculateOptimalLeverage({
    accountValue: 1000,
    availableBalance: 900,
    entryPrice: 185,
    stopLossPrice: 181.3, // Only 2% stop loss - very tight
    side: 'long',
    maxLeverageAllowed: 20,
    safetyBuffer: 0.10
  })
  
  console.log('Tight Stop Result:', {
    leverage: `${tightStopResult.recommendedLeverage.toFixed(1)}x`,
    riskLevel: tightStopResult.riskAssessment,
    liquidation: `$${tightStopResult.liquidationPrice.toFixed(2)}`,
    reasoning: tightStopResult.reasoning
  })

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

// Run the test
testAILeverageCalculator().catch(console.error)
