import { db } from "../../../../database/dbclient.js";
import { settings } from "../../../../database/schema/settings.js";
import { createLog } from "../../logger/logger.js";
import { serverSettings } from "../../server/controller/settings/getSettings.js";

export const prodSqlConfig = async () => {
    try {
        //const serverSetting = await db.select().from(settings);
        const serverSetting = serverSettings as any;
        // create dummy type data
        const server = serverSetting.filter((s: any) => s.name === "dbServer");
        const plantToken = serverSetting.filter(
            (s: any) => s.name === "plantToken"
        );
        const dbUser = serverSetting.filter((s: any) => s.name === "dbUser");
        // if erroring out double check the password was actually encoded before saving
        const dbPassword = serverSetting.filter(
            (s: any) => s.name === "dbPass"
        );

        const sqlConfig = {
            server: server[0].value,
            database: `AlplaPROD_${plantToken[0].value}_cus`,
            user: dbUser[0].value,
            password: atob(dbPassword[0].value),
            options: {
                encrypt: true,
                trustServerCertificate: true,
            },
            requestTimeout: 90000, // in milliseconds
            pool: {
                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
            },
        };

        return sqlConfig;
    } catch (error) {
        createLog(
            "info",
            "lst",
            "sqlProd",
            `${JSON.stringify(
                error
            )} "There was an error getting/setting up the config for the prod sql server."`
        );
    }
};
