import {eq} from "drizzle-orm";
import {db} from "../../../../../database/dbclient.js";
import {modules} from "../../../../../database/schema/modules.js";
import {createLog} from "../../../logger/logger.js";

type Data = {
    active: boolean;
};
export const updateModule = async (data: Data, moduleID: string) => {
    createLog("info", "lst", "server", "Module being updated");
    let module;

    if (typeof data.active !== "boolean") {
        createLog("error", "lst", "server", "Invalid data type: 'active' must be a boolean");
        throw new Error("'active' must be a boolean");
    }

    try {
        module = await db
        .update(modules)
        .set({active: data.active})
        .where(eq(modules.module_id, moduleID))
        .returning({name: modules.name});
        //.where(sql`${userRole} = ANY(roles)`);
    } catch (error) {
        createLog("error", "lst", "server", "There was an error updating the module");
        throw new Error("There was an error updating the module");
    }
    return module;
};
