{% load i18n %}
<div id="user-groups-{{ user.pk }}">
    <div class="d-flex flex-column gap-2">
        <div>
            <small class="text-muted">Project groups:</small>
            <div class="d-flex flex-wrap gap-2 mt-1">
                {% if user.user_groups.all %}
                    {% for group in user.user_groups.all %}
                        {% if request.user.is_superuser or is_cid_user_manager %}
                            <button
                                class="btn btn-sm btn-outline-secondary"
                                hx-post="{% url 'generic:user_toggle_group' user.pk group.pk %}"
                                hx-target="#user-groups-{{ user.pk }}"
                                hx-swap="outerHTML"
                            >
                                {{ group.name }}
                                <span class="ms-1 text-muted small">&times;</span>
                            </button>
                        {% else %}
                            <a class="btn btn-sm btn-outline-secondary" href="{% url 'generic:user_group_detail' group.pk %}">{{ group.name }}</a>
                        {% endif %}
                    {% endfor %}
                {% else %}
                    <div class="text-muted">No groups.</div>
                {% endif %}

                {% if request.user.is_superuser or is_cid_user_manager %}
                    {% if available_groups.count %}
                        <div class="dropdown">
                            <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" id="addGroupDropdown" data-bs-toggle="dropdown" aria-expanded="false">
                                Add group
                            </button>
                            <ul class="dropdown-menu" aria-labelledby="addGroupDropdown">
                                {% for ag in available_groups %}
                                    <li>
                                        <button
                                            class="dropdown-item"
                                            hx-post="{% url 'generic:user_toggle_group' user.pk ag.pk %}"
                                            hx-target="#user-groups-{{ user.pk }}"
                                            hx-swap="outerHTML"
                                        >
                                            {{ ag.name }}
                                        </button>
                                    </li>
                                {% endfor %}
                            </ul>
                        </div>
                    {% endif %}
                {% endif %}
            </div>
        </div>

        {# Auth groups moved to the Permission groups area in profile.html #}
    </div>
</div>
