import pkg from "pg";
const { Pool } = pkg;

const baseConfig = {
  host: process.env.DATABASE_HOST ?? "localhost",
  port: parseInt(process.env.DATABASE_PORT ?? "5433", 10),
  user: process.env.DATABASE_USER,
  password: process.env.DATABASE_PASSWORD,
};

// Pools (one per DB)
const v1Pool = new Pool({
  ...baseConfig,
  database: "lst",
});

const v2Pool = new Pool({
  ...baseConfig,
  database: "lst_db",
});

// Query helpers
export const v1QueryRun = async (query: string, params?: any[]) => {
  try {
    const res = await v1Pool.query(query, params);
    return res;
  } catch (err) {
    console.error("V1 query error:", err);
    throw err;
  }
};

export const v2QueryRun = async (query: string, params?: any[]) => {
  try {
    const res = await v2Pool.query(query, params);
    return res;
  } catch (err) {
    console.error("V2 query error:", err);
    throw err;
  }
};