import { Pool, types } from 'pg'

// Coerce NUMERIC/DECIMAL columns to JS numbers globally. The default
// node-postgres behavior hands them back as strings, which silently breaks
// every downstream `.toFixed()` / direct-number call (seen crashing
// UnitEconomicsCards on the Finanzen slide). Our values fit comfortably in
// Number.MAX_SAFE_INTEGER, so the precision trade-off is fine.
types.setTypeParser(types.builtins.NUMERIC, (val) => (val === null ? null : parseFloat(val)))

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 20,
  idleTimeoutMillis: 30000,
  connectionTimeoutMillis: 10000,
})

export default pool
