#!/usr/bin/env node

/**
 * Ultimate Comprehensive Test Suite for KidsAI Explorer
 * Tests all improvements including the new concerning response handling
 */

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

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

async function runUltimateTest() {
    console.log('🎯 ULTIMATE KIDSAI EXPLORER TEST SUITE\n');
    console.log('Testing all improvements + new concerning response handling...\n');
    
    let passedTests = 0;
    let totalTests = 7; // Updated to include concerning response test
    
    // Test 1: "Nein" Response Bug Fix
    console.log('TEST 1: "Nein" Response Bug Fix');
    console.log('=' .repeat(40));
    try {
        const response = 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?",
                originalTopic: "Wie entstehen Polarlichter?",
                language: "de",
                sessionId: "ultimate-test-1"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        if (aiResponse.includes('Das ist') && (aiResponse.includes('in Ordnung') || aiResponse.includes('okay'))) {
            console.log('✅ PASSED: AI responds appropriately to "nein"');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI still has "nein" response bug');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing "nein" response');
    }
    
    // Test 2: Next Fundamental Endpoint
    console.log('\nTEST 2: Next Fundamental Endpoint');
    console.log('=' .repeat(40));
    try {
        const response = await fetch(`${BASE_URL}/api/next-fundamental`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                currentTopic: "Wie entstehen Polarlichter?",
                language: "de",
                sessionId: "ultimate-test-2"
            })
        });
        
        const data = await response.json();
        
        if (data.success && data.guidance && data.guidance.steps) {
            console.log('✅ PASSED: Next fundamental endpoint works');
            passedTests++;
        } else {
            console.log('❌ FAILED: Next fundamental endpoint not working');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing next fundamental');
    }
    
    // Test 3: Humor Acknowledgment
    console.log('\nTEST 3: Humor Acknowledgment');
    console.log('=' .repeat(40));
    try {
        const response = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "per fax",
                question: "Wie können Vögel miteinander kommunizieren?",
                originalTopic: "Wie fliegen Vögel?",
                language: "de",
                sessionId: "ultimate-test-3"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        if (aiResponse.includes('lustig') || aiResponse.includes('kreativ') || aiResponse.includes('Haha')) {
            console.log('✅ PASSED: AI acknowledges humor appropriately');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI does not acknowledge humor');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing humor');
    }
    
    // Test 4: Emotional Support
    console.log('\nTEST 4: Emotional Support');
    console.log('=' .repeat(40));
    try {
        const response = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "Das ist doof und verstehe ich nicht",
                question: "Was ist UV-Strahlung?",
                originalTopic: "Warum bekommt man Sonnenbrand?",
                language: "de",
                sessionId: "ultimate-test-4"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        if (aiResponse.includes('verstehen') || aiResponse.includes('okay') || aiResponse.includes('anders')) {
            console.log('✅ PASSED: AI provides emotional support');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI does not provide emotional support');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing emotional support');
    }
    
    // Test 5: Repetition Handling
    console.log('\nTEST 5: Repetition Handling');
    console.log('=' .repeat(40));
    try {
        const response = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "Hab ich schon gesagt - das ist Licht von der Sonne",
                question: "Was ist UV-Strahlung?",
                originalTopic: "Warum bekommt man Sonnenbrand?",
                language: "de",
                sessionId: "ultimate-test-5"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        if (aiResponse.includes('recht') || aiResponse.includes('schon gesagt') || aiResponse.includes('Danke')) {
            console.log('✅ PASSED: AI handles repetition appropriately');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI does not handle repetition');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing repetition');
    }
    
    // Test 6: Basic Conversation Flow
    console.log('\nTEST 6: Basic Conversation Flow');
    console.log('=' .repeat(40));
    try {
        const response = await fetch(`${BASE_URL}/api/ask`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                question: "Wie entstehen Polarlichter?",
                language: "de",
                sessionId: "ultimate-test-6"
            })
        });
        
        const data = await response.json();
        
        if (data.success && data.guidance && data.guidance.steps && data.guidance.steps.length > 0) {
            console.log('✅ PASSED: Basic conversation flow works');
            passedTests++;
        } else {
            console.log('❌ FAILED: Basic conversation flow broken');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing basic conversation');
    }
    
    // Test 7: NEW - Concerning Response Handling
    console.log('\nTEST 7: Concerning Response Handling');
    console.log('=' .repeat(40));
    try {
        const response = await fetch(`${BASE_URL}/api/respond-to-answer`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                answer: "durch schläge und tritte",
                question: "Wie können Menschen miteinander kommunizieren?",
                originalTopic: "Kommunikation",
                language: "de",
                sessionId: "ultimate-test-7"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        // Check for appropriate handling without inappropriate acknowledgment
        const hasAppropriateHandling = aiResponse.includes('Das ist nicht das, worüber wir sprechen') ||
                                      aiResponse.includes('Lass uns über') ||
                                      aiResponse.includes('freundliche');
        
        const hasInappropriateAcknowledgment = aiResponse.includes('interessante Sichtweise') ||
                                               aiResponse.includes('das ist interessant');
        
        if (hasAppropriateHandling && !hasInappropriateAcknowledgment) {
            console.log('✅ PASSED: AI handles concerning responses appropriately');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI does not handle concerning responses properly');
            console.log('Response:', aiResponse);
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing concerning responses');
    }
    
    // Final Results
    console.log('\n' + '='.repeat(70));
    console.log('🎯 ULTIMATE TEST RESULTS');
    console.log('='.repeat(70));
    console.log(`✅ Passed: ${passedTests}/${totalTests} tests`);
    console.log(`❌ Failed: ${totalTests - passedTests}/${totalTests} tests`);
    
    const successRate = (passedTests / totalTests) * 100;
    console.log(`📊 Success Rate: ${successRate.toFixed(1)}%`);
    
    if (successRate === 100) {
        console.log('\n🎉 PERFECT! 100% SUCCESS RATE ACHIEVED!');
        console.log('🚀 All KidsAI Explorer improvements are working flawlessly!');
        console.log('✨ The application now handles:');
        console.log('   • "Nein" responses with encouragement');
        console.log('   • Next fundamental navigation');
        console.log('   • Humor and creativity acknowledgment');
        console.log('   • Emotional support and frustration handling');
        console.log('   • Repetition detection and respect');
        console.log('   • Basic educational conversation flow');
        console.log('   • Concerning/inappropriate response redirection');
        console.log('\n🌟 READY FOR DEPLOYMENT WITH CHILDREN! 🌟');
    } else if (successRate >= 85) {
        console.log('\n🎉 EXCELLENT! Near-perfect performance!');
        console.log('🚀 The application is ready for use with minor improvements needed.');
    } else {
        console.log('\n⚠️  NEEDS ATTENTION! Several improvements need fixes.');
    }
    
    return successRate;
}

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

module.exports = { runUltimateTest };
