ActiveAdmin.register JamRuby::LessonSession, :as => 'SlowResponses' do

  menu :label => 'Slow Responses', :parent => 'JamClass'

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

  filter :intervened_at_null, label: 'Not Yet Intervened', as: :boolean

  scope("72 hours since last request/counter", default: true) { |scope| scope.slow_responses }

  member_action :cancel_lesson, :method => :get do
    result = resource.cancel_by_admin

    if result.errors.any?
      redirect_to :back, notice: resource.errors.inspect
    else
      redirect_to :back, notice: 'Canceled lesson'
    end
  end

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

  index do
    column "Not Responded" do |lesson_session|
      div do
        if lesson_session.student_last_proposed?
          "Teacher"
        else
          "Student"
        end
      end
      div do
        late_responder = lesson_session.late_responder
        span do
          link_to "#{late_responder.name}", late_responder.admin_url
        end
      end
      div do
        lesson_session.late_responder.email
      end
    end
    column "Proposer" do |lesson_session|
      div do
        if lesson_session.student_last_proposed?
          "Student"
        else
          "Teacher"
        end
      end
      div do
        last_proposer = lesson_session.last_proposer
        span do
          link_to "#{last_proposer.name}", last_proposer.admin_url
        end
      end
      div do
        lesson_session.last_proposer.email
      end
    end
    column "Lesson Type" do |lesson_session|
      lesson_session.display_type
    end
    column "Proposed Start Time" do |lesson_session|
      span do
        if lesson_session.music_session.nil?
          raise "Lessonsesison with no id #{lesson_session.id}"
        else
          link_to lesson_session.music_session.pretty_scheduled_start(true), lesson_session.admin_url

        end
      end
    end
    column "Days with no response" do |lesson_session|
      if lesson_session.last_response_time.nil?
        'N/A'
      else
        "#{((Time.now - lesson_session.last_response_time) / 24 / 3600).floor} days"
      end
    end
    column "Num Reqs/Counters" do |lesson_session|
      ChatMessage.unscoped.where(lesson_session_id: lesson_session.id).where('purpose = ? OR purpose = ?', "Lesson Requested",  "New Time Proposed").count(:purpose)
    end
    column "Intervene" do |lesson_session|
      span do
        if lesson_session.intervened_at
          lesson_session.intervened_at.to_date
        else
          link_to("intervened", intervened_admin_slow_response_path(lesson_session.id), {confirm: "Mark Lesson as 'Intervened' (i.e., email sent to by hand)?"})
        end

      end
    end
    column "Cancel Lesson" do |lesson_session|
      span do
        link_to("cancel", cancel_lesson_admin_slow_response_path(lesson_session.id), {confirm: "Cancel Lesson?"})
      end
    end
  end
end