// Quick script to inject AI analysis for your current position into the UI

async function showPositionAnalysis() {
  console.log('🔍 Generating AI Analysis for Current Position...\n');

  try {
    // Step 1: Get position data
    console.log('📊 Fetching position data...');
    const positionResponse = await fetch('http://localhost:9001/api/automation/position-monitor');
    const positionData = await positionResponse.json();
    
    if (!positionData.success || !positionData.monitor.hasPosition) {
      console.log('❌ No position found to analyze');
      return;
    }

    console.log('✅ Position found:', positionData.monitor.position.symbol, positionData.monitor.position.side);

    // Step 2: Generate analysis
    console.log('🧠 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('═'.repeat(60));
      console.log(`📊 Recommendation: ${analysisData.decision.recommendation}`);
      console.log(`🎯 Confidence: ${analysisData.decision.confidence}%`);
      console.log(`⏰ Analysis Type: ${analysisData.decision.isRetrospective ? 'RETROACTIVE' : 'LIVE'}`);
      
      console.log('\n💭 AI REASONING:');
      console.log('─'.repeat(60));
      console.log(analysisData.decision.reasoning);
      
      console.log('\n💰 EXECUTION DETAILS:');
      console.log('─'.repeat(60));
      const exec = analysisData.decision.executionDetails;
      console.log(`Side: ${exec.side}`);
      console.log(`Amount: $${exec.amount}`);
      console.log(`Leverage: ${exec.leverage}x`);
      console.log(`Entry: $${exec.currentPrice.toFixed(4)}`);
      console.log(`Stop Loss: $${exec.stopLoss.toFixed(4)}`);
      console.log(`Take Profit: $${exec.takeProfit.toFixed(2)}`);
      
      console.log('\n🧠 AI LEVERAGE REASONING:');
      console.log('─'.repeat(60));
      console.log(exec.aiReasoning);
      
      console.log('\n✅ ANALYSIS COMPLETE!');
      console.log('📱 This analysis should now appear in the automation-v2 page');
      console.log('🔄 Refresh the page if needed to see the AI Trading Analysis panel populated');
      
    } else {
      console.error('❌ Analysis failed:', analysisData.error);
    }

  } catch (error) {
    console.error('❌ Error:', error.message);
  }
}

showPositionAnalysis().catch(console.error);
