import { OpenAPIHono } from "@hono/zod-openapi";

import sendemail from "./routes/sendMail.js";
import { tryCatch } from "../../globalUtils/tryCatch.js";
import { db } from "../../../database/dbclient.js";

import { notifications } from "../../../database/schema/notifications.js";
import { createLog } from "../logger/logger.js";
import { note, notificationCreate } from "./utils/masterNotifications.js";
import { startNotificationMonitor } from "./utils/processNotifications.js";
import notifyStats from "./routes/getActiveNotifications.js";
import tiTrigger from "./routes/manualTiggerTi.js";
import blocking from "./routes/qualityBlocking.js";
import notify from "./routes/getNotifications.js";
import fifoIndex from "./routes/fifoIndex.js";

const app = new OpenAPIHono();

const routes = [
    sendemail,
    notifyStats,
    tiTrigger,
    blocking,
    notify,
    fifoIndex,
] as const;

const appRoutes = routes.forEach((route) => {
    app.route("/notify", route);
});

app.all("/notify/*", (c) => {
    return c.json({
        success: false,
        message: "you have encounted a notication route that dose not exist.",
    });
});

// check if the mastNotications is changed compared to the db and add if needed.
const { data: notes, error: notesError } = await tryCatch(
    db.select().from(notifications)
);

if (notesError) {
    createLog(
        "error",
        "notify",
        "notify",
        `There was an error getting the notifications: ${JSON.stringify(
            notesError
        )}`
    );
}

setTimeout(() => {
    notificationCreate();
    startNotificationMonitor();
}, 5 * 1000);

export default app;
