import deleteDbEntry from "../../backend/db/deleteDbEntry";
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";

type Param = {
    dbFullName: string;
    deletedUserId: string | number;
};
type Return = { success: boolean; result?: any; msg?: string };

/**
 * # Update API User Function
 */
export default async function apiDeleteUser({
    dbFullName,
    deletedUserId,
}: Param): Promise<Return> {
    const existingUserQuery = `SELECT * FROM ${dbFullName}.users WHERE id = ?`;
    const existingUserValues = [deletedUserId];

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

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

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

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