#!/usr/bin/env node

/**
 * Live Component Data Test - Check what the frontend component is actually receiving
 */

const baseUrl = 'http://localhost:9001';

async function testComponentData() {
  console.log('🔍 Testing what the EnhancedAILearningPanel component receives...\n');

  try {
    // Test the three API endpoints the component calls
    console.log('1️⃣ Testing /api/automation/learning-status:');
    const learningResponse = await fetch(`${baseUrl}/api/automation/learning-status`);
    const learningData = await learningResponse.json();
    console.log(`   - Success: ${learningData.success}`);
    console.log(`   - Learning Enabled: ${learningData.learningSystem?.enabled}`);
    console.log(`   - Message: ${learningData.learningSystem?.message}`);

    console.log('\n2️⃣ Testing /api/automation/status:');
    const statusResponse = await fetch(`${baseUrl}/api/automation/status`);
    const statusData = await statusResponse.json();
    console.log(`   - Is Active: ${statusData.isActive}`);
    console.log(`   - Mode: ${statusData.mode}`);
    console.log(`   - Symbol: ${statusData.symbol}`);

    console.log('\n3️⃣ Testing /api/ai-learning-status:');
    const aiLearningResponse = await fetch(`${baseUrl}/api/ai-learning-status`);
    const aiLearningData = await aiLearningResponse.json();
    console.log(`   - Success: ${aiLearningData.success}`);
    console.log(`   - Total Analyses: ${aiLearningData.data?.totalAnalyses}`);
    console.log(`   - Phase: ${aiLearningData.data?.phase}`);
    console.log(`   - Statistics Object:`, JSON.stringify(aiLearningData.data?.statistics, null, 2));

    // Check what the component logic would do
    console.log('\n🧠 Component Logic Analysis:');
    const aiData = aiLearningData.success ? aiLearningData.data : {
      totalAnalyses: 0,
      totalDecisions: 0,
      // ... fallback data
    };

    console.log(`   - Component would use: ${aiLearningData.success ? 'REAL API DATA' : 'FALLBACK DATA'}`);
    console.log(`   - Total Analyses: ${aiData.totalAnalyses}`);
    console.log(`   - Has Learning Data: ${(aiData.totalAnalyses || 0) > 0}`);
    console.log(`   - Should Show Active: ${(aiData.totalAnalyses || 0) > 0 || (aiData.totalDecisions || 0) > 0}`);

    // Check browser cache issues
    console.log('\n🌐 Browser Cache Check:');
    const headers = {
      'Cache-Control': 'no-cache',
      'Pragma': 'no-cache'
    };
    const pageResponse = await fetch(`${baseUrl}/automation-v2`, { headers });
    console.log(`   - Page loads: ${pageResponse.ok ? 'YES' : 'NO'}`);
    console.log(`   - Content-Type: ${pageResponse.headers.get('content-type')}`);
    
    // Check if it's a Next.js page
    const pageText = await pageResponse.text();
    const hasLearningComponent = pageText.includes('AI Learning System');
    const hasReactHydration = pageText.includes('__NEXT_DATA__');
    
    console.log(`   - Has AI Learning Component: ${hasLearningComponent ? 'YES' : 'NO'}`);
    console.log(`   - Has React Hydration: ${hasReactHydration ? 'YES' : 'NO'}`);

    console.log('\n🎯 Diagnosis:');
    if (!statusData.isActive) {
      console.log('   ⚠️  STOP BUTTON: Hidden because automation is not active');
      console.log('   💡 To see stop button: Start automation first');
    }
    
    if (aiLearningData.success && aiData.totalAnalyses > 9000) {
      console.log('   ✅ AI DATA: Component should show real learning data');
      console.log('   💡 If still showing old data: Clear browser cache (Ctrl+F5)');
    }

  } catch (error) {
    console.error('❌ Test failed:', error.message);
  }
}

testComponentData().catch(console.error);
