/**
 * check if the modules are in and if not add them.
 * this will only run on a server start up
 */

import { db } from "../../../../database/dbclient.js";
import { subModules } from "../../../../database/schema/subModules.js";
import { createLog } from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newSubModules = [
    {
        name: "RFID",
        moduleName: "prodcution",
        description: "RFID stuff",
        link: "/rfid",
        icon: "Tags",
        active: true,
        roles: [
            "viewer",
            "technician",
            "supervisor",
            "manager",
            "admin",
            "systemAdmin",
        ],
        subSubModule: [],
    },
    {
        name: "Silo Adjustments",
        moduleName: "logistics",
        description: "Do a silo adjustmnet",
        link: "/siloAdjustments",
        icon: "Cylinder",
        active: false,
        roles: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
        subSubModule: [],
    },
    {
        name: "Demand Management",
        moduleName: "logistics",
        description: "Bulk order and Forecast imports",
        link: "/dm",
        icon: "Truck",
        roles: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
        active: false,
        subSubModule: [],
    },
    {
        name: "Forecast",
        moduleName: "logistics",
        description: "",
        link: "#",
        icon: "Truck",
        roles: ["systemAdmin"],
        active: false,
        subSubModule: [],
    },
    {
        name: "Material Helper",
        moduleName: "logistics",
        description: "",
        link: "/materialHelper/consumption",
        icon: "Package",
        roles: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
        active: false,
        subSubModule: [],
    },
    {
        name: "Ocme Cyclecount",
        moduleName: "logistics",
        description: "",
        link: "/cyclecount",
        icon: "Package",
        roles: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
        active: false,
        subSubModule: [],
    },
    {
        name: "Open Orders",
        moduleName: "logistics",
        description: "Open orders",
        link: "/openOrders",
        icon: "Truck",
        roles: [
            "viewer",
            "technician",
            "supervisor",
            "manager",
            "admin",
            "systemAdmin",
        ],
        active: false,
        subSubModule: [],
    },
    {
        name: "Barcodes",
        moduleName: "logistics",
        description: "Barcodes, lanes and scanable",
        link: "/barcodegen",
        icon: "Barcode",
        roles: [
            "viewer",
            "technician",
            "supervisor",
            "manager",
            "admin",
            "systemAdmin",
        ],
        active: true,
        subSubModule: [],
    },
    {
        name: "Helper Commands",
        moduleName: "logistics",
        description: "Commands to assist when a scanner is not avalible",
        link: "/helpercommands",
        icon: "Command",
        roles: ["technician", "supervisor", "manager", "admin", "systemAdmin"],
        active: true,
        subSubModule: [],
    },

    // admin module
    {
        name: "Servers",
        moduleName: "admin",
        description: "Do a silo adjustmnet",
        link: "/servers",
        icon: "Server",
        roles: ["tester", "systemAdmin"],
        isActive: true,
        subSubModule: [],
    },
    {
        name: "Admin",
        moduleName: "admin",
        description: "Do a silo adjustmnet",
        link: "#", // when link is # this will mean its a button
        icon: "ShieldCheck",
        active: true,
        roles: ["tester", "admin", "systemAdmin"],
        subSubModule: [
            {
                name: "Settings",
                link: "/settings",
                icon: "Settings",
                newWindow: false,
                isActive: true,
            },
            {
                name: "Modules",
                link: "/modules",
                icon: "Settings",
                newWindow: false,
                isActive: true,
            },
            {
                name: "Sub Modules",
                link: "/subModules",
                icon: "Settings",
                newWindow: false,
                isActive: true,
            },
            {
                name: "Notifcations",
                link: "notificationMGT",
                icon: "Webhook",
                newWindow: false,
                isActive: true,
            },
            {
                name: "Swagger",
                link: "#",
                icon: "Webhook",
                newWindow: true,
                isActive: true,
            },
            {
                name: "Logs",
                link: "#",
                icon: "Logs",
                newWindow: false,
                isActive: false,
            },
            {
                name: "Users",
                link: "/users",
                icon: "Users",
                newWindow: false,
                isActive: true,
            },
            {
                name: "Prod Perms",
                link: "/produsers",
                icon: "Users",
                newWindow: false,
                isActive: true,
            },
            {
                name: "UCD",
                link: "https://ucd.alpla.net:8443/",
                icon: "Atom",
                newWindow: false,
                isActive: true,
            },
            {
                name: "Lst Api",
                link: "/api/docs",
                icon: "Webhook",
                newWindow: false,
                isActive: true,
            },
        ],
    },
];
export const areSubModulesIn = async () => {
    try {
        for (let i = 0; i < newSubModules.length; i++) {
            const subModuleUpdate = await db
                .insert(subModules)
                .values(newSubModules[i])
                .onConflictDoUpdate({
                    target: subModules.name,
                    set: {
                        name: newSubModules[i].name,
                        moduleName: newSubModules[i].moduleName,
                        description: newSubModules[i].description,
                        roles: newSubModules[i].roles,
                        link: newSubModules[i].link,
                        subSubModule: newSubModules[i].subSubModule,
                        icon: newSubModules[i].icon,
                    },
                }) // this will  only update the ones that are new :D
                .returning({ name: subModules.name });
        }
        createLog(
            "info",
            "lst",
            "server",
            "SubModules were just added due to missing them on server startup"
        );
    } catch (error) {
        console.log(error);
        createLog(
            "error",
            "lst",
            "server",
            "There was an error adding new subModules to the db"
        );
    }
};
