import { DEFAULT_DATABASE_URL } from '../../../packages/config/src/index.js';
import { createPool, query as runQuery, queryOne as runQueryOne } from '../../../packages/db/src/index.js';
import { loadEnv } from './env.js';

loadEnv();

export const pool = createPool({
  connectionString: process.env.DATABASE_URL || DEFAULT_DATABASE_URL,
  max: 5,
});

export async function query<T = unknown>(sql: string, params: unknown[] = []): Promise<T[]> {
  return runQuery<T>(pool, sql, params);
}

export async function queryOne<T = unknown>(sql: string, params: unknown[] = []): Promise<T | null> {
  return runQueryOne<T>(pool, sql, params);
}

export async function closeDb() {
  await pool.end();
}
