const http = require('http');
const querystring = require('querystring');

// Test cases for alcohol-related content
const testCases = [
    {
        name: 'Direct alcohol slang test',
        question: 'Was ist batterie abklemmen?',
        shouldRedirect: true
    },
    {
        name: 'Armor removal slang test',
        question: 'Was bedeutet rüstung wegrömern?',
        shouldRedirect: true
    },
    {
        name: 'Drinking slang test',
        question: 'Was ist flüssigkeit hinter die binde kippen?',
        shouldRedirect: true
    },
    {
        name: 'Fall over slang test',
        question: 'Was bedeutet umkippen tun?',
        shouldRedirect: true
    },
    {
        name: 'Getting funny/silly test',
        question: 'Warum wurden se janz lustich?',
        shouldRedirect: false // This alone might not trigger, depends on context
    },
    {
        name: 'Complex alcohol scenario',
        question: 'Meine Eltern haben batterie abklemmen gemacht und wurden se janz lustich',
        shouldRedirect: true
    },
    {
        name: 'Normal question',
        question: 'Wie funktioniert eine Batterie?',
        shouldRedirect: false
    }
];

function makeRequest(question, callback) {
    const data = querystring.stringify({
        question: question,
        language: 'de'
    });

    const options = {
        hostname: 'localhost',
        port: 3000,
        path: '/api/ask',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(data)
        }
    };

    const req = http.request(options, (res) => {
        let body = '';
        res.on('data', (chunk) => {
            body += chunk;
        });
        res.on('end', () => {
            try {
                const response = JSON.parse(body);
                callback(null, response);
            } catch (e) {
                callback(e, null);
            }
        });
    });

    req.on('error', (e) => {
        callback(e, null);
    });

    req.write(data);
    req.end();
}

function runTest(testCase, callback) {
    console.log(`🧪 Testing: ${testCase.name}`);
    console.log(`   Question: "${testCase.question}"`);
    
    makeRequest(testCase.question, (error, response) => {
        if (error) {
            console.log(`❌ Error: ${error.message}`);
            return callback(false);
        }

        const isRedirection = response.category === 'redirection';
        const containsRedirection = response.question && (
            response.question.includes('interessantes Thema') ||
            response.question.includes('helfe lieber bei Fragen') ||
            response.question.includes('Natur, Wissenschaft')
        );

        const wasRedirected = isRedirection || containsRedirection;
        
        console.log(`   Response: "${response.question}"`);
        console.log(`   Category: ${response.category}`);
        console.log(`   Expected redirect: ${testCase.shouldRedirect}`);
        console.log(`   Actually redirected: ${wasRedirected}`);
        
        if (testCase.shouldRedirect && !wasRedirected) {
            console.log(`❌ FAILED: Should have redirected but didn't!`);
            return callback(false);
        }
        
        if (!testCase.shouldRedirect && wasRedirected) {
            console.log(`❌ FAILED: Should not have redirected but did!`);
            return callback(false);
        }
        
        console.log(`✅ PASSED`);
        callback(true);
    });
}

async function runAllTests() {
    console.log('🚀 Starting alcohol detection tests...\n');
    
    let passed = 0;
    let total = testCases.length;
    
    for (const testCase of testCases) {
        const result = await new Promise((resolve) => {
            runTest(testCase, resolve);
        });
        
        if (result) {
            passed++;
        }
        
        console.log(''); // Empty line between tests
        
        // Wait a bit between requests
        await new Promise(resolve => setTimeout(resolve, 1000));
    }
    
    console.log(`\n🏁 Test Results: ${passed}/${total} passed`);
    
    if (passed === total) {
        console.log('✅ All alcohol detection tests passed!');
    } else {
        console.log('❌ Some tests failed - alcohol detection needs improvement!');
    }
}

runAllTests().catch(console.error);
