// Test script to verify AI learning system integration
const path = require('path');

async function testLearningIntegration() {
  console.log('🧪 Testing AI Learning System Integration...\n');

  try {
    // Test 1: Check if learning-enhanced automation can be imported
    console.log('1️⃣ Testing automation with learning import...');
    const AutomationWithLearning = require('./lib/automation-with-learning.js');
    console.log('✅ AutomationWithLearning imported successfully');
    
    // Test 2: Create automation instance
    console.log('\n2️⃣ Creating automation instance...');
    const automation = new AutomationWithLearning();
    console.log('✅ Automation instance created');
    console.log('   - Has learner property:', 'learner' in automation);
    console.log('   - Has learning methods:', typeof automation.getLearningStatus === 'function');
    
    // Test 3: Check if SimplifiedStopLossLearner can be imported
    console.log('\n3️⃣ Testing SimplifiedStopLossLearner import...');
    try {
      const { SimplifiedStopLossLearner } = await import('./lib/simplified-stop-loss-learner-fixed.js');
      console.log('✅ SimplifiedStopLossLearner imported successfully');
      
      // Test creating learner instance
      const learner = new SimplifiedStopLossLearner();
      console.log('✅ SimplifiedStopLossLearner instance created');
      console.log('   - Available methods:', Object.getOwnPropertyNames(Object.getPrototypeOf(learner)).filter(name => name !== 'constructor'));
      
    } catch (learnerError) {
      console.log('❌ SimplifiedStopLossLearner import failed:', learnerError.message);
    }
    
    // Test 4: Initialize learning system
    console.log('\n4️⃣ Testing learning system initialization...');
    try {
      const initialized = await automation.initializeLearningSystem();
      console.log('✅ Learning system initialization result:', initialized);
      console.log('   - Learner created:', !!automation.learner);
      
      if (automation.learner) {
        console.log('   - Learner type:', automation.learner.constructor.name);
        
        // Test learning status
        if (typeof automation.getLearningStatus === 'function') {
          const status = await automation.getLearningStatus();
          console.log('   - Learning status:', status);
        }
      }
      
    } catch (initError) {
      console.log('❌ Learning system initialization failed:', initError.message);
    }
    
    // Test 5: Test singleton manager
    console.log('\n5️⃣ Testing singleton automation manager...');
    try {
      const { getAutomationInstance } = require('./lib/automation-singleton.js');
      const singletonInstance = await getAutomationInstance();
      console.log('✅ Singleton automation instance retrieved');
      console.log('   - Instance type:', singletonInstance.constructor.name);
      console.log('   - Has learning capabilities:', typeof singletonInstance.getLearningStatus === 'function');
      
    } catch (singletonError) {
      console.log('❌ Singleton manager test failed:', singletonError.message);
    }
    
    // Test 6: Test database connection
    console.log('\n6️⃣ Testing database connection...');
    try {
      const { getDB } = require('./lib/db.js');
      const db = await getDB();
      console.log('✅ Database connection successful');
      
      // Test if learning tables exist
      const tables = await db.$queryRaw`
        SELECT name FROM sqlite_master 
        WHERE type='table' AND name LIKE '%learning%'
      `;
      console.log('   - Learning-related tables:', tables.map(t => t.name));
      
    } catch (dbError) {
      console.log('❌ Database connection failed:', dbError.message);
    }
    
    console.log('\n🎯 Integration Test Summary:');
    console.log('📊 The AI learning system integration appears to be working');
    console.log('🔗 Key components are properly connected');
    console.log('💡 Learning system should now enhance trading decisions when automation starts');
    
  } catch (error) {
    console.error('❌ Integration test failed:', error);
  }
}

// Run the test
testLearningIntegration().catch(console.error);
