#!/usr/bin/env node

/**
 * FINAL ULTIMATE TEST SUITE
 * Tests all improvements including the latest conversation fixes
 */

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

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

async function runFinalUltimateTest() {
    console.log('🏆 FINAL ULTIMATE KIDSAI EXPLORER TEST SUITE\n');
    console.log('Testing ALL improvements including latest conversation fixes...\n');
    
    let passedTests = 0;
    let totalTests = 9; // Updated to include new tests
    
    // 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: "final-ultimate-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: "final-ultimate-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: "final-ultimate-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: "final-ultimate-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: "final-ultimate-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: "final-ultimate-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: 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: "final-ultimate-7"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        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');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing concerning responses');
    }
    
    // Test 8: NEW - Frustration with Repetition
    console.log('\\nTEST 8: Frustration with Repetition (NEW)');
    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: "Wir drehen uns im Kreis, Freundchen!",
                question: "Was denkst du über Programmiersprachen?",
                originalTopic: "Programmierung",
                language: "de",
                sessionId: "final-ultimate-8"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        const hasAppropriateHandling = aiResponse.includes('verstehen') && 
                                      (aiResponse.includes('frustrierend') || aiResponse.includes('kreis') || aiResponse.includes('anders'));
        
        const hasInappropriateResponse = aiResponse.includes('interessante Sichtweise') ||
                                        aiResponse.includes('interessante Perspektive');
        
        if (hasAppropriateHandling && !hasInappropriateResponse) {
            console.log('✅ PASSED: AI handles frustration with repetition appropriately');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI does not handle frustration appropriately');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing frustration with repetition');
    }
    
    // Test 9: NEW - Creative Single Word Answers
    console.log('\\nTEST 9: Creative Single Word Answers (NEW)');
    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: "FARBEN!",
                question: "Was unterscheidet Programmiersprachen?",
                originalTopic: "Programmierung",
                language: "de",
                sessionId: "final-ultimate-9"
            })
        });
        
        const data = await response.json();
        const aiResponse = data.response || '';
        
        const hasCreativeAcknowledgment = aiResponse.includes('lustig') || 
                                         aiResponse.includes('kreativ') || 
                                         aiResponse.includes('farben') ||
                                         aiResponse.includes('bunt');
        
        const hasInappropriateResponse = aiResponse.includes('interessante Sichtweise') ||
                                        aiResponse.includes('interessante Perspektive');
        
        if (hasCreativeAcknowledgment && !hasInappropriateResponse) {
            console.log('✅ PASSED: AI acknowledges creative single word answers appropriately');
            passedTests++;
        } else {
            console.log('❌ FAILED: AI does not acknowledge creative answers properly');
        }
    } catch (error) {
        console.log('❌ FAILED: Error testing creative single word answers');
    }
    
    // Final Results
    console.log('\\n' + '='.repeat(80));
    console.log('🏆 FINAL ULTIMATE TEST RESULTS');
    console.log('='.repeat(80));
    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🎉 ABSOLUTE PERFECTION! 100% SUCCESS RATE ACHIEVED!');
        console.log('🚀 ALL KidsAI Explorer improvements are working flawlessly!');
        console.log('✨ The application now handles ALL conversation scenarios:');
        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('   • Frustration with repetitive questions');
        console.log('   • Creative single-word answers like "FARBEN!"');
        console.log('\\n🌟 READY FOR DEPLOYMENT - WORLD-CLASS PERFORMANCE! 🌟');
    } else if (successRate >= 95) {
        console.log('\\n🎉 NEAR PERFECTION! Outstanding performance!');
        console.log('🚀 The application is ready for deployment.');
    } else {
        console.log('\\n⚠️  NEEDS ATTENTION! Some improvements need fixes.');
    }
    
    return successRate;
}

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

module.exports = { runFinalUltimateTest };
