<Layouts.app flash={@flash} current_user={@current_user}>
  <.header>
    {gettext("Listing Users")}
    <:actions>
      <%= if can?(@current_user, :create, Mv.Accounts.User) do %>
        <.button variant="primary" navigate={~p"/users/new"} data-testid="user-new">
          <.icon name="hero-plus" /> {gettext("New User")}
        </.button>
      <% end %>
    </:actions>
  </.header>

  <.table
    id="users"
    rows={@users}
    row_id={fn user -> "row-#{user.id}" end}
    row_click={fn user -> JS.navigate(~p"/users/#{user}") end}
    row_tooltip={gettext("Click for user details")}
    sort_field={@sort_field}
    sort_order={@sort_order}
  >
    <:col
      :let={user}
      sort_field={:email}
      label={
        sort_button(%{
          field: :email,
          label: gettext("Email"),
          sort_field: @sort_field,
          sort_order: @sort_order
        })
      }
    >
      {user.email}
    </:col>
    <:col :let={user} label={gettext("Role")}>
      {user.role.name}
    </:col>
    <:col :let={user} label={gettext("Linked Member")}>
      <%= if user.member do %>
        {MvWeb.Helpers.MemberHelpers.display_name(user.member)}
      <% else %>
        <span class="text-base-content/70">{gettext("No member linked")}</span>
      <% end %>
    </:col>
    <:col :let={user} label={gettext("Password")}>
      <%= if MvWeb.Helpers.UserHelpers.has_password?(user) do %>
        <span>{gettext("Enabled")}</span>
      <% else %>
        <span class="text-base-content/70">—</span>
      <% end %>
    </:col>
    <:col :let={user} label={gettext("OIDC")}>
      <%= if MvWeb.Helpers.UserHelpers.has_oidc?(user) do %>
        <span>{gettext("Linked")}</span>
      <% else %>
        <span class="text-base-content/70">—</span>
      <% end %>
    </:col>
  </.table>
</Layouts.app>
