import { drizzle } from 'drizzle-orm/node-postgres';
import pg from 'pg';
import { env } from '../../../config/env';
import * as schema from './schema';

const { Pool } = pg;

// Create PostgreSQL connection pool for Drizzle
const pool = new Pool({
  connectionString: env.DATABASE_URL,
});

// Drizzle database instance with schema
export const db = drizzle(pool, { schema });

// Export schema for use in queries
export { schema };

// Graceful shutdown
export async function closeDrizzleConnection() {
  await pool.end();
}
