import pg from 'pg';

const { Pool } = pg;

let pool;

export function initPool(config) {
  pool = new Pool({
    // Pin the session timezone to business time (matches the
    // `ALTER DATABASE inventory_db SET timezone = 'America/Chicago'` default)
    // so a DB restore/migration can't silently revert date semantics.
    options: '-c TimeZone=America/Chicago',
    ...config,
  });
  return pool;
}

export async function getConnection() {
  if (!pool) {
    throw new Error('Database pool not initialized');
  }
  return pool.connect();
}

export function getPool() {
  return pool;
}

export default { initPool, getConnection, getPool };
