require('dotenv').config();
const mysql = require('mysql2/promise');

// Database connection configuration
const dbConfig = {
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
    database: process.env.DB_NAME || 'masm_app',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
};

// Create a connection pool for promise-based queries
const pool = mysql.createPool(dbConfig);

// Export a db object with execute method for compatibility with DatabaseService
const db = {
    execute: async (query, params) => {
        const connection = await pool.getConnection();
        try {
            // connection.execute returns [rows, fields], we need to return [rows] for compatibility
            const result = await connection.execute(query, params || []);
            return result;
        } finally {
            connection.release();
        }
    }
};

module.exports = db;

