import sql from "mssql";

// hardcoding everything to make sure it all works as intended
const server = "usday1vms010";
const dbUser = "ocme";
const dbPassword = "Democrat-Humongous";

export const sqlConfig_ocme = {
    server: server,
    database: `Alpla_lst`,
    user: dbUser,
    password: dbPassword,
    options: {
        encrypt: true,
        trustServerCertificate: true,
    },
    requestTimeout: 90000, // in milliseconds
    // ocmePool: {
    //     max: 20, // Maximum number of connections in the pool
    //     min: 0, // Minimum number of connections in the pool
    //     idleTimeoutMillis: 10000, // How long a connection is allowed to be idle before being released
    //     reapIntervalMillis: 1000, // how often to check for idle resourses to destory
    //     acquireTimeoutMillis: 100000, // How long until a complete timeout happens
    // },
};

export async function runQuery(queryToRun: string, name: string) {
    let connection;
    try {
        // Establish a direct connection to the different server
        connection = await sql.connect(sqlConfig_ocme);

        // Execute the query
        const result = await connection.request().query(queryToRun);
        console.log(`${name} query ran successfully on different server`);

        return result.recordset;
    } catch (err) {
        console.log(`Error running ${name} query on different server: ${err}`);

        return [];
    } finally {
        // Close the direct connection
        if (connection) {
            await connection.close();
        }
    }
}
