class Admin::UsersController < Admin::BaseController
  before_action :set_user, only: [:show]
  before_action :set_current_section

  def index
    ldap   = LdapService.new
    @ou    = params[:ou] || Setting.primary_domain
    @orgs  = ldap.fetch_organizations
    @pagy, @users = pagy(User.where(ou: @ou).order(cn: :asc))

    @stats = {
      users_confirmed: User.where(ou: @ou).confirmed.count,
      users_pending: User.where(ou: @ou).pending.count
    }
  end

  def show
    if Setting.lndhub_admin_enabled?
      @lndhub_user = @user.lndhub_user
    end

    @services_enabled = @user.services_enabled

    @avatar = LdapManager::FetchAvatar.call(cn: @user.cn)
  end

  private

  def set_user
    address = params[:address].split("@")
    @user = User.where(cn: address.first, ou: address.last).first
  end

  def set_current_section
    @current_section = :users
  end
end
