const { PrismaClient } = require('@prisma/client');

async function checkLearningSystemStatus() {
  const prisma = new PrismaClient();
  
  try {
    console.log('🤖 AI Learning System Intelligence Report\n');
    
    // Check if we can access the actual learning system
    const SimplifiedStopLossLearner = require('./lib/simplified-stop-loss-learner.js');
    const learner = new SimplifiedStopLossLearner();
    
    console.log('📊 Learning System Status:');
    
    // Try to get learning report
    if (typeof learner.generateLearningReport === 'function') {
      try {
        const report = await learner.generateLearningReport();
        console.log('  ✅ Learning system is active and functional');
        console.log(`  📈 System confidence: ${report.summary?.systemConfidence || 'N/A'}%`);
        console.log(`  🎯 Success rate: ${report.summary?.successRate || 'N/A'}%`);
        console.log(`  📋 Total decisions: ${report.summary?.totalDecisions || 'N/A'}`);
        
        if (report.insights) {
          console.log('\n🧠 Current Learning Insights:');
          console.log(`  Confidence threshold: ${report.insights.confidenceLevel || 'N/A'}%`);
          if (report.insights.thresholds) {
            console.log(`  Risk thresholds: ${JSON.stringify(report.insights.thresholds)}`);
          }
        }
        
        if (report.recommendations && report.recommendations.length > 0) {
          console.log('\n💡 System Recommendations:');
          report.recommendations.forEach(rec => {
            console.log(`  - ${rec}`);
          });
        }
        
      } catch (error) {
        console.log(`  ❌ Error getting learning report: ${error.message}`);
      }
    }
    
    // Test smart recommendation system
    if (typeof learner.getSmartRecommendation === 'function') {
      try {
        const testRequest = {
          distanceFromSL: 0.02,
          symbol: 'SOL-PERP',
          marketConditions: 'VOLATILE',
          currentPrice: 180,
          stopLossPrice: 175
        };
        
        const smartRec = await learner.getSmartRecommendation(testRequest);
        console.log('\n🎯 Smart Recommendation Test:');
        console.log(`  Action: ${smartRec.action}`);
        console.log(`  Confidence: ${smartRec.confidence}%`);
        console.log(`  Reasoning: ${smartRec.reasoning}`);
        
      } catch (error) {
        console.log(`\n❌ Smart recommendation test failed: ${error.message}`);
      }
    }
    
    // Check what the system has learned from patterns
    const recentDecisions = await prisma.ai_learning_data.findMany({
      where: {
        timeframe: 'DECISION',
        confidenceScore: { not: null }
      },
      select: {
        analysisData: true,
        confidenceScore: true,
        outcome: true,
        createdAt: true
      },
      orderBy: { createdAt: 'desc' },
      take: 10
    });
    
    console.log('\n📋 Recent AI Decision Logic:');
    recentDecisions.forEach((decision, index) => {
      try {
        const analysis = JSON.parse(decision.analysisData);
        console.log(`\n  Decision ${index + 1} (${new Date(decision.createdAt).toLocaleString()}):`);
        console.log(`    Confidence: ${decision.confidenceScore}%`);
        console.log(`    Action: ${analysis.action || 'N/A'}`);
        
        if (analysis.reasoning) {
          // Extract key learning phrases
          const reasoning = analysis.reasoning;
          if (reasoning.includes('based on') || reasoning.includes('learned') || reasoning.includes('pattern')) {
            console.log(`    🧠 Learning-based: Yes`);
          }
          console.log(`    Logic: ${reasoning.substring(0, 150)}${reasoning.length > 150 ? '...' : ''}`);
        }
        
        if (analysis.riskFactors) {
          console.log(`    Risk factors: ${JSON.stringify(analysis.riskFactors)}`);
        }
        
      } catch (e) {
        console.log(`  Decision ${index + 1}: Parse error`);
      }
    });
    
    // Check confidence evolution patterns
    const confidenceEvolution = await prisma.ai_learning_data.findMany({
      where: {
        confidenceScore: { not: null },
        timeframe: { in: ['DECISION', 'OUTCOME'] }
      },
      select: {
        confidenceScore: true,
        timeframe: true,
        createdAt: true
      },
      orderBy: { createdAt: 'desc' },
      take: 20
    });
    
    console.log('\n📈 Confidence Pattern Analysis:');
    
    const decisions = confidenceEvolution.filter(r => r.timeframe === 'DECISION');
    const outcomes = confidenceEvolution.filter(r => r.timeframe === 'OUTCOME');
    
    if (decisions.length >= 3) {
      const recent = decisions.slice(0, 3).map(d => d.confidenceScore).reduce((a, b) => a + b, 0) / 3;
      const older = decisions.slice(3, 6).map(d => d.confidenceScore).reduce((a, b) => a + b, 0) / Math.max(1, decisions.slice(3, 6).length);
      
      console.log(`  Recent decisions avg: ${recent.toFixed(1)}%`);
      console.log(`  Previous decisions avg: ${older.toFixed(1)}%`);
      console.log(`  Trend: ${recent > older ? '📈 Increasing confidence' : '📉 Becoming more cautious'}`);
    }
    
    // Check what conditions trigger high vs low confidence
    const highConf = await prisma.ai_learning_data.findMany({
      where: {
        confidenceScore: { gte: 70 },
        timeframe: 'DECISION'
      },
      select: { analysisData: true },
      take: 5
    });
    
    const lowConf = await prisma.ai_learning_data.findMany({
      where: {
        confidenceScore: { lte: 30 },
        timeframe: 'DECISION'
      },
      select: { analysisData: true },
      take: 5
    });
    
    console.log('\n🔍 What Triggers AI Confidence Changes:');
    
    if (highConf.length > 0) {
      console.log('\n  High Confidence Triggers:');
      highConf.forEach((record, i) => {
        try {
          const analysis = JSON.parse(record.analysisData);
          if (analysis.reasoning) {
            const key = analysis.reasoning.split('.')[0] || analysis.reasoning.substring(0, 80);
            console.log(`    ${i + 1}. ${key}...`);
          }
        } catch (e) {}
      });
    }
    
    if (lowConf.length > 0) {
      console.log('\n  Low Confidence Triggers:');
      lowConf.forEach((record, i) => {
        try {
          const analysis = JSON.parse(record.analysisData);
          if (analysis.reasoning) {
            const key = analysis.reasoning.split('.')[0] || analysis.reasoning.substring(0, 80);
            console.log(`    ${i + 1}. ${key}...`);
          }
        } catch (e) {}
      });
    }
    
  } catch (error) {
    console.error('❌ Error analyzing learning system:', error);
  } finally {
    await prisma.$disconnect();
  }
}

checkLearningSystemStatus();
