// Copyright (c) 2024, JINGROW
// For license information, please see license.txt

jingrow.ui.form.on("System Health Report", {
	onload(frm) {
		let poll_attempts = 0;
		const interval = setInterval(() => {
			jingrow
				.xcall(
					"jingrow.desk.pagetype.system_health_report.system_health_report.get_job_status",
					{ job_id: frm.pg.test_job_id }
				)
				.then((status) => {
					poll_attempts += 1;
					if (["finished", "failed"].includes(status) || poll_attempts > 30) {
						clearInterval(interval);
					}
					status && frm.set_value("background_jobs_check", status);
				});
		}, 1000);
	},
	refresh(frm) {
		frm.set_value("socketio_ping_check", "Fail");
		jingrow.realtime.on("pong", () => {
			frm.set_value("socketio_ping_check", "Pass");
			frm.set_value(
				"socketio_transport_mode",
				jingrow.realtime.socket.io?.engine?.transport?.name
			);
		});
		jingrow.realtime.emit("ping");
		frm.disable_save();
		frm.trigger("setup_highlight");
	},

	setup_highlight(frm) {
		/// field => is bad?
		const conditions = {
			scheduler_status: (val) => val.toLowerCase() != "active",
			background_jobs_check: (val) => val.toLowerCase() != "finished",
			total_background_workers: (val) => val == 0,
			binary_logging: (val) => val.toLowerCase() != "on",
			socketio_ping_check: (val) => val != "Pass",
			socketio_transport_mode: (val) => val != "websocket",
			onsite_backups: (val) => val == 0,
			failed_logins: (val) => val > frm.pg.total_users,
			total_errors: (val) => val > 50,
			// 5% excluding very small numbers
			unhandled_emails: (val) =>
				val > 3 && frm.pg.handled_emails > 3 && val / frm.pg.handled_emails > 0.05,
			failed_emails: (val) =>
				val > 3 &&
				frm.pg.total_outgoing_emails > 3 &&
				val / frm.pg.total_outgoing_emails > 0.05,
			pending_emails: (val) =>
				val > 3 &&
				frm.pg.total_outgoing_emails > 3 &&
				val / frm.pg.total_outgoing_emails > 0.1,
			oldest_unscheduled_job: (val) => !!val,
			"queue_status.pending_jobs": (val) => val > 50,
			"background_workers.utilization": (val) => val > 70,
			"background_workers.failed_jobs": (val) => val > 50,
			"top_errors.occurrences": (val) => val > 10,
			"failing_scheduled_jobs.failure_rate": (val) => val > 10,
		};

		const style = document.createElement("style");
		style.innerText = `.health-check-failed {
				font-weight: bold;
				color: var(--text-colour);
				background-color: var(--bg-red);
			}`;
		document.head.appendChild(style);

		const update_fields = () => {
			if (!jingrow.get_route().includes(frm.pg.name)) {
				clearInterval(interval);
			}
			Object.entries(conditions).forEach(([field, condition]) => {
				try {
					if (field.includes(".")) {
						let [table, fieldname] = field.split(".");

						frm.fields_dict[table].grid.grid_rows.forEach((row) => {
							let is_bad = condition(row.pg[fieldname]);
							$(row.columns[fieldname]).toggleClass("health-check-failed", is_bad);
						});
					} else {
						let is_bad = condition(frm.pg[field]);
						let df = frm.fields_dict[field];
						$(df.disp_area).toggleClass("health-check-failed", is_bad);
					}
				} catch (e) {
					console.log("Failed to evaluated", e);
				}
			});
		};

		update_fields();
		const interval = setInterval(update_fields, 1000);
	},
});
