#!/usr/bin/env node

/**
 * Test Enhanced Risk Manager with Intelligent Screenshot Analysis
 * 
 * Simulates different distance scenarios to test when screenshot analysis triggers
 */

console.log('🧪 TESTING INTELLIGENT SCREENSHOT ANALYSIS TRIGGERING');
console.log('='.repeat(70));

async function testScreenshotTrigger() {
  const EnhancedAutonomousRiskManager = require('./lib/enhanced-autonomous-risk-manager');
  const riskManager = new EnhancedAutonomousRiskManager();
  
  console.log('🔧 Risk Manager Configuration:');
  console.log(`   Screenshot Analysis Threshold: ${riskManager.screenshotAnalysisThreshold}%`);
  console.log(`   Analysis Interval: ${riskManager.screenshotAnalysisInterval / 1000 / 60} minutes`);
  console.log(`   API URL: ${riskManager.baseApiUrl}`);
  
  // Test scenarios
  const testScenarios = [
    { distance: 8.5, description: 'Safe position - far from SL' },
    { distance: 4.2, description: 'Medium risk - approaching threshold' },
    { distance: 2.8, description: 'High risk - should trigger screenshot' },
    { distance: 1.5, description: 'Critical risk - should trigger screenshot' },
    { distance: 0.8, description: 'Emergency - should trigger screenshot' }
  ];
  
  console.log('\n📊 Testing Screenshot Trigger Logic:');
  console.log('='.repeat(50));
  
  for (const scenario of testScenarios) {
    const shouldTrigger = riskManager.shouldTriggerScreenshotAnalysis(scenario.distance);
    const emoji = shouldTrigger ? '📸' : '⏭️';
    const action = shouldTrigger ? 'TRIGGER ANALYSIS' : 'numerical only';
    
    console.log(`${emoji} ${scenario.distance}% - ${scenario.description}: ${action}`);
    
    // Simulate time passing for interval testing
    if (shouldTrigger) {
      console.log(`   ⏰ Last analysis time updated to prevent immediate re-trigger`);
      riskManager.lastScreenshotAnalysis = new Date();
      
      // Test immediate re-trigger (should be blocked)
      const immediateRetrigger = riskManager.shouldTriggerScreenshotAnalysis(scenario.distance);
      console.log(`   🔄 Immediate re-trigger test: ${immediateRetrigger ? 'ALLOWED' : 'BLOCKED (correct)'}`);
    }
  }
  
  console.log('\n🎯 OPTIMAL STRATEGY SUMMARY:');
  console.log('✅ Safe positions (>3%): Fast numerical monitoring only');
  console.log('📸 Risk positions (<3%): Trigger intelligent chart analysis');
  console.log('⏰ Rate limiting: Max 1 analysis per 5 minutes');
  console.log('🧠 Smart decisions: Combine numerical + visual data');
  
  console.log('\n💡 BENEFITS:');
  console.log('• Fast 30-second monitoring for normal conditions');
  console.log('• Detailed chart analysis only when needed');
  console.log('• Prevents screenshot analysis spam');
  console.log('• Smarter risk decisions with visual confirmation');
  console.log('• Optimal resource usage');
}

// Test the triggering logic
testScreenshotTrigger().catch(console.error);
