#!/usr/bin/env node

/**
 * Test Last Decision Tracking in Automation
 */

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

async function testLastDecisionTracking() {
  console.log('🧪 Testing Last Decision Tracking...');
  
  // Set up mock config
  simpleAutomation.config = {
    symbol: 'SOLUSD',
    tradingAmount: 100,
    leverage: 1,
    mode: 'SIMULATION',
    selectedTimeframes: ['60', '240']
  };
  
  // Test scenario 1: Low confidence - should not execute
  console.log('\n1️⃣ Testing Low Confidence Decision...');
  const lowConfidenceAnalysis = {
    recommendation: 'BUY',
    confidence: 65, // Below 75% threshold
    reasoning: 'Weak bullish signal with mixed indicators',
    currentPrice: 186.50
  };
  
  const shouldTrade1 = simpleAutomation.shouldExecuteTrade(lowConfidenceAnalysis);
  console.log('Decision:', shouldTrade1 ? 'EXECUTE' : 'NO TRADE');
  console.log('Last Decision:', simpleAutomation.stats.lastDecision);
  
  // Test scenario 2: High confidence - should execute (but in simulation)
  console.log('\n2️⃣ Testing High Confidence Decision...');
  const highConfidenceAnalysis = {
    recommendation: 'BUY',
    confidence: 85, // Above 75% threshold
    reasoning: 'Strong bullish breakout with volume confirmation',
    currentPrice: 186.50
  };
  
  const shouldTrade2 = simpleAutomation.shouldExecuteTrade(highConfidenceAnalysis);
  console.log('Decision:', shouldTrade2 ? 'EXECUTE' : 'NO TRADE');
  console.log('Last Decision:', simpleAutomation.stats.lastDecision);
  
  // Test scenario 3: Live mode simulation
  console.log('\n3️⃣ Testing Live Mode Decision...');
  simpleAutomation.config.mode = 'LIVE';
  
  const liveAnalysis = {
    recommendation: 'SELL',
    confidence: 88,
    reasoning: 'Strong bearish reversal pattern with RSI divergence',
    currentPrice: 186.50
  };
  
  const shouldTrade3 = simpleAutomation.shouldExecuteTrade(liveAnalysis);
  console.log('Decision:', shouldTrade3 ? 'EXECUTE' : 'NO TRADE');
  console.log('Last Decision:', simpleAutomation.stats.lastDecision);
  
  // Test execution tracking
  if (shouldTrade3) {
    console.log('\n4️⃣ Testing Execution Tracking...');
    try {
      await simpleAutomation.executeTrade(liveAnalysis);
      console.log('Updated Last Decision with Execution:', simpleAutomation.stats.lastDecision);
    } catch (error) {
      console.log('Execution test completed (expected error in test environment)');
    }
  }
  
  console.log('\n✅ Last Decision Tracking Test Complete!');
  console.log('\n📊 Final Status:', simpleAutomation.getStatus());
}

testLastDecisionTracking();
