#!/usr/bin/env node

/**
 * Test Script for the Exact Problematic Conversation
 * Simulates the conversation flow that showed issues with response handling
 */

const fetch = require('node-fetch');

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

async function testProblematicConversation() {
    console.log('🔍 Testing the EXACT Problematic Conversation Flow\n');
    console.log('Simulating the conversation that showed response issues...\n');
    
    const sessionId = `problematic-conversation-${Date.now()}`;
    let allPassed = true;
    
    try {
        // Step 1: Start conversation about programming languages
        console.log('1️⃣ Starting conversation about programming languages...');
        const step1 = await fetch(`${BASE_URL}/api/ask`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                question: "Warum haben Menschen Programmiersprachen erfunden?",
                language: "de",
                sessionId: sessionId
            })
        });
        
        const step1Data = await step1.json();
        console.log('🤖 AI Response:', step1Data.guidance?.steps?.[0]?.text || 'No response');
        
        // Step 2: Child expresses potential boredom
        console.log('\\n2️⃣ Child says "War ihnen vielleicht langweilig?"...');
        const step2 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "War ihnen vielleicht langweilig?",
                question: "Warum haben Menschen Programmiersprachen erfunden?",
                originalTopic: "Programmiersprachen",
                language: "de",
                sessionId: sessionId
            })
        });
        
        const step2Data = await step2.json();
        const response2 = step2Data.response || '';
        console.log('🤖 AI Response:', response2);
        
        // Check if boredom is handled appropriately
        if (response2.includes('verstehen') || response2.includes('okay')) {
            console.log('✅ GOOD: Boredom handled appropriately');
        } else {
            console.log('❌ ISSUE: Boredom not handled well');
            allPassed = false;
        }
        
        // Step 3: Child gives thoughtful answer about AI
        console.log('\\n3️⃣ Child says "Eine KI, die mich versteht"...');
        const step3 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "Eine KI, die mich versteht",
                question: "Was für eine App würdest du erstellen?",
                originalTopic: "Programmiersprachen",
                language: "de",
                sessionId: sessionId
            })
        });
        
        const step3Data = await step3.json();
        const response3 = step3Data.response || '';
        console.log('🤖 AI Response:', response3);
        
        // Check if thoughtful answer is acknowledged properly
        if (response3.includes('interessante Sichtweise')) {
            console.log('❌ ISSUE: Still using generic "interessante Sichtweise"');
            allPassed = false;
        } else if (response3.includes('toll') || response3.includes('gut') || response3.includes('idee')) {
            console.log('✅ GOOD: Thoughtful answer acknowledged appropriately');
        } else {
            console.log('⚠️  NEUTRAL: Response could be more enthusiastic');
        }
        
        // Step 4: Child expresses frustration with repetition
        console.log('\\n4️⃣ 🎯 CRITICAL TEST: Child says "Wir drehen uns im Kreis, Freundchen!"...');
        const step4 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "Wir drehen uns im Kreis, Freundchen!",
                question: "Was glaubst du, wie viele verschiedene Programmiersprachen es gibt?",
                originalTopic: "Programmiersprachen",
                language: "de",
                sessionId: sessionId
            })
        });
        
        const step4Data = await step4.json();
        const response4 = step4Data.response || '';
        console.log('🤖 AI Response:', response4);
        
        // Check if frustration is handled appropriately
        if (response4.includes('interessante Sichtweise') || response4.includes('interessante Perspektive')) {
            console.log('❌ CRITICAL ISSUE: Still using inappropriate "interessante Sichtweise" for frustration');
            allPassed = false;
        } else if (response4.includes('verstehen') && (response4.includes('frustrierend') || response4.includes('kreis') || response4.includes('anders'))) {
            console.log('✅ EXCELLENT: Frustration handled appropriately');
        } else {
            console.log('❌ ISSUE: Frustration not handled well');
            allPassed = false;
        }
        
        // Step 5: Child gives creative single-word answer
        console.log('\\n5️⃣ 🎯 CREATIVE TEST: Child says "FARBEN!"...');
        const step5 = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "FARBEN!",
                question: "Was unterscheidet eine Programmiersprache von einer normalen Sprache?",
                originalTopic: "Programmiersprachen",
                language: "de",
                sessionId: sessionId
            })
        });
        
        const step5Data = await step5.json();
        const response5 = step5Data.response || '';
        console.log('🤖 AI Response:', response5);
        
        // Check if creative answer is acknowledged properly
        if (response5.includes('interessante Sichtweise') || response5.includes('interessante Perspektive')) {
            console.log('❌ CRITICAL ISSUE: Still using generic "interessante Sichtweise" for creative answer');
            allPassed = false;
        } else if (response5.includes('lustig') || response5.includes('kreativ') || response5.includes('farben') || response5.includes('bunt')) {
            console.log('✅ EXCELLENT: Creative answer acknowledged appropriately');
        } else {
            console.log('❌ ISSUE: Creative answer not acknowledged well');
            allPassed = false;
        }
        
        console.log('\\n' + '='.repeat(70));
        console.log('🎯 PROBLEMATIC CONVERSATION TEST RESULTS');
        console.log('='.repeat(70));
        
        if (allPassed) {
            console.log('🎉 ALL ISSUES FIXED! The problematic conversation now works perfectly!');
            console.log('✨ Key improvements verified:');
            console.log('   • Boredom expressions handled with understanding');
            console.log('   • Thoughtful answers acknowledged appropriately');
            console.log('   • Frustration with repetition recognized and addressed');
            console.log('   • Creative answers celebrated instead of dismissed');
            console.log('   • No more inappropriate "interessante Sichtweise" responses');
            console.log('\\n🚀 The conversation flow is now child-friendly and engaging!');
        } else {
            console.log('⚠️  Some issues remain. The AI needs further improvements.');
        }
        
        return allPassed;
        
    } catch (error) {
        console.error('❌ Test failed:', error.message);
        return false;
    }
}

// Run the test
if (require.main === module) {
    testProblematicConversation().catch(console.error);
}

module.exports = { testProblematicConversation };
