import { db } from "../../../pkg/db/db.js";
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
import { createLogger } from "../../../pkg/logger/logger.js";
import { tryCatch } from "../../../pkg/utils/tryCatch.js";

export const systemAdminRole = async (userId: string) => {
	const log = createLogger({
		module: "admin",
		subModule: "systemAdminSetup",
	});
	const systemAdminRoles = [
		{
			userId: userId,
			module: "users",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "admin",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "ocp",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "siloAdjustments",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "demandManagement",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "logistics",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "production",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "quality",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "eom",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "forklifts",
			role: "systemAdmin",
		},
		{
			userId: userId,
			module: "helperCommands",
			role: "systemAdmin",
		},
	];
	const { data, error } = await tryCatch(
		db.insert(userRoles).values(systemAdminRoles).onConflictDoNothing(),
	);

	if (error) {
		log.error(
			{ stack: { error: error } },
			"There was an error creating the system admin roles",
		);
	}

	log.info({ data }, "New system admin roles created");
};
