import { Pool } from 'pg';
import { logger } from '@/shared/logger';

/**
 * Service-keyed Postgres pool factory.
 *
 * Each service reads its own connection string from `QUINN_<SERVICE>_DB_URL`
 * (e.g. service `macsync` → `QUINN_MACSYNC_DB_URL`). The URL points at the local
 * pgBouncer (`127.0.0.1:6432`), which terminates TLS to the DO Managed PG
 * upstream — so the app→bouncer hop is plaintext on the loopback and needs no
 * `ssl` block here.
 *
 * Vendored in-repo (was `@lilith/quinn-db-pg`) — that registry lived on the
 * retired `black` host and is gone; this is the faithful, self-contained pool.
 */
export function createPool(serviceName: string): Pool {
  const envKey = `QUINN_${serviceName.toUpperCase().replace(/-/g, '_')}_DB_URL`;
  const connectionString = process.env[envKey];
  if (!connectionString) {
    throw new Error(`${envKey} is required (no default — set the connection string in env)`);
  }

  const pool = new Pool({
    connectionString,
    max: Number(process.env['DB_POOL_MAX'] ?? 10),
    idleTimeoutMillis: 30_000,
    connectionTimeoutMillis: 10_000,
  });

  // An idle backend dropping the connection emits 'error' on the pool; without a
  // handler node-postgres rethrows and crashes the process. Log and let the pool
  // evict/replace the client on next checkout.
  pool.on('error', (err) => {
    logger.error(`db: idle client error`, { service: serviceName, err: err.message });
  });

  return pool;
}
