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

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

    console.log(data);

    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(subModules)
            .set({ active: data.active })
            .where(eq(subModules.submodule_id, subModuleID))
            .returning({ name: subModules.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;
};
