/**
 * @ai-summary PostgreSQL connection pool configuration
 * @ai-context Shared pool for all feature repositories
 */
import { Pool } from 'pg';
import { logger } from '../logging/logger';
import { appConfig } from './config-loader';

export const pool = new Pool({
  connectionString: appConfig.getDatabaseUrl(),
  max: 10,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 10000,
});

pool.on('connect', () => {
  logger.debug('Database pool: client connected');
});

pool.on('error', (err) => {
  logger.error('Database pool error', { error: err.message });
});

// Graceful shutdown
process.on('SIGTERM', async () => {
  await pool.end();
});

export default pool;
