ActiveAdmin.register JamRuby::EmailBatchScheduledSessions, :as => 'Daily Sessions' do

  menu :label => 'Daily Sessions', :parent => 'Misc'

  config.sort_order = 'updated_at DESC'
  config.filters = false
  config.batch_actions = false
  config.clear_action_items!

  index do
    column 'Created' do |bb| bb.created_at end
    column 'Status' do |bb| bb.aasm_state end
    column 'Sent Count' do |bb| bb.sent_count end
    column 'Counters' do |bb| bb.test_emails end
    column 'Started' do |bb| bb.started_at end
    column 'Completed' do |bb| bb.completed_at end
  end

  action_item :only => [:index] do
    link_to('Daily Session Snapshot', new_admin_daily_session_path)
  end

  show :title => "Daily Session Snapshot" do |obj|
    h3 "Session created range: (#{obj.earliest_session_create_time}, #{obj.latest_session_create_time}); Earliest session start: #{obj.earliest_session_start_time}"
    h3 "Max Latency Score: #{params[:max_score] ? params[:max_score] : Score::MAX_YELLOW_LATENCY}"
    h4 "(append URL with ?max_score=N to change max latency from default (#{Score::MAX_YELLOW_LATENCY}))"

    num, objs = obj.snapshot_scored_recipients
    panel "Session & Scoring Matches (#{num})" do 
      table_for(objs) do
        column :receiver_id do |oo| 
          link_to(oo.vals['receiver_id'], admin_user_path(oo.vals['receiver_id']), {:target => '_blank'})
        end
        column :session_id do |oo| 
          link_to(oo.vals['session_id'], admin_music_session_path(oo.vals['session_id']), {:target => '_blank'})
        end
        column :latency_score do |oo| oo.vals['latency'] end
      end
    end

    num, objs = obj.snapshot_eligible_sessions
    panel "Eligible Sessions (#{num})" do 
      table_for(objs) do
        column :session_id do |oo| 
          link_to(oo.vals['session_id'], admin_music_session_path(oo.vals['session_id']), {:target => '_blank'}) 
        end
        column :creator_id do |oo| 
          link_to(oo.vals['creator_id'], admin_user_path(oo.vals['creator_id']), {:target => '_blank'})
        end
        column :creator_score_idx do |oo| oo.vals['creator_score_idx'] end
        column :instrument_id do |oo| oo.vals['instrument_id'] end
      end
    end

    num, objs = obj.snapshot_eligible_recipients
    panel "Eligible Recipients (#{num})" do 
      table_for(objs) do
        column :receiver_id do |oo| 
          link_to(oo.vals['receiver_id'], admin_user_path(oo.vals['receiver_id']), {:target => '_blank'})
        end
        column :receiver_score_idx do |oo| oo.vals['receiver_score_idx'] end
        column :instrument_id do |oo| oo.vals['instrument_id'] end
      end
    end
  end

  controller do
    def new
      if 0 < (max_score = params[:max_score].to_i)
        ENV[EmailBatchScheduledSessions::ENV_MAX_LATENCY] = max_score.to_s
      else
        ENV[EmailBatchScheduledSessions::ENV_MAX_LATENCY] = '0'
      end
      ENV[EmailBatchScheduledSessions::ENV_QUERY_LIMIT] = EmailBatchScheduledSessions::SNAPSHOT_QUERY_LIMIT
      set_resource_ivar(EmailBatchScheduledSessions.refresh_snapshot!)
      render active_admin_template('show')
    end
  end

end
