import mysql from 'mysql2/promise';

function createPool(): mysql.Pool {
  const port = process.env.DB_PORT ? parseInt(process.env.DB_PORT, 10) : 3306;
  return mysql.createPool({
    connectionLimit: 10,
    port,
    host: process.env.DB_ENDPOINT,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: 'catherine_league',
    charset: 'utf8mb4',
  });
}

let pool: mysql.Pool | undefined;

export function getPool(): mysql.Pool {
  if (!pool) {
    pool = createPool();
  }
  return pool;
}
