/**
 * 공유 데이터베이스 커넥션 풀
 *
 * Group B 서비스(tkuser, tkpurchase, tksafety, tksupport)용 동기 풀
 * mysql2/promise 기반, 싱글톤 lazy initialization
 */

const mysql = require('mysql2/promise');

let pool;

function getPool() {
  if (!pool) {
    pool = mysql.createPool({
      host: process.env.DB_HOST || 'mariadb',
      port: parseInt(process.env.DB_PORT) || 3306,
      user: process.env.DB_USER || 'hyungi_user',
      password: process.env.DB_PASSWORD,
      database: process.env.DB_NAME || 'hyungi',
      waitForConnections: true,
      connectionLimit: parseInt(process.env.DB_CONN_LIMIT) || 10,
      queueLimit: 0
    });
  }
  return pool;
}

module.exports = { getPool };
