/**
 * 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 { modules } from "../../../../database/schema/modules.js";
import { createLog } from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newModules: any = [
    {
        name: "production",
        active: true,
        roles: ["viewer", "tester", "systemAdmin"],
    },
    {
        name: "logistics",
        active: false,
        roles: ["viewer", "manager", "supervisor", "tester", "systemAdmin"],
    },
    {
        name: "quality",
        active: false,
        roles: ["viewer", "manager", "tester", "systemAdmin"],
    },
    {
        name: "forklift",
        active: false,
        roles: ["manager", "admin", "tester", "systemAdmin"],
    },
    {
        name: "eom",
        active: false,
        roles: ["manager", "admin", "tester", "systemAdmin"],
    },
    { name: "admin", active: true, roles: ["admin", "systemAdmin"] },
    {
        name: "ocp",
        active: false,
        roles: ["viewer", "admin", "tester", "systemAdmin"],
    },
];
export const areModulesIn = async () => {
    // get the roles
    for (let i = 0; i < newModules.length; i++) {
        try {
            const newRole = await db
                .insert(modules)
                .values(newModules[i])
                .onConflictDoUpdate({
                    target: modules.name,
                    set: { roles: newModules[i].roles },
                }) // this will  only update the ones that are new :D
                .returning({ name: modules.name });
        } catch (error) {
            console.log(error);
            createLog(
                "error",
                "lst",
                "server",
                "There was an error adding new modules to the db"
            );
        }
    }
    createLog(
        "info",
        "lst",
        "server",
        "Modules were just added due to missing them on server startup"
    );
};
