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

import { sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { prodPermissions } from "../../../../database/schema/prodPermissions.js";
import { createLog } from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newProdRoles: any = [
    {
        name: "planning",
        description: "Planning viewer only",
        roles: ["Manufacturing\\IssueMaterial\\MaterialHandler"],
        rolesLegacy: [3],
    },
    // production
    {
        name: "prodsupervisor",
        description:
            "Production supervisor, planning, labeling, and production contorlling.",
        roles: [
            "Manufacturing\\IssueMaterial\\MaterialHandler",
            "Manufacturing\\IssueMaterial\\ProcessAdmin",
            "Manufacturing\\ProductionLabelling\\ProcessAdmin",
        ],
        rolesLegacy: [3, 11, 41, 48, 49],
    },
    // quality
    {
        name: "qualityTechB",
        description:
            "Quality tech with blocking, planning, stock warehouse leader, waste, quality waste.",
        roles: ["Quality\\Blocking\\ProcessAdmin"],
        rolesLegacy: [3, 14, 45],
    },
    {
        name: "qualityManager",
        description:
            "Quality manager with cch, blocking, planning, stock warehouse leader, waste, quality waste.",
        roles: [
            "Manufacturing\\ProductionControlling\\ProductionController",
            "Quality\\ComplaintHandling\\ProcessAdmin",
            "Quality\\Blocking\\ProcessAdmin",
            "Manufacturing\\IssueMaterial\\ProcessAdmin",
            "Manufacturing\\ProductionLabelling\\ProcessAdmin",
        ],
        rolesLegacy: [105, 3, 14, 45],
    },
    // logistics

    {
        name: "planner",
        description: "Planning role.",
        roles: [
            "Administration\\Scan\\ApiConsumer",
            "Administration\\Printing\\ApiConsumer",
            "Logistics\\Warehousing\\ProcessAdmin",
            "Manufacturing\\IssueMaterial\\ProcessAdmin",
            "Manufacturing\\ProductionLabelling\\ProcessAdmin",
        ],
        rolesLegacy: [55, 95, 15, 105, 145, 9],
    },
    // plant manager
    {
        name: "plantManager",
        description:
            "Quality tech with blocking, planning, stock warehouse leader, waste, quality waste.",
        roles: [
            "Quality\\ComplaintHandling\\ProcessAdmin",
            "Manufacturing\\ProductionControlling\\ProcessAdmin",
            "MasterData\\Manufacturing\\ProcessAdmin",
            "Quality\\Blocking\\ProcessAdmin",
            "Logistics\\InboundDeliveries\\ApiConsumer",
            "MasterData\\Logistics\\ProcessAdmin",
            "DemandManagement\\Order\\ProcessAdmin",
            "Manufacturing\\IssueMaterial\\ProcessAdmin",
            "Logistics\\Warehousing\\ProcessAdmin",
            "Logistics\\OutboundDeliveries\\ProcessAdmin",
            "DemandManagement\\Forecast\\ProcessAdmin",
            "Manufacturing\\ProductionLabelling\\ProcessAdmin",
        ],
        rolesLegacy: [3, 55, 145, 95, 45, 105, 65, 15, 125],
    },
    // regional
    {
        name: "ctoRegional",
        description: "admin for everything except planning.",
        roles: [
            "Manufacturing\\IssueMaterial\\ProcessAdmin",
            "MasterData\\Logistics\\ProcessAdmin",
            "MasterData\\Manufacturing\\ProcessAdmin",
            "Manufacturing\\IssueMaterial\\MaterialHandler",
            "Manufacturing\\ProductionLabelling\\ProcessAdmin",
            "DemandManagement\\Order\\ProcessAdmin",
            "Logistics\\Warehousing\\ProcessAdmin",
            "Quality\\Blocking\\BlockingContributor",
            "Quality\\Blocking\\ProcessAdmin",
        ],
        rolesLegacy: [65, 95, 105, 55, 135, 45, 15, 125, 3, 9, 145],
    },
];
export const prodRoles = async () => {
    // get the roles
    for (let i = 0; i < newProdRoles.length; i++) {
        try {
            const newRole = await db
                .insert(prodPermissions)
                .values(newProdRoles[i])
                .onConflictDoUpdate({
                    target: prodPermissions.name,
                    set: {
                        name: newProdRoles[i].name,
                        description: newProdRoles[i].description,
                        roles: newProdRoles[i].roles,
                        rolesLegacy: newProdRoles[i].rolesLegacy,
                        upd_date: sql`NOW()`,
                    },
                }) // this will  only update the ones that are new :D
                .returning({ name: prodPermissions.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"
    );
};
