#!/usr/bin/env node

/**
 * Test the AI Learning System
 * Verify that generateLearningReport is working
 */

async function testLearningSystem() {
  console.log('🧪 Testing AI Learning System');
  console.log('=' .repeat(50));
  
  try {
    // Import the learner
    const SimplifiedStopLossLearner = require('./lib/simplified-stop-loss-learner');
    const learner = new SimplifiedStopLossLearner();
    
    console.log('✅ Successfully imported SimplifiedStopLossLearner');
    
    // Test generateLearningReport function
    console.log('\n📊 Testing generateLearningReport...');
    const report = await learner.generateLearningReport();
    
    if (report) {
      console.log('✅ Learning report generated successfully!');
      console.log('\n📋 Report Summary:');
      console.log('  - Total Decisions:', report.summary?.totalDecisions || 0);
      console.log('  - Recent Decisions:', report.summary?.recentDecisions || 0);
      console.log('  - System Confidence:', Math.round((report.summary?.systemConfidence || 0) * 100) + '%');
      console.log('  - Active Learning:', report.summary?.isActive ? 'YES' : 'NO');
      
      if (report.insights) {
        console.log('\n🔍 Learning Insights:');
        console.log('  - Emergency Threshold:', report.insights.emergencyThreshold + '%');
        console.log('  - Risk Threshold:', report.insights.riskThreshold + '%');
        console.log('  - Confidence Level:', report.insights.confidenceLevel);
      }
      
      if (report.recommendations && report.recommendations.length > 0) {
        console.log('\n💡 Recommendations:');
        report.recommendations.forEach(rec => {
          console.log(`  - ${rec.type}: ${rec.message} (${rec.priority})`);
        });
      }
    } else {
      console.log('❌ No report generated');
    }
    
    // Test getSmartRecommendation
    console.log('\n🎯 Testing getSmartRecommendation...');
    const recommendation = await learner.getSmartRecommendation({
      distanceFromSL: 3.5,
      symbol: 'SOL-PERP',
      marketConditions: {
        price: 187.50,
        side: 'long'
      }
    });
    
    if (recommendation) {
      console.log('✅ Smart recommendation generated:');
      console.log('  - Action:', recommendation.action);
      console.log('  - Confidence:', Math.round((recommendation.confidence || 0) * 100) + '%');
      console.log('  - Reasoning:', recommendation.reasoning);
    }
    
    console.log('\n🎉 AI Learning System Test Complete!');
    console.log('🚀 The system is ready to learn from trading decisions.');
    
  } catch (error) {
    console.error('❌ Test failed:', error.message);
    console.log('\n🔍 Error details:');
    console.log(error.stack);
  }
}

// Run the test
testLearningSystem().then(() => {
  console.log('\n✅ Test completed');
}).catch(error => {
  console.error('❌ Test error:', error);
});
