// an external way to creating logs
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { responses } from "../../../globalUtils/routeDefs/responses.js";

import qualityBlockingMonitor from "../controller/notifications/qualityBlocking.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";

import { notifications } from "../../../../database/schema/notifications.js";
import { db } from "../../../../database/dbclient.js";
import { eq } from "drizzle-orm";
import { apiHit } from "../../../globalUtils/apiHits.js";

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

app.openapi(
    createRoute({
        tags: ["notify"],
        summary: "Manually trigger TI intergrations.",
        method: "get",
        path: "/blockingTrigger",
        //middleware: authMiddleware,
        responses: responses(),
    }),
    async (c) => {
        /**
         * get the blocking notification stuff
         */
        apiHit(c, { endpoint: "/blockingTrigger" });
        const { data, error } = await tryCatch(
            db
                .select()
                .from(notifications)
                .where(eq(notifications.name, "qualityBlocking"))
        );

        if (error) {
            return c.json({
                success: false,
                message: "Error Getting Notification Settings.",
                data: error,
            });
        }
        const blocking = await qualityBlockingMonitor(data[0]);

        return c.json({
            success: blocking?.success,
            message: blocking?.message,
        });
    }
);
export default app;
