def METHOD_NAME(
    groups: list[str],
    user: User,
    action: str,
    ensure_joined: bool,
    check_info: Callable[[GroupSelfJoinSettings], bool],
    apply: Callable[[User, UserGroup], None],
) -> tuple[bool, bool, dict[str, str]]:
    user_groups: set[str] = set(
        g for g, in user.get_groups(include_expired=False).with_entities(UserGroup.name)
    )
    result = dict.fromkeys(groups, "")
    ugs: list[UserGroup] = UserGroup.query.filter(UserGroup.name.in_(groups)).all()
    all_ok = True
    is_course = False
    for ug in ugs:
        if (ensure_joined and ug.name not in user_groups) or (
            not ensure_joined and ug.name in user_groups
        ):
            result[ug.name] = f"User is {'not' if ensure_joined else ''} in this group"
            all_ok = False
            continue
        if not _check_self_join(ug, user, check_info):
            result[ug.name] = f"Self-{action} not enabled for this group"
            all_ok = False
            continue
        apply(user, ug)
        is_course = is_course or ug.is_self_join_course
        result[ug.name] = "OK"
    for g in result:
        if result[g] == "":
            result[g] = "Group not found"
            all_ok = False
    return all_ok, is_course, result