// config/db.js
require("dotenv").config();
const { Pool } = require("pg");

const pool = new Pool({
  user: process.env.PG_USER,
  host: process.env.PG_HOST,
  database: process.env.PG_DATABASE,
  password: process.env.PG_PASSWORD,
  port: process.env.PG_PORT,
  max: 10, // important
});

const connectPostgres = async () => {
  try {
    const client = await pool.connect();
    // console.log(pool.PG_DATABASE)
    console.log("✅ PostgreSQL connected successfully");
    client.release();
  } catch (error) {
    console.error("❌ PostgreSQL connection failed:", error);
    throw error;
  }
};

module.exports = { pool, connectPostgres };
