#!/usr/bin/env node

/**
 * Test Script for Repetition Acknowledgment
 * Tests the "already said that" detection and handling
 */

const testCases = [
    {
        name: "German Repetition Test 1",
        question: "Was ist UV-Strahlung?",
        answer: "Hab ich schon gesagt - das ist Licht von der Sonne",
        originalTopic: "Warum bekommt man Sonnenbrand?",
        language: "de",
        previousAnswers: ["Das ist Licht von der Sonne"],
        expectedBehavior: "Should apologize and ask a new advancing question"
    },
    {
        name: "English Repetition Test 1",
        question: "What is UV radiation?",
        answer: "I already said that - it's light from the sun",
        originalTopic: "Why do people get sunburned?",
        language: "en",
        previousAnswers: ["It's light from the sun"],
        expectedBehavior: "Should apologize and ask a new advancing question"
    },
    {
        name: "German Repetition Test 2",
        question: "Wie fliegen Vögel?",
        answer: "Das sagte ich schon - mit ihren Flügeln",
        originalTopic: "Wie kann ein Vogel fliegen?",
        language: "de",
        previousAnswers: ["Mit ihren Flügeln"],
        expectedBehavior: "Should acknowledge repetition and build on previous answer"
    },
    {
        name: "English Repetition Test 2",
        question: "How do birds fly?",
        answer: "I told you that already - with their wings",
        originalTopic: "How can a bird fly?",
        language: "en",
        previousAnswers: ["With their wings"],
        expectedBehavior: "Should acknowledge repetition and build on previous answer"
    },
    {
        name: "German Polite Repetition",
        question: "Was passiert bei Sonnenbrand?",
        answer: "Habe ich doch schon erwähnt - die Haut wird rot",
        originalTopic: "Warum bekommt man Sonnenbrand?",
        language: "de",
        previousAnswers: ["Die Haut wird rot"],
        expectedBehavior: "Should thank child and ask advancing question"
    }
];

async function testRepetitionHandling() {
    console.log('🔄 Testing Repetition Acknowledgment Handling\n');
    
    let passedTests = 0;
    let totalTests = testCases.length;
    
    for (let i = 0; i < testCases.length; i++) {
        const testCase = testCases[i];
        console.log(`\n${'='.repeat(60)}`);
        console.log(`TEST ${i + 1}: ${testCase.name}`);
        console.log(`${'='.repeat(60)}`);
        console.log(`Question: "${testCase.question}"`);
        console.log(`Answer: "${testCase.answer}"`);
        console.log(`Expected: ${testCase.expectedBehavior}`);
        console.log(`\nSending request...`);
        
        try {
            const response = await fetch('http://localhost:3002/api/respond-to-answer', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    answer: testCase.answer,
                    question: testCase.question,
                    originalTopic: testCase.originalTopic,
                    language: testCase.language,
                    sessionId: `test_repetition_${i}`
                })
            });
            
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            
            const data = await response.json();
            
            if (data.success) {
                console.log(`\n✅ Response received:`);
                console.log(`"${data.response}"`);
                
                // Analyze response for repetition acknowledgment
                const aiResponse = data.response.toLowerCase();
                
                // Check for apology/acknowledgment phrases
                const acknowledgmentPhrases = testCase.language === 'de' 
                    ? ['du hast recht', 'schon gesagt', 'danke', 'erinnerst', 'entschuldigung', 'stimmt']
                    : ['you\'re right', 'already said', 'thanks', 'remind', 'sorry', 'correct'];
                
                const hasAcknowledgment = acknowledgmentPhrases.some(phrase => 
                    aiResponse.includes(phrase)
                );
                
                // Check if response builds on previous answer or asks new question
                const hasBuildingOrNewQuestion = aiResponse.includes('?') && aiResponse.length > 30;
                
                if (hasAcknowledgment && hasBuildingOrNewQuestion) {
                    console.log(`✅ PASSED: Repetition acknowledged and new question provided`);
                    passedTests++;
                } else if (hasAcknowledgment) {
                    console.log(`⚠️  PARTIAL: Acknowledged repetition but may lack advancing question`);
                    passedTests += 0.5;
                } else {
                    console.log(`❌ FAILED: Repetition not properly acknowledged`);
                }
            } else {
                console.log(`❌ FAILED: ${data.error || 'Unknown error'}`);
            }
        } catch (error) {
            console.log(`❌ FAILED: ${error.message}`);
        }
        
        // Wait between requests
        await new Promise(resolve => setTimeout(resolve, 1000));
    }
    
    console.log(`\n${'='.repeat(60)}`);
    console.log(`🎯 FINAL RESULTS: ${passedTests}/${totalTests} tests passed`);
    console.log(`${'='.repeat(60)}`);
    
    if (passedTests >= totalTests * 0.8) {
        console.log(`🎉 Most tests passed! Repetition handling working well.`);
        return true;
    } else {
        console.log(`⚠️  Many tests failed. Review repetition detection logic.`);
        return false;
    }
}

// Run tests if this script is executed directly
if (require.main === module) {
    testRepetitionHandling().catch(console.error);
}

module.exports = { testRepetitionHandling };
