import pg from 'pg';

/**
 * Create a connection pool from an explicit connection string.
 * Config (src/infrastructure/config) owns env parsing and fail-fast checks;
 * this helper stays pure.
 */
export function createPool(connectionString: string): pg.Pool {
  if (!connectionString) {
    throw new Error('A database connection string is required to create a pool');
  }
  return new pg.Pool({ connectionString, max: 10 });
}

/** Thin typed query helper. Modules get data through repositories, not raw pools. */
export async function query<T extends pg.QueryResultRow = pg.QueryResultRow>(
  pool: pg.Pool,
  text: string,
  params: unknown[] = [],
): Promise<pg.QueryResult<T>> {
  return pool.query<T>(text, params);
}
