<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <!--
        Composant réutilisable: Actions Utilisateur - Écosystème LionsDev
        
        Auteur: Lions User Manager
        Version: 2.1.0
        Description: Ensemble de boutons d'action pour un utilisateur.
                     Utilise le confirmDialog global pour les suppressions.
    -->

    <c:set var="update" value="#{empty update ? '@form' : update}" />
    <c:set var="layout" value="#{empty layout ? 'horizontal' : layout}" />
    <c:set var="showView" value="#{empty showView ? true : showView}" />
    <c:set var="showEdit" value="#{empty showEdit ? true : showEdit}" />
    <c:set var="showDelete" value="#{empty showDelete ? true : showDelete}" />
    <c:set var="showActivate" value="#{empty showActivate ? true : showActivate}" />
    <c:set var="showDeactivate" value="#{empty showDeactivate ? true : showDeactivate}" />
    <c:set var="showResetPassword" value="#{empty showResetPassword ? true : showResetPassword}" />
    <c:set var="showLogoutSessions" value="#{empty showLogoutSessions ? false : showLogoutSessions}" />
    <c:set var="iconOnly" value="#{empty iconOnly ? true : iconOnly}" />

    <!-- Bean Cible pour les actions (par défaut userBean) -->
    <c:set var="targetBean" value="#{empty actionBean ? userBean : actionBean}" />

    <!-- Actions par défaut -->
    <c:set var="actActivate" value="#{empty activateAction ? targetBean.activateUser(user.id) : activateAction}" />
    <c:set var="actDeactivate"
        value="#{empty deactivateAction ? targetBean.deactivateUser(user.id) : deactivateAction}" />
    <c:set var="actDelete" value="#{empty deleteAction ? targetBean.deleteUser(user.id) : deleteAction}" />
    <c:set var="actReset"
        value="#{empty resetPasswordAction ? targetBean.resetPassword(user.id) : resetPasswordAction}" />
    <c:set var="actLogout"
        value="#{empty logoutSessionsAction ? targetBean.logoutAllSessions(user.id) : logoutSessionsAction}" />

    <c:choose>
        <!-- Layout Dropdown -->
        <c:when test="#{layout == 'dropdown'}">
            <p:commandButton icon="pi pi-ellipsis-v"
                styleClass="p-button-text p-button-sm p-button-rounded p-button-plain" type="button" title="Actions"
                style="width: 2rem; height: 2rem;">
                <p:menu styleClass="w-12rem">
                    <c:if test="#{showView}">
                        <p:menuitem value="Voir le profil" icon="pi pi-eye"
                            outcome="#{not empty viewOutcome ? viewOutcome : '/pages/user-manager/users/profile'}">
                            <f:param name="userId" value="#{user.id}" />
                        </p:menuitem>
                    </c:if>
                    <c:if test="#{showEdit}">
                        <p:menuitem value="Modifier" icon="pi pi-pencil"
                            outcome="#{not empty editOutcome ? editOutcome : '/pages/user-manager/users/edit'}">
                            <f:param name="userId" value="#{user.id}" />
                        </p:menuitem>
                    </c:if>
                    <c:if test="#{showResetPassword}">
                        <p:menuitem value="Réinitialiser MDP" icon="pi pi-key"
                            onclick="PF('resetPasswordDialog_#{user.id}').show()" />
                    </c:if>
                    <p:separator />
                    <c:if test="#{showActivate and not user.enabled}">
                        <p:menuitem value="Activer" icon="pi pi-check" styleClass="text-green-600"
                            action="#{actActivate}" update="#{update}" />
                    </c:if>
                    <c:if test="#{showDeactivate and user.enabled}">
                        <p:menuitem value="Désactiver" icon="pi pi-times" styleClass="text-orange-600"
                            action="#{actDeactivate}" update="#{update}" />
                    </c:if>
                    <c:if test="#{showLogoutSessions}">
                        <p:menuitem value="Déconnecter sessions" icon="pi pi-sign-out" styleClass="text-blue-600"
                            action="#{actLogout}" update="#{update}" />
                    </c:if>
                    <c:if test="#{showDelete}">
                        <p:separator />
                        <p:menuitem value="Supprimer" icon="pi pi-trash" styleClass="text-red-600" action="#{actDelete}"
                            update="#{update}">
                            <p:confirm header="Confirmation" message="Supprimer l'utilisateur #{user.username} ?"
                                icon="pi pi-exclamation-triangle" />
                        </p:menuitem>
                    </c:if>
                </p:menu>
            </p:commandButton>
        </c:when>

        <!-- Layout Horizontal -->
        <c:otherwise>
            <div class="flex gap-1 align-items-center">
                <c:if test="#{showView}">
                    <ui:include src="/templates/components/shared/buttons/button-user-action.xhtml">
                        <ui:param name="icon" value="pi pi-eye" />
                        <ui:param name="iconOnly" value="true" />
                        <ui:param name="severity" value="info" />
                        <ui:param name="title" value="Voir" />
                        <ui:param name="rounded" value="true" />
                        <ui:param name="hasOutcome" value="true" />
                        <ui:param name="outcome"
                            value="#{not empty viewOutcome ? viewOutcome : '/pages/user-manager/users/profile'}" />
                        <ui:param name="paramUserId" value="#{user.id}" />
                        <ui:param name="paramRealm" value="#{user.realmName}" />
                    </ui:include>
                </c:if>

                <c:if test="#{showEdit}">
                    <ui:include src="/templates/components/shared/buttons/button-user-action.xhtml">
                        <ui:param name="icon" value="pi pi-pencil" />
                        <ui:param name="iconOnly" value="true" />
                        <ui:param name="severity" value="warning" />
                        <ui:param name="title" value="Modifier" />
                        <ui:param name="rounded" value="true" />
                        <ui:param name="hasOutcome" value="true" />
                        <ui:param name="outcome"
                            value="#{not empty editOutcome ? editOutcome : '/pages/user-manager/users/edit'}" />
                        <ui:param name="paramUserId" value="#{user.id}" />
                        <ui:param name="paramRealm" value="#{user.realmName}" />
                    </ui:include>
                </c:if>

                <c:if test="#{showResetPassword}">
                    <ui:include src="/templates/components/shared/buttons/button-user-action.xhtml">
                        <ui:param name="icon" value="pi pi-key" />
                        <ui:param name="iconOnly" value="true" />
                        <ui:param name="severity" value="help" />
                        <ui:param name="title" value="Réinitialiser MDP" />
                        <ui:param name="rounded" value="true" />
                        <ui:param name="onclick" value="PF('resetPasswordDialog_#{user.id}').show()" />
                    </ui:include>
                </c:if>

                <c:if test="#{showActivate and not user.enabled}">
                    <ui:include src="/templates/components/shared/buttons/button-user-action.xhtml">
                        <ui:param name="icon" value="pi pi-check" />
                        <ui:param name="iconOnly" value="true" />
                        <ui:param name="severity" value="success" />
                        <ui:param name="title" value="Activer" />
                        <ui:param name="rounded" value="true" />
                        <ui:param name="hasAction" value="true" />
                        <ui:param name="action" value="#{actActivate}" />
                        <ui:param name="update" value="#{update}" />
                    </ui:include>
                </c:if>

                <c:if test="#{showDeactivate and user.enabled}">
                    <ui:include src="/templates/components/shared/buttons/button-user-action.xhtml">
                        <ui:param name="icon" value="pi pi-times" />
                        <ui:param name="iconOnly" value="true" />
                        <ui:param name="severity" value="warning" />
                        <ui:param name="title" value="Désactiver" />
                        <ui:param name="rounded" value="true" />
                        <ui:param name="hasAction" value="true" />
                        <ui:param name="action" value="#{actDeactivate}" />
                        <ui:param name="update" value="#{update}" />
                    </ui:include>
                </c:if>

                <c:if test="#{showDelete}">
                    <ui:include src="/templates/components/shared/buttons/button-user-action.xhtml">
                        <ui:param name="icon" value="pi pi-trash" />
                        <ui:param name="iconOnly" value="true" />
                        <ui:param name="severity" value="danger" />
                        <ui:param name="title" value="Supprimer" />
                        <ui:param name="rounded" value="true" />
                        <ui:param name="hasAction" value="true" />
                        <ui:param name="action" value="#{actDelete}" />
                        <ui:param name="update" value="#{update}" />
                        <ui:param name="confirmMessage" value="Supprimer l'utilisateur #{user.username} ?" />
                    </ui:include>
                </c:if>
            </div>
        </c:otherwise>
    </c:choose>

    <!-- Dialog de Reset Password (reste spécifique par user car il contient un form) -->
    <p:dialog id="resetPasswordDialog_#{user.id}" widgetVar="resetPasswordDialog_#{user.id}"
        header="Réinitialiser le mot de passe" modal="true" styleClass="w-full md:w-4">
        <h:form>
            <div class="field">
                <p:outputLabel for="newPass" value="Nouveau mot de passe" />
                <p:password id="newPass" value="#{targetBean.newPassword}" feedback="true" required="true"
                    toggleMask="true" styleClass="w-full" />
            </div>
            <div class="field">
                <p:outputLabel for="confPass" value="Confirmer" />
                <p:password id="confPass" value="#{targetBean.newPasswordConfirm}" required="true" toggleMask="true"
                    styleClass="w-full" />
            </div>
            <div class="flex justify-content-end gap-2">
                <p:commandButton value="Valider" icon="pi pi-check" action="#{actReset}" update="#{update}"
                    oncomplete="PF('resetPasswordDialog_#{user.id}').hide()" />
                <p:commandButton value="Annuler" icon="pi pi-times" styleClass="p-button-secondary p-button-outlined"
                    onclick="PF('resetPasswordDialog_#{user.id}').hide()" />
            </div>
        </h:form>
    </p:dialog>

</ui:composition>