// Test script to simulate the conversation flow that was problematic
const fetch = require('node-fetch');

const BASE_URL = 'http://localhost:3002';

async function testConversation() {
    console.log('🧪 Testing KidsAI conversation flow...\n');
    
    try {
        // Step 1: Start conversation about northern lights
        console.log('1️⃣ Starting conversation about Polarlichter...');
        const step1 = await fetch(`${BASE_URL}/api/ask`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                question: "Wie entstehen Polarlichter?",
                language: "de"
            })
        });
        
        const step1Data = await step1.json();
        console.log('🤖 AI Response:', step1Data.guidance?.steps?.[0]?.text || 'No response');
        
        // Step 2: Child says "das weis ich nicht" 
        console.log('\n2️⃣ Child says "das weis ich nicht"...');
        const step2 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "das weis ich nicht",
                question: "Was denkst du, was der Sonnenwind ist?",
                originalTopic: "Wie entstehen Polarlichter?",
                language: "de",
                sessionId: "test-session-123"
            })
        });
        
        const step2Data = await step2.json();
        console.log('🤖 AI Response:', step2Data.response || 'No response');
        
        // Step 3: Child says "vielleicht durch die sonne?"
        console.log('\n3️⃣ Child says "vielleicht durch die sonne?"...');
        const step3 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "vielleicht durch die sonne ?",
                question: "Wo könnte das Magnetfeld der Erde herkommen?",
                originalTopic: "Wie entstehen Polarlichter?",
                language: "de",
                sessionId: "test-session-123"
            })
        });
        
        const step3Data = await step3.json();
        console.log('🤖 AI Response:', step3Data.response || 'No response');
        
        // Step 4: The critical test - Child says "nein" 
        console.log('\n4️⃣ 🎯 CRITICAL TEST: Child says "nein" (this was the bug)...');
        const step4 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "nein",
                question: "Weißt du, wie das Magnetfeld der Erde entsteht und welche Rolle der Erdkern dabei spielt?",
                originalTopic: "Wie entstehen Polarlichter?",
                language: "de",
                sessionId: "test-session-123"
            })
        });
        
        const step4Data = await step4.json();
        console.log('🤖 AI Response:', step4Data.response || 'No response');
        
        // Check if the response is appropriate
        const response = step4Data.response || '';
        const isAppropriate = !response.includes('interessante Frage') && 
                             (response.includes('in Ordnung') || 
                              response.includes('ehrlich') || 
                              response.includes('verstehen') ||
                              response.includes('Schritt für Schritt'));
        
        console.log('\n📊 RESULT ANALYSIS:');
        console.log('✅ Response does NOT contain "interessante Frage":', !response.includes('interessante Frage'));
        console.log('✅ Response contains appropriate language:', isAppropriate);
        console.log('🎉 BUG FIX STATUS:', isAppropriate ? 'SUCCESS! ✅' : 'STILL BROKEN ❌');
        
        if (isAppropriate) {
            console.log('\n🎊 EXCELLENT! The fix is working correctly!');
            console.log('🔧 When a child says "nein", the AI now responds appropriately');
            console.log('📚 Instead of "That\'s an interesting question!", it acknowledges the "no" properly');
        } else {
            console.log('\n❌ The bug still exists. Need further investigation.');
        }
        
    } catch (error) {
        console.error('❌ Test failed:', error.message);
    }
}

// Run the test
testConversation();
