import { and, eq, sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { printerData } from "../../../../database/schema/printers.js";
import { settings } from "../../../../database/schema/settings.js";
import { delay } from "../../../globalUtils/delay.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { createLog } from "../../logger/logger.js";
import { getPrinters } from "../controller/printers/getPrinters.js";

//**The logic here will be when the setting is active to utilize the lots times it will update the printDelay in the printer data table */
export const printerDelayByLot = async (lot: any) => {
	const { data: settingData, error: settingError } = await tryCatch(
		db.select().from(settings),
	);

	if (settingError) {
		return {
			success: false,
			message: "There was an error getting the settings.",
			settingError,
		};
	}
	// get the plantToken
	const printDelay = settingData.filter((n) => n.name === "lotPrintDelay");

	const ignorePrinters = [
		"Autolabeler",
		"pdf24",
		"PDF24",
		"zecchetti_1",
		"zecchetti2",
	];
	const printers = (await getPrinters()) as any;
	const p = printers.data;

	if (printDelay[0].value === "0") {
		for (let i = 0; i < p.length; i++) {
			if (p[i].printDelay > 90) {
				if (ignorePrinters.includes(p[i].name)) continue;

				const { data, error } = await tryCatch(
					db
						.update(printerData)
						.set({
							printDelay: "90",
							upd_date: sql`NOW()`,
						})
						.where(
							and(
								eq(printerData.humanReadableId, lot[i].printerID),
								eq(printerData.printDelayOverride, false),
							),
						),
				);

				// if (data) {
				//     createLog(
				//         "info",
				//         "printers",
				//         "ocp",
				//         `${printerData.name} had its delay time updated to 90 seconds `
				//     );
				// }

				// if (error) {
				//     createLog(
				//         "error",
				//         "printers",
				//         "ocp",
				//         `${printerData.name} encountered an error updating the printer delay time `
				//     );
				// }
				await delay(500);
			}
		}
	}

	if (printDelay[0].value === "1") {
		for (let i = 0; i < lot.length; i++) {
			if (ignorePrinters.includes(lot[i].PrinterName)) continue;

			const { data, error } = await tryCatch(
				db
					.update(printerData)
					.set({
						printDelay: lot[i].timeTOmakeInSeconds,
						upd_date: sql`NOW()`,
					})
					.where(
						and(
							eq(printerData.humanReadableId, lot[i].printerID),
							eq(printerData.printDelayOverride, false),
						),
					),
			);

			// if (data) {
			//     createLog(
			//         "info",
			//         "printers",
			//         "ocp",
			//         `${lot[i].PrinterName} had its delay time updated to ${lot[
			//             i
			//         ].timeTOmakeInSeconds.toFixed(2)} seconds `
			//     );
			// }

			// if (error) {
			//     createLog(
			//         "error",
			//         "printers",
			//         "ocp",
			//         `${lot[i].PrinterName} encountered an error updating the printer delay time `
			//     );
			// }
			await delay(500);
		}
	}
};
