import { db } from "../../../../database/dbclient.js";
import { notifications } from "../../../../database/schema/notifications.js";
import { createLog } from "../../logger/logger.js";

export const note: any = [
    {
        name: "reprintLabels",
        description:
            "Monitors the labels that are printed and returns a value if one falls withing the time frame defined below.",
        checkInterval: 1,
        timeType: "min",
        emails: "",
        active: false,
        notifiySettings: { prodID: 1 },
    },
    {
        name: "downTimeCheck",
        description:
            "Checks for specific downtimes that are greater than 105 min.",
        checkInterval: 30,
        timeType: "min",
        emails: "",
        active: false,
        notifiySettings: { prodID: 1, daysInPast: 5, duration: 105 },
    },
    {
        name: "qualityBlocking",
        description:
            "Checks for new blocking orders that have been entered, recommened to get the most recent order in here before activating.",
        checkInterval: 30,
        timeType: "min",
        emails: "",
        active: false,
        notifiySettings: {
            prodID: 1,
            sentBlockingOrders: [{ timeStamp: "0", blockingOrder: 1 }],
        },
    },
    {
        name: "productionCheck",
        description: "Checks ppoo",
        checkInterval: 2,
        timeType: "hour",
        emails: "",
        active: false,
        notifiySettings: {
            prodID: 1,
            count: 0,
            weekend: false,
            locations: "0",
        },
    },
    {
        name: "stagingCheck",
        description:
            "Checks staging based on locations, locations need to be seperated by a ,",
        checkInterval: 2,
        timeType: "hour",
        emails: "",
        active: false,
        notifiySettings: {
            prodID: 1,
            count: 0,
            weekend: false,
            locations: "0",
        },
    },
    {
        name: "tiIntergration",
        description: "Checks for new releases to be put into ti",
        checkInterval: 2,
        timeType: "hour",
        emails: "",
        active: false,
        notifiySettings: {
            prodID: 1,
            start: 36,
            end: 36,
            releases: [{ timeStamp: "0", releaseNumber: 1 }],
        },
    },
    {
        name: "exampleNotification",
        description: "Checks for new releases to be put into ti",
        checkInterval: 2,
        timeType: "min",
        emails: "",
        active: true,
        notifiySettings: {
            prodID: 1,
            start: 36,
            end: 36,
            releases: [1, 2, 3],
        },
    },
    {
        name: "fifoIndex",
        description: "Checks for pallets that were shipped out of fifo",
        checkInterval: 1,
        timeType: "hour",
        emails: "blake.matthes@alpla.com",
        active: false,
        notifiySettings: {
            prodID: 1,
            start: 36,
            end: 36,
            releases: [1, 2, 3],
        },
    },
    {
        name: "bow2henkelincoming",
        description:
            "Checks for new incoming goods orders to be completed and sends an email for what truck and carrier it was",
        checkInterval: 15,
        timeType: "min",
        emails: "blake.matthes@alpla.com",
        active: false,
        notifiySettings: { processTime: 15 },
    },
    {
        name: "palletsRemovedAsWaste",
        description:
            "Validates stock to make sure, there are no pallets released that have been removed as waste already ",
        checkInterval: 15,
        timeType: "min",
        emails: "blake.matthes@alpla.com",
        active: false,
        notifiySettings: { prodID: 1 },
    },
    {
        name: "shortageBookings",
        description:
            "Checks for material shortage bookings by single av type or all types ",
        checkInterval: 15,
        timeType: "min",
        emails: "blake.matthes@alpla.com",
        active: false,
        notifiySettings: {
            time: 15,
            type: "all", // change this to something else or leave blank to use the av type
            avType: 1,
        },
    },
];

export const notificationCreate = async () => {
    for (let i = 0; i < note.length; i++) {
        try {
            const notify = await db
                .insert(notifications)
                .values(note[i])
                .onConflictDoUpdate({
                    target: notifications.name,
                    set: {
                        name: note[i].name,
                        description: note[i].description,
                        //notifiySettings: note[i].notifiySettings,
                    },
                });
        } catch (error) {
            createLog(
                "error",
                "notify",
                "notify",
                `There was an error getting the notifications: ${JSON.stringify(
                    error
                )}`
            );
        }
    }
    createLog(
        "info",
        "lst",
        "nofity",
        "notifications were just added/updated due to server startup"
    );
};
