<app-ui-card title="Liste des Utilisateurs">
  <a
    helper-text
    href="javascript:void(0);"
    class="icon-link icon-link-hover link-primary fw-semibold"
    >Gérez les accès utilisateurs de votre plateforme
  </a>

  <div card-body>
    <!-- Barre d'actions supérieure -->
    <div class="row mb-3">
      <div class="col-md-6">
        <div class="d-flex justify-right-end gap-2">
          <button 
            class="btn btn-primary" 
            (click)="openCreateModal.emit()"
          >
            <ng-icon name="lucideUserPlus" class="me-1"></ng-icon>
            Nouvel Utilisateur
          </button>
        </div>
      </div>
    </div>

    <!-- Barre de recherche et filtres -->
    <div class="row mb-3">
      <div class="col-md-4">
        <div class="input-group">
          <span class="input-group-text">
            <ng-icon name="lucideSearch"></ng-icon>
          </span>
          <input 
            type="text" 
            class="form-control" 
            placeholder="Rechercher par nom, email, username..."
            [(ngModel)]="searchTerm"
            (keyup.enter)="onSearch()"
          >
        </div>
      </div>
      <div class="col-md-3">
        <select class="form-select" [(ngModel)]="statusFilter" (change)="onSearch()">
          <option value="all">Tous les statuts</option>
          <option value="enabled">Activés</option>
          <option value="disabled">Désactivés</option>
        </select>
      </div>
      <div class="col-md-3">
        <select class="form-select" [(ngModel)]="emailVerifiedFilter" (change)="onSearch()">
          <option value="all">Tous les emails</option>
          <option value="verified">Email vérifié</option>
          <option value="not-verified">Email non vérifié</option>
        </select>
      </div>
      <div class="col-md-2">
        <div class="d-flex gap-2">
          <button class="btn btn-outline-primary" (click)="onSearch()">
            <ng-icon name="lucideFilter"></ng-icon>
          </button>
          <button class="btn btn-outline-secondary" (click)="onClearFilters()">
            <ng-icon name="lucideX"></ng-icon>
          </button>
        </div>
      </div>
    </div>

    <!-- Loading State -->
    @if (loading) {
      <div class="text-center py-4">
        <div class="spinner-border text-primary" role="status">
          <span class="visually-hidden">Chargement...</span>
        </div>
        <p class="mt-2 text-muted">Chargement des utilisateurs...</p>
      </div>
    }

    <!-- Error State -->
    @if (error && !loading) {
      <div class="alert alert-danger" role="alert">
        <ng-icon name="lucideAlertCircle" class="me-2"></ng-icon>
        {{ error }}
      </div>
    }

    <!-- Users Table -->
    @if (!loading && !error) {
      <div class="table-responsive">
        <table class="table table-hover table-striped">
          <thead class="table-light">
            <tr>
              <th (click)="sort('username')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Utilisateur</span>
                  <ng-icon [name]="getSortIcon('username')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th (click)="sort('email')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Email</span>
                  <ng-icon [name]="getSortIcon('email')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th>Rôles</th>
              <th (click)="sort('enabled')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Statut</span>
                  <ng-icon [name]="getSortIcon('enabled')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th (click)="sort('createdTimestamp')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Créé le</span>
                  <ng-icon [name]="getSortIcon('createdTimestamp')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th width="150">Actions</th>
            </tr>
          </thead>
          <tbody>
            @for (user of displayedUsers; track user.id) {
              <tr>
                <td>
                  <div class="d-flex align-items-center">
                    <div class="avatar-sm bg-primary rounded-circle d-flex align-items-center justify-content-center me-2">
                      <span class="text-white fw-bold small">
                        {{ getUserInitials(user) }}
                      </span>
                    </div>
                    <div>
                      <strong>{{ getUserDisplayName(user) }}</strong>
                      <div class="text-muted small">@{{ user.username }}</div>
                    </div>
                  </div>
                </td>
                <td>
                  <div>{{ user.email }}</div>
                  @if (!user.emailVerified) {
                    <small class="text-warning">
                      <ng-icon name="lucideAlertCircle" class="me-1"></ng-icon>
                      Non vérifié
                    </small>
                  }
                </td>
                <td>
                  @for (role of user.clientRoles; track role) {
                    <span class="badge me-1 mb-1" [ngClass]="getRoleBadgeClass(role)">
                      {{ role }}
                    </span>
                  }
                  @if (user.clientRoles.length === 0) {
                    <span class="text-muted small">Aucun rôle</span>
                  }
                </td>
                <td>
                  <span [class]="getStatusBadgeClass(user)">
                    {{ getStatusText(user) }}
                  </span>
                </td>
                <td>
                  <small class="text-muted">
                    {{ formatTimestamp(user.createdTimestamp) }}
                  </small>
                </td>
                <td>
                  <div class="btn-group btn-group-sm" role="group">
                    <button 
                      class="btn btn-outline-primary btn-sm"
                      (click)="viewUserProfile(user.id)"
                      title="Voir le profil"
                    >
                      <ng-icon name="lucideEye"></ng-icon>
                    </button>
                    <button 
                      class="btn btn-outline-info btn-sm"
                      (click)="resetPassword(user)"
                      title="Réinitialiser le mot de passe"
                    >
                      <ng-icon name="lucideKey"></ng-icon>
                    </button>
                    @if (user.enabled) {
                      <button 
                        class="btn btn-outline-warning btn-sm"
                        (click)="disableUser(user)"
                        title="Désactiver l'utilisateur"
                      >
                        <ng-icon name="lucidePause"></ng-icon>
                      </button>
                    } @else {
                      <button 
                        class="btn btn-outline-success btn-sm"
                        (click)="enableUser(user)"
                        title="Activer l'utilisateur"
                      >
                        <ng-icon name="lucidePlay"></ng-icon>
                      </button>
                    }
                    <button 
                      class="btn btn-outline-danger btn-sm"
                      (click)="deleteUser(user)"
                      title="Supprimer l'utilisateur"
                    >
                      <ng-icon name="lucideTrash2"></ng-icon>
                    </button>
                  </div>
                </td>
              </tr>
            }
            @empty {
              <tr>
                <td colspan="6" class="text-center py-4">
                  <ng-icon name="lucideUsers" class="text-muted fs-1 mb-2"></ng-icon>
                  <p class="text-muted">Aucun utilisateur trouvé</p>
                  <button class="btn btn-primary" (click)="openCreateModal.emit()">
                    Créer le premier utilisateur
                  </button>
                </td>
              </tr>
            }
          </tbody>
        </table>
      </div>

      <!-- Pagination -->
        <div class="d-flex justify-content-between align-items-center mt-3">
          <div class="text-muted">
            Affichage de {{ getStartIndex() }} à {{ getEndIndex() }} sur {{ totalItems }} utilisateurs
          </div>
          <nav>
            <ngb-pagination
              [collectionSize]="totalItems"
              [page]="currentPage"
              [pageSize]="itemsPerPage"
              [maxSize]="5"
              [rotate]="true"
              [boundaryLinks]="true"
              (pageChange)="onPageChange($event)"
            />
          </nav>
        </div>
    }
  </div>

</app-ui-card>