ActiveAdmin.register Cohort, :as => 'Cohorts Monthly' do

  menu :label => 'Cohorts Monthly', :parent => 'Reports'

  config.sort_order = 'group_start_desc'
  config.batch_actions = false
  config.clear_action_items!
  config.per_page = 50

  filter(:monthly_start, as: :select, collection: Cohort.monthly_starts)

  controller do
    def scoped_collection
      args = params[:q] || {}
      Cohort.monthly_cohorts!(Time.parse(args[:monthly_start_eq])) if ! args[:monthly_start_eq].nil?
      super.where(all_time: false).order('group_start DESC')
    end
  end

  index :title => proc { "Monthly Cohorts #{params[:q] ? '('+Time.parse(params[:q][:monthly_start_eq]).strftime('%Y-%m')+')' : ''}" } do

    column 'Cohort' do |cc| 
      div(class: :cohort_col) { cc.group_start_str }
        end

    column Cohort::MONTHLY_LABELS[:registered_users] do |cc| 
      div(class: :cohort_col) { cc.data_val(:registered_users) }
        end

    column Cohort::MONTHLY_LABELS[:first_downloaded_client_at] do |cc| 
      div(class: :cohort_col) { cc.data_val(:first_downloaded_client_at) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:first_downloaded_client_at, true) } end

    column Cohort::MONTHLY_LABELS[:first_certified_gear_at] do |cc| 
      div(class: :cohort_col) { cc.data_val(:first_certified_gear_at) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:first_certified_gear_at, true) } end

    column Cohort::MONTHLY_LABELS[:music_sessions_user_history_1] do |cc| 
      div(class: :cohort_col) { cc.data_val(:music_sessions_user_history_1) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:music_sessions_user_history_1, true) } end

    column Cohort::MONTHLY_LABELS[:music_sessions_user_history_2_5] do |cc| 
      div(class: :cohort_col) { cc.data_val(:music_sessions_user_history_2_5) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:music_sessions_user_history_2_5, true) } end

    column Cohort::MONTHLY_LABELS[:music_sessions_user_history_6_] do |cc| 
      div(class: :cohort_col) { cc.data_val(:music_sessions_user_history_6_) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:music_sessions_user_history_6_, true) } end

    column Cohort::MONTHLY_LABELS[:jam_tracks_played_1] do |cc| 
      div(class: :cohort_col) { cc.data_val(:jam_tracks_played_1) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:jam_tracks_played_1, true) } end

    column Cohort::MONTHLY_LABELS[:jam_tracks_played_2_5] do |cc| 
      div(class: :cohort_col) { cc.data_val(:jam_tracks_played_2_5) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:jam_tracks_played_2_5, true) } end

    column Cohort::MONTHLY_LABELS[:jam_tracks_played_6_] do |cc| 
      div(class: :cohort_col) { cc.data_val(:jam_tracks_played_6_) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:jam_tracks_played_6_, true) } end

    column Cohort::MONTHLY_LABELS[:jam_track_rights_redeemed] do |cc| 
      div(class: :cohort_col) { cc.data_val(:jam_track_rights_redeemed) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:jam_track_rights_redeemed, true) } end

    column Cohort::MONTHLY_LABELS[:jam_track_rights] do |cc| 
      div(class: :cohort_col) { cc.data_val(:jam_track_rights) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:jam_track_rights, true) } end

    column Cohort::MONTHLY_LABELS[:recorded_tracks] do |cc| 
      div(class: :cohort_col) { cc.data_val(:recorded_tracks) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:recorded_tracks, true) } end

    column Cohort::MONTHLY_LABELS[:friendships] do |cc| 
      div(class: :cohort_col) { cc.data_val(:friendships) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:friendships, true) } end

    column Cohort::MONTHLY_LABELS[:invited_users] do |cc| 
      div(class: :cohort_col) { cc.data_val(:invited_users) }
    end
    column '%' do |cc| div(class: :cohort_col) { cc.data_val(:invited_users, true) } end
  end

end
