// SELECT count(*) FROM V_EtikettenGedruckt where AnzahlGedruckterKopien > 2 and CONVERT(varchar(5), Add_Date,108) not like CONVERT(varchar(5), Upd_Date,108) and Upd_Date > DATEADD(SECOND, -30,getdate()) and VpkVorschriftBez not like '%$%'

import { errorMonitor } from "node:events";
import { eq, sql } from "drizzle-orm";
import { db } from "../../../../../database/dbclient.js";
import { notifications } from "../../../../../database/schema/notifications.js";
import { settings } from "../../../../../database/schema/settings.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { createLog } from "../../../logger/logger.js";
import { sendEmail } from "../sendMail.js";

export interface DownTime {
	downTimeId?: number;
	machineAlias?: string;
}
export default async function tooManyErrors(notifyData: any) {
	// we will over ride this with users that want to sub to this
	// a new table will be called subalerts and link to the do a kinda linkn where the user wants it then it dose subId: 1, userID: x, notificationId: y. then in here we look up the userid to get the email :D
	// this could then leave the emails in the notificaion blank and let users sub to it.
	//console.log(notifyData);
	if (notifyData.emails === "") {
		createLog(
			"error",
			"notify",
			"notify",
			`There are no emails set for ${notifyData.name}`,
		);

		return;
	}

	// console.log(data.secondarySetting[0].duration);

	const plant = await db
		.select()
		.from(settings)
		.where(eq(settings.name, "plantToken"));
	console.log(plant[0].value);
	// console.log(
	// 	errorQuery
	// 		.replace("[time]", notifyData.checkInterval)
	// 		.replace("[errorCount]", notifyData.notifiySettings.errorCount),
	// 	errorLogQuery.replace("[time]", notifyData.checkInterval),
	// );

	let errorLogData: any = [];
	try {
		const errorData = await db.execute(sql`
            SELECT 'error' AS level, COUNT(*) AS error_count
            FROM public.logs
            WHERE level = 'error'
                AND "add_Date" > now() - INTERVAL ${sql.raw(`'${notifyData.checkInterval} minutes'`)}
            GROUP BY level
            HAVING COUNT(*) >= ${notifyData.notifiySettings.errorCount}
            `);
		if (
			errorData.length > 0
			// && downTime[0]?.downTimeId > notifyData.notifiySettings.prodID
		) {
			const errorLogs = await db.execute(sql`
            select* from public.logs where level = 'error' and "add_Date" > now() - INTERVAL ${sql.raw(`'${notifyData.checkInterval} minutes'`)} order by "add_Date" desc;
            `);

			errorLogData = errorLogs;
			//send the email :D
			const emailSetup = {
				email: notifyData.emails,
				subject: `Alert! ${plant[0].value} has encountered ${
					errorLogData.length
				} ${errorLogData.length > 1 ? "errors" : "error"} in the last ${notifyData.checkInterval} min`,
				template: "tooManyErrors",
				context: {
					data: errorLogData,
					count: notifyData.notifiySettings.errorCount,
					time: notifyData.checkInterval,
				},
			};

			//console.log(emailSetup);

			const sentEmail = await sendEmail(emailSetup);

			if (!sentEmail.success) {
				createLog(
					"error",
					"notify",
					"notify",
					"Failed to send email, will try again on next interval",
				);
				return {
					success: false,
					message: "Failed to send email, will try again on next interval",
					data: sentEmail,
				};
			}
		}
	} catch (err) {
		console.log(err);
		createLog(
			"error",
			"notify",
			"notify",
			`Error from running the downtimeCheck query: ${err}`,
		);

		return {
			success: false,
			message: "Error running error data",
			data: err,
		};
	}

	return {
		success: true,
		message: "Error log checking ran",
		data: errorLogData ?? [],
	};
}
