const envCredential = require("../config");
const { Sequelize } = require("sequelize");
const DATABASE = envCredential.DATABASE;
const USER = envCredential.USER;
const HOST = envCredential.HOST;
const PASSWORD = envCredential.PASSWORD;
const PORT = envCredential.DB_PORT;

const sequelize = new Sequelize(DATABASE, USER, PASSWORD, {
  host: HOST,
  dialect: "mysql",
  port: PORT,
  dialectOptions: {
    useUTC: false,
    // need to comment before making build
    // socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock',
  },
});

console.log("DATABASE--------->", DATABASE);
console.log("USER--------->", USER);
console.log("PASSWORD--------->", PASSWORD);
console.log("PORT--------->", PORT);

sequelize
  .authenticate()
  .then(async() => {
    //  // 👇 Remove ONLY_FULL_GROUP_BY for this session
    //  await sequelize.query(`
    //   SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''))
    // `);

    // // ✅ Confirm it's gone
    // const [rows] = await sequelize.query("SELECT @@SESSION.sql_mode AS mode");
    // console.log("🚦 Current SQL_MODE:", rows[0].mode);

    console.log("Connection has been established successfully.");
  })
  .catch((error) => {
    console.error("Unable to connect to the database: ", error);
  });

module.exports = sequelize;
