ActiveAdmin.register JamRuby::User, :as => 'InactiveJamClassUsers' do

  menu :label => 'Inactive JamClass Users w/o Credits', :parent => 'JamClass'

  config.sort_order = 'created_at'
  config.batch_actions = false
  config.per_page = 100
  config.paginate = true
  config.filters = false

  scope("All", default: true) { |scope| scope.includes(:taken_lessons => :music_session).select("distinct(users.id), users.email, users.first_name, users.last_name").joins("inner join posa_cards on posa_cards.user_id = users.id inner join lesson_sessions on lesson_sessions.user_id = users.id left outer join music_sessions on music_sessions.lesson_session_id = music_sessions.id ").where("jamclass_credits = 0") }

  index do
    column "Name" do |user|
      span do
        link_to "#{user.name} (#{user.email})", "#{Rails.application.config.external_root_url}/client#/profile/#{user.id}"
      end
    end
    column "POSA" do |user|
      span do
        posa = user.posa_cards[0]
        if posa.lesson_package_type
          posa.lesson_package_type.id
        else
          posa.card_type
        end
      end
    end
    column "Last Session" do |user|
      span do
        if user.taken_lessons.length == 0
          "none yet"
        else
          most_recent_lesson = user.taken_lessons.order('created_at desc')[0]
          link_to most_recent_lesson.scheduled_start, admin_lesson_session_path(most_recent_lesson)
        end
      end
    end
  end
end