// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { eq } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { notifications } from "../../../../database/schema/notifications.js";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { authMiddleware } from "../../auth/middleware/authMiddleware.js";
import hasCorrectRole from "../../auth/middleware/roleCheck.js";
import tooManyErrors from "../controller/notifications/tooManyErrors.js";
import { getAllJobs } from "../utils/processNotifications.js";

const app = new OpenAPIHono({ strict: false });

app.openapi(
	createRoute({
		tags: ["server"],
		summary: "Returns current active notifications.",
		method: "get",
		path: "/toomanyerrors",
		middleware: [authMiddleware, hasCorrectRole(["systemAdmin"], "admin")],
		responses: responses(),
	}),
	async (c) => {
		apiHit(c, { endpoint: "/toomanyerrors" });

		const { data, error } = await tryCatch(
			db
				.select()
				.from(notifications)
				.where(eq(notifications.name, "tooManyErrors")),
		);

		if (error) {
			return c.json({
				success: false,
				message: "Error Getting Notification Settings.",
				data: error,
			});
		}
		const errorData = await tooManyErrors(data[0]);
		return c.json({
			success: true,
			message: "Current Error log data",
			data: errorData?.data,
		});
	},
);
export default app;
