// src/test.js
const PlayerService = require('./services/PlayerService');

async function testPlayerService() {
    console.log('Testing PlayerService...');
    
    const playerService = new PlayerService();
    
    try {
        // Test user lookup
        console.log('\nTesting user lookup...');
        const userData = await playerService.findUserByUsername('uthcowboys');
        console.log('User data received:', !!userData);
        console.log('Success:', userData?.success);
        
        if (userData && userData.success) {
            const playerData = JSON.parse(userData.player_data);
            console.log('\nBasic user info:');
            console.log('Username:', playerData.username);
            console.log('Level:', playerData.profile?.level);
            console.log('Country:', playerData.profile?.country);
            
            // Test matches
            console.log('\nRecent matches:');
            const matches = Object.values(playerData.matches || {})
                .sort((a, b) => new Date(b.start_time) - new Date(a.start_time))
                .slice(0, 3);
            
            matches.forEach(match => {
                console.log(`- ${match.game_name} (${match.game_mode}): ${match.score} on ${new Date(match.start_time).toLocaleDateString()}`);
            });
            
            // Test stats
            console.log('\nGame stats:');
            const games = playerData.stats?.games || {};
            Object.entries(games).forEach(([gameName, gameData]) => {
                console.log(`\n${gameName}:`);
                Object.entries(gameData).forEach(([mode, stats]) => {
                    if (mode !== 'Overall') {
                        console.log(`  ${mode}:`);
                        console.log(`    Matches: ${stats.matches}`);
                        console.log(`    Win Rate: ${stats.win_rate}`);
                    }
                });
            });
        }
    } catch (error) {
        console.error('Test failed:', error);
    }
}

// Run tests
console.log('Starting tests...\n');
testPlayerService().then(() => {
    console.log('\nTests completed.');
}).catch(error => {
    console.error('Tests failed:', error);
});