// Test script for handling child's emotional frustration
const fetch = require('node-fetch');

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

async function testEmotionalFrustration() {
    console.log('🧪 Testing emotional frustration handling...\n');
    
    try {
        // Step 1: Child expresses initial frustration
        console.log('1️⃣ Child says "Keiner versteht mich zu Hause!"...');
        const step1 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "Keiner versteht mich zu Hause!",
                question: "Was denkst du über Kommunikation?",
                originalTopic: "Wie funktioniert Kommunikation?",
                language: "de",
                sessionId: "test-emotional-123"
            })
        });
        
        const step1Data = await step1.json();
        console.log('🤖 AI Response 1:', step1Data.response || 'No response');
        
        // Step 2: Child escalates with blame
        console.log('\n2️⃣ Child escalates: "Ich drücke mich sehr gewählt aus. Das Problem sind die anderen!!!"...');
        const step2 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "Ich drücke mich sehr gewählt aus. Das Problem sind die anderen!!!",
                question: "Was denkst du, warum es manchmal schwer ist, sich auszudrücken?",
                originalTopic: "Wie funktioniert Kommunikation?",
                language: "de",
                sessionId: "test-emotional-123"
            })
        });
        
        const step2Data = await step2.json();
        console.log('🤖 AI Response 2:', step2Data.response || 'No response');
        
        // Analyze the responses
        const response1 = step1Data.response || '';
        const response2 = step2Data.response || '';
        
        const handlesEmotionWell1 = response1.includes('frustrierend') || 
                                   response1.includes('verstehen') || 
                                   response1.includes('schwer') ||
                                   response1.includes('fühlen');
        
        const handlesEmotionWell2 = response2.includes('verstehen') || 
                                   response2.includes('frustriert') || 
                                   response2.includes('ärgerlich') ||
                                   response2.includes('anders angehen') ||
                                   !response2.includes('interessanter Punkt');
        
        console.log('\n📊 ANALYSIS:');
        console.log('Response 1 acknowledges emotion:', handlesEmotionWell1 ? '✅' : '❌');
        console.log('Response 2 handles escalation well:', handlesEmotionWell2 ? '✅' : '❌');
        
        if (handlesEmotionWell1 && handlesEmotionWell2) {
            console.log('\n🎉 EXCELLENT: AI handles emotional frustration well!');
        } else {
            console.log('\n⚠️  NEEDS IMPROVEMENT: AI should better handle emotional states');
            if (!handlesEmotionWell1) {
                console.log('   - First response should acknowledge frustration more clearly');
            }
            if (!handlesEmotionWell2) {
                console.log('   - Second response should avoid analytical questions when child is escalating');
            }
        }
        
    } catch (error) {
        console.error('❌ Test failed:', error.message);
    }
}

// Run the test
testEmotionalFrustration();
