const mysql = require("mysql2");

const generateQuestions = (req, res) => {
    const connection = mysql.createConnection({
    host: process.env.MARIA_HOST,
    user: process.env.MARIA_USER,
    password: process.env.MARIA_PASS,
    database: process.env.MARIA_DBNM
    });

    connection.connect((err) => {
    if (err) {
        console.error('Error connecting to the database:', err);
        return;
    }
    console.log('Connected to the MariaDB database.');
    });

    const { questions } = req.body;
    res.send( questions );
    questions.forEach(question => {
        const { questionText, options, correctAnswer } = question;
        const sql = 'INSERT INTO quiz_questions (questionText, option1, option2, option3, option4, correctAnswer) VALUES (?, ?, ?, ?, ?, ?)';
        db.query(sql, [questionText, options[0], options[1], options[2], options[3], correctAnswer], (err, result) => {
            if (err) {
                console.error('Error inserting question:', err);
                res.status(500).send('Error inserting question');
                return;
            }
        });
    });
    
    res.status(200).send('Questions added successfully');
}
module.exports = generateQuestions;