import dotenv from 'dotenv';
import { Sequelize } from 'sequelize';
import mysql2 from 'mysql2';

dotenv.config();

const sequelize = new Sequelize(
  process.env.MYSQL_DATABASE || 'fix_it_auto_care',
  process.env.MYSQL_USER || 'root',
  process.env.MYSQL_PASSWORD || '',
  {
    host: process.env.MYSQL_HOST || '127.0.0.1',
    port: Number(process.env.MYSQL_PORT || 3306),
    dialect: 'mysql',
    dialectModule: mysql2, // Required for Vercel / Serverless environments
    logging:
      process.env.SEQUELIZE_LOGGING === 'true'
        ? (sql) => console.log(sql)
        : false,
    define: {
      underscored: true,
      freezeTableName: false,
      timestamps: false,
    },
  }
);

export default sequelize;
