// @ts-check

const deleteDbEntry = require("../../backend/db/deleteDbEntry");
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");

/**
 * # Update API User Function
 *
 * @param {object} params
 * @param {string} params.dbFullName
 * @param {string | number} params.deletedUserId
 * @param {boolean} [params.useLocal]
 *
 * @returns {Promise<{ success: boolean, result?: any, msg?: string }>}
 */
module.exports = async function apiDeleteUser({
    dbFullName,
    deletedUserId,
    useLocal,
}) {
    const existingUserQuery = `SELECT * FROM users WHERE id = ?`;
    const existingUserValues = [deletedUserId];

    const existingUser = await varDatabaseDbHandler({
        queryString: existingUserQuery,
        queryValuesArray: existingUserValues,
        database: dbFullName,
        useLocal,
    });

    if (!existingUser?.[0]) {
        return {
            success: false,
            msg: "User not found",
        };
    }

    const deleteUser = await deleteDbEntry({
        dbContext: "Dsql User",
        paradigm: "Full Access",
        dbFullName,
        tableName: "users",
        identifierColumnName: "id",
        identifierValue: deletedUserId,
        useLocal,
    });

    return {
        success: true,
        result: deleteUser,
    };
};
