const getGameScore = (req, res) => {
    const { MongoClient } = require('mongodb');

async function main() {
    const url = process.env.MONGODB_URL;
    const dbName = process.env.MONGO_DB_NAME;
    const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });

    try {
        // Connect to the MongoDB client
        await client.connect();

        // Select the database and collection
        const database = client.db(dbName); // Replace with your database name
        const collection = database.collection('gameData'); // Replace with your collection name

        // Find a single document
        // const user = await collection.findOne({ childId: 'dsfdfgffgfgeg' });

        const { childId, gameName, gameID } = req.body;
            const gameData = await collection.findOne(
                {
                    childId: childId,
                    gameName: gameName,
                    gameID: gameID
                },{
                    sort: {_id: -1}
                }
            );
            // console.log('gameData', gameData);
            if (gameData) {
                res.json({gameData});
            } else {
                res.status(404).json({ message: 'Game data not found' });
            }

        // Output the result
        // if (user) {
        //     console.log('User found:', user);
        // } else {
        //     console.log('No user found with the username "john_doe"');
        // }
    } catch (error) {
        console.error('Error connecting to MongoDB or performing query:', error);
    } finally {
        // Close the connection
        await client.close();
    }
}

main().catch(console.error);

};

module.exports = getGameScore;
  