const mysql = require("mysql");

function connectToDatabase() {
  return new Promise((resolve, reject) => {
    const connection = mysql.createConnection({
      database: "kayceoki_qdalel", // Use 'database' instead of 'name'
      user: "kayceoki_kadmin",
      password: "qdaleeldbgtx123",
      host: "localhost",
      port: 3306, // port should be a number
    });

    connection.connect(err => {
      if (err) {
        console.error("Failed to connect to the database:", err);
        reject(err);
        return;
      }

      connection.query("SELECT * FROM webapp_doctors LIMIT 1", (err, results) => {
        if (err) {
          console.error("Query failed:", err);
          reject(err);
          return;
        }

        console.log("Successfully connected to the database.", results);
        resolve(connection);
      });
    });
  });
}

module.exports = { connectToDatabase };
