/**
 * 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 { settings } from "../../../../database/schema/settings.js";

import { createLog } from "../../logger/logger.js";
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
const newSettings = [
    {
        name: "server",
        value: "localhost",
        description: "Where the app runs at",
        moduleName: "server",
    },
    {
        name: "serverPort",
        value: "4400",
        description: "What are we listening on",
        moduleName: "server",
    },
    {
        name: "timezone",
        value: "America/Chicago",
        description:
            "What time zone is the server in this is used for cronjobs and some other time stuff",
        moduleName: "server",
    },
    {
        name: "dbUser",
        value: "alplaprod",
        description: "What is the db username",
        moduleName: "server",
    },
    {
        name: "dbPass",
        value: "b2JlbGl4",
        description: "What is the db password",
        moduleName: "server",
    },
    {
        name: "tcpPort",
        value: "2222",
        description:
            "TCP port for printers to connect send data and the zedra cameras",
        moduleName: "server",
    },
    {
        name: "prolinkCheck",
        value: "1",
        description:
            "Will prolink be considered to check if matches, maninly used in plants that do not fully utilize prolink + ocp",
        moduleName: "production",
    },
    {
        name: "bookin",
        value: "1",
        description: "do we want to book in after a label is printed",
        moduleName: "ocp",
    },
    {
        name: "dbServer",
        value: "usmcd1vms036",
        description: "What server is the prod db on?",
        moduleName: "server",
    },
    {
        name: "printDelay",
        value: "90",
        description: "How long in seconds between prints",
        moduleName: "ocp",
    },
    {
        name: "plantToken",
        value: "test3",
        description: "What is the plant token",
        moduleName: "server",
    },
    {
        name: "dualPrinting",
        value: "0",
        description: "Dose the plant have 2 machines that go to 1?",
        moduleName: "ocp",
    },
    {
        name: "ocmeService",
        value: "0",
        description:
            "Is the ocme service enabled. this is gernerally only for Dayton.",
        moduleName: "ocme",
    },
    {
        name: "fifoCheck",
        value: "45",
        description:
            "How far back do we want to check for fifo default 45, putting 0 will ignore.",
        moduleName: "ocme",
    },
    {
        name: "dayCheck",
        value: "3",
        description: "how many days +/- to check for shipments in alplaprod",
        moduleName: "ocme",
    },
    {
        name: "maxLotPerTruck",
        value: "3",
        description: "How mant lots can we have per truck?",
        moduleName: "ocme",
    },
    {
        name: "monitorAddress",
        value: "8",
        description:
            "What address is monitored to be limited to the amount of lots that can be added to a truck.",
        moduleName: "ocme",
    },
    {
        name: "ocmeCycleCount",
        value: "1",
        description: "Are we allowing ocme cycle counts?",
        roles: "admin",
        module: "ocme",
    },
    {
        name: "devDir",
        value: "C:\\Users\\matthes01\\Documents\\lstv2",
        description:
            "This is the dev dir and strictly only for updating the servers.",
        moduleName: "server",
    },
    {
        name: "demandMGTActivated",
        value: "0",
        description: "Do we allow for new fake edi?",
        roles: "admin",
        module: "logistics",
    },
    {
        name: "qualityRequest",
        value: "0",
        description: "quality request module?",
        roles: "admin",
        module: "logistics",
    },
    {
        name: "ocpLogsCheck",
        value: "4",
        description:
            "How long do we want to allow logs to show that have not been cleared?",
        roles: "admin",
        module: "ocp",
    },
    {
        name: "inhouseDelivery",
        value: "0",
        description: "Are we doing auto inhouse delivery?",
        roles: "admin",
        module: "ocp",
    },
    // dyco settings
    {
        name: "dycoConnect",
        value: "0",
        description: "Are we running the dyco system?",
        roles: "admin",
        module: "ocp",
    },
    {
        name: "dycoPrint",
        value: "0",
        description: "Are we using the dyco to get the labels or the rfid?",
        roles: "admin",
        module: "ocp",
    },
    {
        name: "strapperCheck",
        value: "1",
        description: "Are we monitoring the strapper for faults?",
        roles: "admin",
        module: "ocp",
    },
    {
        name: "v1SysServer",
        value: `localhost`,
        description:
            "The remaining v1 stuff here until we finish the frontend here.",
        serviceBelowsTo: "system",
        roleToChange: "admin",
    },
    {
        name: "v1SysPort",
        value: `4000`,
        description:
            "The remaining v1 stuff here until we finish the frontend here.",
        serviceBelowsTo: "system",
        roleToChange: "admin",
    },

    // ocp
    {
        name: "ocpActive",
        value: `1`,
        description: "Are we pritning on demand?",
        serviceBelowsTo: "ocp",
        roleToChange: "admin",
    },
    {
        name: "ocpCycleDelay",
        value: `10`,
        description: "How long between printer cycles do we want to monitor.",
        serviceBelowsTo: "ocp",
        roleToChange: "admin",
    },
    {
        name: "pNgAddress",
        value: `139`,
        description:
            "What is the address for p&g so we can make sure we have the correct fake edi forcast going in.",
        serviceBelowsTo: "dm",
        roleToChange: "admin",
    },
    {
        name: "scannerID",
        value: `500`,
        description: "What scanner id will we be using for the app",
        serviceBelowsTo: "logistics",
        roleToChange: "admin",
    },
    {
        name: "scannerPort",
        value: `50002`,
        description: "What port instance will we be using?",
        serviceBelowsTo: "logistics",
        roleToChange: "admin",
    },
    {
        name: "stagingReturnLocations",
        value: `30125,31523`,
        description:
            "What are the staging location IDs we will use to select from. seperated by commas",
        serviceBelowsTo: "logistics",
        roleToChange: "admin",
    },
    // temp settings can be deleted at a later date once that code is removed
    {
        name: "siloAdjMigrations",
        value: `0`,
        description: "Migrates the old silo adjustments to lst v2.",
        serviceBelowsTo: "admin",
        roleToChange: "admin",
    },
];
export const areSettingsIn = async () => {
    // get the roles
    try {
        try {
            const newRole = await db
                .insert(settings)
                .values(newSettings)
                .onConflictDoNothing() // this will  only update the ones that are new :D
                .returning({ name: settings.name });
            createLog(
                "info",
                "lst",
                "server",
                "Settings were just added due to missing them on server startup"
            );
        } catch (error) {
            createLog(
                "error",
                "lst",
                "server",
                "There was an error adding new roles to the db"
            );
        }
    } catch (error) {
        createLog(
            "error",
            "lst",
            "server",
            "There was an error getting or adding new Settingss"
        );
    }
};
