/**
 * Database Connection Module
 * Handles PostgreSQL connection using pg library
 */

const { Pool } = require('pg');

const pool = new Pool({
    connectionString: process.env.DATABASE_URL,
    ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
    max: 20,
    idleTimeoutMillis: 30000,
    connectionTimeoutMillis: 2000,
});

async function connectDB() {
    const client = await pool.connect();
    await client.query('SELECT NOW()');
    client.release();
    return true;
}

async function query(text, params) {
    return await pool.query(text, params);
}

async function getClient() {
    return await pool.connect();
}

module.exports = { connectDB, query, getClient, pool };
