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

async function analyzeDecisionPatterns() {
  const prisma = new PrismaClient();
  
  try {
    console.log('🔍 AI Decision Pattern Analysis\n');
    
    // Get records with decision and outcome pairs
    const decisionOutcomePairs = await prisma.ai_learning_data.findMany({
      where: {
        OR: [
          { timeframe: 'DECISION' },
          { timeframe: 'OUTCOME' }
        ]
      },
      select: {
        timeframe: true,
        analysisData: true,
        confidenceScore: true,
        createdAt: true,
        outcome: true
      },
      orderBy: { createdAt: 'desc' },
      take: 50
    });
    
    console.log('📋 Recent Decision-Outcome Learning Patterns:');
    
    const decisions = decisionOutcomePairs.filter(r => r.timeframe === 'DECISION');
    const outcomes = decisionOutcomePairs.filter(r => r.timeframe === 'OUTCOME');
    
    console.log(`  Decisions logged: ${decisions.length}`);
    console.log(`  Outcomes recorded: ${outcomes.length}`);
    
    // Analyze confidence patterns
    const decisionConfidences = decisions
      .filter(d => d.confidenceScore)
      .map(d => d.confidenceScore);
    
    const outcomeConfidences = outcomes
      .filter(o => o.confidenceScore)
      .map(o => o.confidenceScore);
    
    if (decisionConfidences.length > 0) {
      const avgDecisionConf = decisionConfidences.reduce((a, b) => a + b, 0) / decisionConfidences.length;
      console.log(`  Average Decision Confidence: ${avgDecisionConf.toFixed(1)}%`);
    }
    
    if (outcomeConfidences.length > 0) {
      const avgOutcomeConf = outcomeConfidences.reduce((a, b) => a + b, 0) / outcomeConfidences.length;
      console.log(`  Average Outcome Assessment: ${avgOutcomeConf.toFixed(1)}%`);
    }
    
    // Look for specific learning patterns
    console.log('\n🎯 What Made the AI "Think Twice" - Learning Adjustments:');
    
    let learningAdjustments = 0;
    let cautionPatterns = 0;
    let confidenceBoosts = 0;
    
    for (const record of decisionOutcomePairs) {
      try {
        const analysis = JSON.parse(record.analysisData);
        
        // Look for learning-based adjustments
        if (analysis.reasoning && typeof analysis.reasoning === 'string') {
          const reasoning = analysis.reasoning.toLowerCase();
          
          if (reasoning.includes('learn') || reasoning.includes('pattern') || reasoning.includes('historical')) {
            learningAdjustments++;
            console.log(`  📚 Learning-based adjustment: ${record.timeframe} at ${new Date(record.createdAt).toLocaleString()}`);
          }
          
          if (reasoning.includes('caution') || reasoning.includes('risk') || reasoning.includes('careful')) {
            cautionPatterns++;
            console.log(`  ⚠️  Caution pattern: ${record.timeframe} - Confidence: ${record.confidenceScore}%`);
          }
          
          if (reasoning.includes('confident') || reasoning.includes('strong signal')) {
            confidenceBoosts++;
            console.log(`  🚀 Confidence boost: ${record.timeframe} - Confidence: ${record.confidenceScore}%`);
          }
        }
        
        // Check for specific learning insights
        if (analysis.learningInsights) {
          console.log(`  🧠 Learning Insight: ${JSON.stringify(analysis.learningInsights)}`);
        }
        
      } catch (e) {
        // Skip invalid JSON
      }
    }
    
    console.log(`\n📊 Pattern Summary:`);
    console.log(`  Learning-based adjustments: ${learningAdjustments}`);
    console.log(`  Caution patterns identified: ${cautionPatterns}`);
    console.log(`  Confidence boosts: ${confidenceBoosts}`);
    
    // Check for high confidence decisions
    const highConfidenceDecisions = await prisma.ai_learning_data.findMany({
      where: {
        confidenceScore: { gte: 80 },
        timeframe: 'DECISION'
      },
      select: {
        confidenceScore: true,
        analysisData: true,
        createdAt: true
      },
      orderBy: { createdAt: 'desc' },
      take: 5
    });
    
    console.log('\n🔥 High Confidence Decisions (≥80%):');
    highConfidenceDecisions.forEach(decision => {
      try {
        const analysis = JSON.parse(decision.analysisData);
        console.log(`  ${decision.confidenceScore}% - ${analysis.action || 'NO_ACTION'} - ${new Date(decision.createdAt).toLocaleString()}`);
        if (analysis.reasoning) {
          const shortReason = analysis.reasoning.substring(0, 100) + (analysis.reasoning.length > 100 ? '...' : '');
          console.log(`    Reasoning: ${shortReason}`);
        }
      } catch (e) {
        console.log(`  ${decision.confidenceScore}% - Parse error`);
      }
    });
    
    // Look for learning evolution over time
    const earliestRecords = await prisma.ai_learning_data.findMany({
      orderBy: { createdAt: 'asc' },
      take: 10,
      select: {
        confidenceScore: true,
        analysisData: true,
        createdAt: true
      }
    });
    
    const latestRecords = await prisma.ai_learning_data.findMany({
      orderBy: { createdAt: 'desc' },
      take: 10,
      select: {
        confidenceScore: true,
        analysisData: true,
        createdAt: true
      }
    });
    
    const earlyAvgConfidence = earliestRecords
      .filter(r => r.confidenceScore)
      .reduce((sum, r) => sum + r.confidenceScore, 0) / earliestRecords.filter(r => r.confidenceScore).length;
    
    const recentAvgConfidence = latestRecords
      .filter(r => r.confidenceScore)
      .reduce((sum, r) => sum + r.confidenceScore, 0) / latestRecords.filter(r => r.confidenceScore).length;
    
    console.log('\n📈 Learning Evolution:');
    console.log(`  Early confidence average: ${earlyAvgConfidence ? earlyAvgConfidence.toFixed(1) + '%' : 'N/A'}`);
    console.log(`  Recent confidence average: ${recentAvgConfidence ? recentAvgConfidence.toFixed(1) + '%' : 'N/A'}`);
    
    if (earlyAvgConfidence && recentAvgConfidence) {
      const evolution = recentAvgConfidence - earlyAvgConfidence;
      console.log(`  Evolution: ${evolution > 0 ? '+' : ''}${evolution.toFixed(1)}% ${evolution > 0 ? '📈 Improving' : '📉 Adjusting'}`);
    }
    
  } catch (error) {
    console.error('❌ Error analyzing decision patterns:', error);
  } finally {
    await prisma.$disconnect();
  }
}

analyzeDecisionPatterns();
