// Test the position analysis functionality

async function testPositionAnalysis() {
  console.log('🔍 Testing Position Analysis Functionality...\n');

  try {
    // Get current position data
    console.log('📊 Fetching current position...');
    const positionResponse = await fetch('http://localhost:9001/api/automation/position-monitor');
    const positionData = await positionResponse.json();
    
    if (positionData.success && positionData.monitor.hasPosition) {
      console.log('✅ Position found:', positionData.monitor.position);
      
      // Analyze the position
      console.log('\n🧠 Generating AI analysis...');
      const analysisResponse = await fetch('http://localhost:9001/api/automation/analyze-position', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          action: 'analyze_existing_position',
          positionData: positionData.monitor.position
        })
      });
      
      const analysisData = await analysisResponse.json();
      
      if (analysisData.success) {
        console.log('\n🎯 AI Analysis Generated:');
        console.log(`Recommendation: ${analysisData.decision.recommendation}`);
        console.log(`Confidence: ${analysisData.decision.confidence}%`);
        console.log(`Retroactive: ${analysisData.decision.isRetrospective}`);
        console.log('\n📝 Reasoning Preview:');
        console.log(analysisData.decision.reasoning.substring(0, 200) + '...');
        
        console.log('\n💰 Execution Details:');
        console.log(`Side: ${analysisData.decision.executionDetails.side}`);
        console.log(`Amount: $${analysisData.decision.executionDetails.amount}`);
        console.log(`Leverage: ${analysisData.decision.executionDetails.leverage}x`);
        console.log(`Entry: $${analysisData.decision.executionDetails.currentPrice}`);
        console.log(`Stop Loss: $${analysisData.decision.executionDetails.stopLoss}`);
        console.log(`Take Profit: $${analysisData.decision.executionDetails.takeProfit.toFixed(2)}`);
        
        console.log('\n✅ Position analysis test completed successfully!');
        console.log('📱 Now you can use the "🔍 ANALYZE" button in the UI to see this analysis.');
      } else {
        console.error('❌ Analysis failed:', analysisData.error);
      }
    } else {
      console.log('ℹ️ No position found to analyze');
    }
    
  } catch (error) {
    console.error('❌ Test error:', error.message);
  }
}

testPositionAnalysis().catch(console.error);
