import pg from 'pg';

const { Pool } = pg;
let pool = null;
function getPool() {
  if (!pool) {
    const connectionString = process.env.DATABASE_URL;
    if (!connectionString) {
      throw new Error("DATABASE_URL environment variable is not set");
    }
    pool = new Pool({ connectionString, max: 10 });
  }
  return pool;
}
async function query(text, params) {
  const p = getPool();
  return p.query(text, params);
}
function log(message, ...args) {
  const ts = (/* @__PURE__ */ new Date()).toISOString();
  console.log(`[${ts}] ${message}`, ...args);
}
function logError(message, error) {
  const ts = (/* @__PURE__ */ new Date()).toISOString();
  console.error(`[${ts}] ERROR: ${message}`, error?.message || error || "");
}

export { logError as a, log as l, query as q };
