import mysql from 'mysql2/promise';
import { config } from './config.js';

export function createPool() {
  return mysql.createPool({
    host: config.mysql.host,
    port: config.mysql.port,
    user: config.mysql.user,
    password: config.mysql.password,
    database: config.mysql.database,
    waitForConnections: true,
    connectionLimit: config.mysql.connectionLimit,
    enableKeepAlive: true,
    keepAliveInitialDelay: 0,
  });
}

export async function hasTable(pool, tableName) {
  const [rows] = await pool.query(
    'SELECT COUNT(*) AS c FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?',
    [tableName]
  );
  return (rows?.[0]?.c ?? 0) > 0;
}
