const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(
    process.env.DB_NAME || 'test_db',
    process.env.DB_USER || 'arc',
    process.env.DB_PASS || 'aaradhya@2003',
    {
    host: process.env.DB_HOST || 'localhost',
    dialect: 'mysql',
    logging: false,
    pool: {
        max: Number(process.env.DB_POOL_MAX || 20),
        min: Number(process.env.DB_POOL_MIN || 2),
        acquire: Number(process.env.DB_POOL_ACQUIRE_MS || 60000),
        idle: Number(process.env.DB_POOL_IDLE_MS || 10000),
        evict: Number(process.env.DB_POOL_EVICT_MS || 1000)
    },
    retry: {
        max: Number(process.env.DB_RETRY_MAX || 3)
    }
});

const testConnection = async () => {
    try {
        await sequelize.authenticate();
        console.log('Database connection established');
    }
    catch (error) {
        console.log('Unable to connect to Database', error);
        throw error;
    }
};

module.exports = sequelize;
module.exports.testConnection = testConnection;