var MongoClient = require('mongodb').MongoClient;
// const AWS = require('aws-sdk');

const saveGameScore = (req, res) => {
    const url = process.env.MONGODB_URL;
    const dbName = process.env.MONGO_DB_NAME;
    const client = new MongoClient(url, { useUnifiedTopology: true });
    client.connect((err) => {
        if (err) {
            console.error('Failed to connect to the server', err);
            return;
        }
        // console.log('Connected successfully to server');
        const db = client.db(dbName);
        const collection = db.collection('questions');
        // const data = req.body;
        const { question, option1, option2, option3, option4, correctAnswer, moduleId } = req.body;
        const data = {
            question: question,
            option1: option1,
            option2: option2,
            option3: option3,
            option4: option4,
            correctAnswer: correctAnswer,
            moduleId: moduleId
        };

        collection.insertOne(data, (err, result) => {
            if (err) {
                console.error('Failed to insert document', err);
            } else {
                // console.log('Document inserted with _id: ', result.insertedId);
            }
            client.close((err) => {
                if (err) {
                    console.error('Failed to close connection', err);
                } else {
                    console.log('Connection closed');
                    
                    res.send(result.insertedId);
                }
            });
        });
    });
  };
  
  module.exports = saveGameScore;
  