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

import {db} from "../../../../database/dbclient.js";
import {roles} from "../../../../database/schema/roles.js";
import {createLog} from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newRoles = [
    {name: "viewer"},
    {name: "technician"},
    {name: "supervisor"},
    {name: "manager"},
    {name: "admin"},
    {name: "tester"},
    {name: "systemAdmin"},
];
export const areRolesIn = async () => {
    // get the roles
    try {
        const roleCheck = await db.select().from(roles);

        if (roleCheck.length !== newRoles.length) {
            try {
                const newRole = await db
                .insert(roles)
                .values(newRoles)
                .onConflictDoNothing() // this will  only update the ones that are new :D
                .returning({name: roles.name});
                createLog(
                    "info",
                    "lst",
                    "auth",
                    `${JSON.stringify(newRole)}, "Roles were just added due to missing them on server startup"`
                );
            } catch (error) {
                createLog("error", "lst", "auth", `There was an error adding new roles to the db, ${error}`);
            }
        }
    } catch (error) {
        createLog("error", "lst", "auth", `There was an error adding new roles to the db, ${error}`);
    }
};
