// Node.js 18+ has built-in fetch

async function testAPI() {
    try {
        // Test login (user already exists)
        console.log('Testing login...');
        const loginResponse = await fetch('http://localhost:12004/api/auth/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                email: 'test@example.com',
                password: 'test123'
            })
        });
        
        if (loginResponse.ok) {
            const loginData = await loginResponse.json();
            console.log('Login successful:', loginData);
            const token = loginData.token;
            
            // Test mood tracking
            console.log('\nTesting mood tracking...');
            const moodResponse = await fetch('http://localhost:12004/api/mood/track', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`
                },
                body: JSON.stringify({
                    moodType: 'happy',
                    intensity: 8,
                    notes: 'Feeling good today!'
                })
            });
            
            if (moodResponse.ok) {
                console.log('Mood tracking successful');
            } else {
                console.log('Mood tracking failed');
            }
            
            // Test notifications
            console.log('\nTesting notifications...');
            const notifResponse = await fetch('http://localhost:12004/api/notifications', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`
                },
                body: JSON.stringify({
                    title: 'Test Notification',
                    message: 'This is a test notification',
                    type: 'info'
                })
            });
            
            if (notifResponse.ok) {
                console.log('Notification creation successful');
                const notifData = await notifResponse.json();
                console.log('Notification data:', notifData);
            } else {
                console.log('Notification creation failed');
                const errorData = await notifResponse.json();
                console.log('Error:', errorData);
            }
            
            // Get notifications
            const getNotifResponse = await fetch('http://localhost:12004/api/notifications', {
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            });
            
            if (getNotifResponse.ok) {
                const notifications = await getNotifResponse.json();
                console.log('Notifications retrieved:', notifications.length);
            }
            
        } else {
            console.log('Registration failed');
        }
        
    } catch (error) {
        console.error('API test error:', error);
    }
}

testAPI();