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

  menu :label => 'Students', :parent => 'JamClass'

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

  def   booked_anything(scope)
    scope.joins(:student_lesson_bookings).where('lesson_bookings.active = true').uniq
  end

  filter :jamuser_full_name_or_email_cont, label: 'Name Or Email', as: :string
  filter :school, label: 'School'

  scope("TestDrive/Amazon Users", default: true) {|scope| scope.joins(:posa_cards).where('posa_cards.lesson_package_type_id in (?)', LessonPackageType::AMAZON_PACKAGES + LessonPackageType::LESSON_PACKAGE_TYPES) }
  scope("Student Or Has Credits Or Has Lesson") { |scope| scope.where('is_a_student = true OR jamclass_credits > 0 OR ((select count(id) from lesson_bookings where lesson_bookings.user_id = users.id) > 0)').order('users.ready_for_session_at IS NULL DESC') }

  index do
    column "Name" do |user|
      link_to user.name, user.admin_url
    end
    column "Email" do |user|
      user.email
    end
    column "Location" do |user|
      user.location(country = true)
    end

    column "Session Ready" do |user|
      div do
        if user.ready_for_session_at
          span do
            'YES'
          end
        else
          span do
            'NO'
          end
          span do
            br
          end
          span do
            link_to("mark as checked", mark_session_ready_admin_student_path(user.id), {confirm: "Mark as ready for session?"})
          end

        end
      end
    end
    column "School" do |user|
      if user.school
        user.school.name
      end
    end
    column :updated_at
  end

  member_action :mark_session_ready, :method => :get do
    resource.mark_session_ready
    redirect_to :back
  end


  member_action :mark_session_not_ready, :method => :get do
    resource.mark_session_not_ready
    redirect_to :back
  end


end