import mysql from "serverless-mysql"
import dotenv from "dotenv"

// needed to read the proper .env file, even when index.ts has it
dotenv.config()

const config = process.env.calendar_db_socket_path
	? {
			// socket path exists, production mode (linux)
			database: process.env.calendar_db_name,
			user: process.env.calendar_db_user,
			password: process.env.calendar_db_password,
			socketPath: process.env.calendar_db_socket_path,
		}
	: {
			// no socket path, development mode (windows)
			/* don't expose password or any sensitive info, done only for demo */
			host: "localhost",
			database: "calendar",
			user: "root",
			password: "",
			// connectionLimit: 50,
			port: 3306,
		}

const db = mysql({ config })

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default async function executeQuery({ query, values = "" }: { query: string; values?: string }): Promise<any> {
	// try {
	const results = await db.query(query, values)

	await db.end()

	return results
	// } catch (error) {
	// throw error;
	// }
}

// PATH: /etc/profile.d
// export varname=value
//
// startup status handled by update-rc.d (scriptname) defaults
