const { simpleAutomation } = require('./lib/simple-automation.js');

async function testDecisionTracking() {
  console.log('🧪 Testing AI Decision Tracking...\n');

  // Create a mock analysis result
  const mockAnalysis = {
    recommendation: 'STRONG BUY',
    confidence: 85,
    reasoning: 'Multiple bullish signals: RSI oversold recovery, MACD bullish crossover, strong volume confirmation. Price broke above key resistance with momentum.',
    summary: 'Technical indicators align for upward momentum with high probability of continuation',
    stopLoss: 184.50,
    takeProfit: 190.25,
    entry: { price: 186.20 },
    currentPrice: 186.20
  };

  console.log('📊 Mock Analysis Input:');
  console.log(JSON.stringify(mockAnalysis, null, 2));
  console.log('\n');

  // Test decision evaluation (without execution)
  console.log('🎯 Testing shouldExecuteTrade logic...');
  
  // Simulate the automation config
  simpleAutomation.config = {
    selectedTimeframes: ['1h', '4h'],
    symbol: 'SOLUSD',
    mode: 'SIMULATION',
    enableTrading: false
  };

  const shouldExecute = simpleAutomation.shouldExecuteTrade(mockAnalysis);
  console.log(`✅ Should Execute Trade: ${shouldExecute}`);
  console.log(`📝 Decision stored: ${!!simpleAutomation.lastDecision}`);

  if (simpleAutomation.lastDecision) {
    console.log('\n🧠 Last Decision Details:');
    console.log(`   Timestamp: ${simpleAutomation.lastDecision.timestamp}`);
    console.log(`   Recommendation: ${simpleAutomation.lastDecision.recommendation}`);
    console.log(`   Confidence: ${simpleAutomation.lastDecision.confidence}%`);
    console.log(`   Min Required: ${simpleAutomation.lastDecision.minConfidenceRequired}%`);
    console.log(`   Reasoning: ${simpleAutomation.lastDecision.reasoning.substring(0, 80)}...`);
    console.log(`   Executed: ${simpleAutomation.lastDecision.executed}`);
  }

  // Test getting status with decision
  console.log('\n📊 Testing getStatus() with decision...');
  const status = simpleAutomation.getStatus();
  console.log(`✅ Status includes lastDecision: ${!!status.lastDecision}`);
  
  if (status.lastDecision) {
    console.log(`   Decision confidence: ${status.lastDecision.confidence}%`);
    console.log(`   Decision recommendation: ${status.lastDecision.recommendation}`);
  }

  console.log('\n✅ Decision tracking test completed successfully!');
  console.log('📝 The automation-v2 page should now display AI decisions when automation runs.');
}

testDecisionTracking().catch(console.error);
