/**
 * Test AI Learning Integration
 * Verifies that the learning system is properly integrated and working
 */

import { SimplifiedStopLossLearner } from './lib/simplified-stop-loss-learner-fixed.js';

async function testLearningIntegration() {
  console.log('🧪 Testing AI Learning Integration...\n');
  
  try {
    // 1. Initialize the learner
    const learner = new SimplifiedStopLossLearner();
    console.log('✅ Learning system initialized');
    
    // 2. Test learning status
    console.log('\n📊 Getting learning status...');
    const status = await learner.getLearningStatus();
    console.log('Status:', JSON.stringify(status, null, 2));
    
    // 3. Test learning report
    console.log('\n📈 Generating learning report...');
    const report = await learner.generateLearningReport();
    console.log('Report Summary:', {
      totalDecisions: report.summary.totalDecisions,
      systemConfidence: report.summary.systemConfidence,
      isActive: report.summary.isActive,
      phase: report.insights?.confidenceLevel
    });
    
    // 4. Test smart recommendation
    console.log('\n🧠 Testing smart recommendation...');
    const testRequest = {
      symbol: 'SOL-PERP',
      confidence: 75,
      recommendation: 'LONG',
      marketConditions: {
        timeframes: ['1h', '4h'],
        strategy: 'Day Trading'
      },
      aiLevels: {
        stopLoss: 190.50,
        takeProfit: 195.50
      }
    };
    
    const recommendation = await learner.getSmartRecommendation(testRequest);
    if (recommendation) {
      console.log('Smart Recommendation:', {
        action: recommendation.action,
        confidence: Math.round(recommendation.confidence * 100) + '%',
        reasoning: recommendation.reasoning
      });
    } else {
      console.log('No smart recommendation available (insufficient data)');
    }
    
    // 5. Test decision recording
    console.log('\n📝 Testing decision recording...');
    const testDecision = {
      tradeId: `test_${Date.now()}`,
      symbol: 'SOL-PERP',
      decision: 'EXECUTE_TRADE',
      confidence: 78,
      recommendation: 'LONG',
      reasoning: 'Test decision for learning integration',
      marketConditions: { strategy: 'Test' },
      expectedOutcome: 'PROFITABLE_TRADE'
    };
    
    const decisionId = await learner.recordDecision(testDecision);
    console.log(`Decision recorded with ID: ${decisionId}`);
    
    console.log('\n� All learning integration tests passed!');
    console.log('\n� Learning System Status:');
    console.log(`   - Total Decisions: ${status.totalDecisions}`);
    console.log(`   - Recent Activity: ${status.recentDecisions} (last 24h)`);
    console.log(`   - System Confidence: ${Math.round(report.summary.systemConfidence * 100)}%`);
    console.log(`   - Learning Phase: ${report.insights?.confidenceLevel || 'UNKNOWN'}`);
    console.log(`   - Is Active: ${status.isActive ? 'YES' : 'NO'}`);
    
  } catch (error) {
    console.error('❌ Learning integration test failed:', error.message);
    console.error('Stack:', error.stack);
  }
}

// Run the test
testLearningIntegration().then(() => {
  console.log('\n✅ Test completed');
  process.exit(0);
}).catch(error => {
  console.error('❌ Test failed:', error);
  process.exit(1);
});
