const mysql = require("mysql2");
const questionList = (req, res) => {
    // res.send(req.query.doa); //get
    // res.send(req.body.doa);  //post
    function generateUniqueId() {
      let timestamp = new Date().getTime().toString();
      let randomNumber = Math.floor(Math.random() * 100000).toString();
      let uniqueId = timestamp + randomNumber;

      if (uniqueId.length > 10) {
          uniqueId = uniqueId.substring(0, 10);
      } else if (uniqueId.length < 10) {
          while (uniqueId.length < 10) {
              uniqueId += Math.floor(Math.random() * 10).toString();
          }
      }

      return uniqueId;
  }

    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 data = req.body;

      const query = `SELECT * FROM quiz_questions LIMIT 6`;  
      connection.query(query, (err, results) => {
          if (err) {
            console.error('Error inserting data:', err);
            res.status(500).send('Internal Server Error');
            return;
          }
          const uniqueId = generateUniqueId();
          results.forEach(obj => {
            obj.newQuizId = uniqueId
          })

          
          res.status(200).json(results);
      });
};
  
module.exports = questionList;
//   {
//     id: 1,
//     question: "What is the capital of France?",
//     options: ["Berlin", "Madrid", "Paris", "Rome"],
//     answer: "Paris",
//   },
//   {
//     id: 2,
//     question: "Which planet is known as the Red Planet?",
//     options: ["Earth", "Mars", "Jupiter", "Saturn"],
//     answer: "Mars",
//   },
//   {
//     id: 3,
//     question: "What is the chemical symbol for gold?",
//     options: ["Au", "Ag", "Pb", "Fe"],
//     answer: "Au",
//   },
//   {
//     id: 4,
//     question: "Who wrote 'To Kill a Mockingbird'?",
//     options: ["Harper Lee", "Mark Twain", "Ernest Hemingway", "J.K. Rowling"],
//     answer: "Harper Lee",
//   },
//   {
//     id: 5,
//     question: "What is the largest ocean on Earth?",
//     options: ["Atlantic Ocean", "Indian Ocean", "Arctic Ocean", "Pacific Ocean"],
//     answer: "Pacific Ocean",
//   },
//   {
//     id: 6,
//     question: "Which element has the atomic number 1?",
//     options: ["Helium", "Hydrogen", "Oxygen", "Carbon"],
//     answer: "Hydrogen",
//   },
//   {
//     id: 7,
//     question: "In which year did the Titanic sink?",
//     options: ["1912", "1905", "1898", "1923"],
//     answer: "1912",
//   },
//   {
//     id: 8,
//     question: "Who is the author of '1984'?",
//     options: ["George Orwell", "Aldous Huxley", "Ray Bradbury", "J.D. Salinger"],
//     answer: "George Orwell",
//   },
//   {
//     id: 9,
//     question: "What is the hardest natural substance on Earth?",
//     options: ["Gold", "Platinum", "Diamond", "Iron"],
//     answer: "Diamond",
//   },
//   {
//     id: 10,
//     question: "What is the largest planet in our solar system?",
//     options: ["Earth", "Saturn", "Neptune", "Jupiter"],
//     answer: "Jupiter",
//   },
//   {
//     id: 11,
//     question: "What is the main ingredient in guacamole?",
//     options: ["Tomato", "Avocado", "Pepper", "Onion"],
//     answer: "Avocado",
//   },
//   {
//     id: 12,
//     question: "Which country is known as the Land of the Rising Sun?",
//     options: ["China", "Japan", "Thailand", "South Korea"],
//     answer: "Japan",
//   },
//   {
//     id: 13,
//     question: "What is the smallest prime number?",
//     options: ["1", "2", "3", "5"],
//     answer: "2",
//   },
//   {
//     id: 14,
//     question: "Who painted the Mona Lisa?",
//     options: ["Vincent van Gogh", "Leonardo da Vinci", "Pablo Picasso", "Claude Monet"],
//     answer: "Leonardo da Vinci",
//   },
//   {
//     id: 15,
//     question: "What is the capital city of Australia?",
//     options: ["Sydney", "Melbourne", "Canberra", "Brisbane"],
//     answer: "Canberra",
//   },
//   {
//     id: 16,
//     question: "Which gas do plants primarily use for photosynthesis?",
//     options: ["Oxygen", "Nitrogen", "Carbon Dioxide", "Hydrogen"],
//     answer: "Carbon Dioxide",
//   },
//   {
//     id: 17,
//     question: "What is the boiling point of water in Celsius?",
//     options: ["90°C", "100°C", "110°C", "120°C"],
//     answer: "100°C",
//   },
//   {
//     id: 18,
//     question: "Which language is primarily spoken in Brazil?",
//     options: ["Spanish", "Portuguese", "French", "English"],
//     answer: "Portuguese",
//   },
//   {
//     id: 19,
//     question: "What is the smallest unit of life?",
//     options: ["Tissue", "Organ", "Cell", "Organism"],
//     answer: "Cell",
//   },
//   {
//     id: 20,
//     question: "Who developed the theory of relativity?",
//     options: ["Isaac Newton", "Galileo Galilei", "Albert Einstein", "Niels Bohr"],
//     answer: "Albert Einstein",
//   },
//   {
//     id: 21,
//     question: "In what year did World War II end?",
//     options: ["1945", "1944", "1946", "1943"],
//     answer: "1945",
//   },
// ];