ActiveAdmin.register JamRuby::MusicSessionHistory, :as => 'Music Session History', :sort_order => 'created_at DESC' do 

  config.filters = false
  config.per_page = 50
  config.clear_action_items!

  controller do
    def scoped_collection
      @music_session_histories ||= end_of_association_chain
        .includes([:user, :band])
        .order('created_at DESC')
    end
  end

  index :as => :block do |msh|
    div :for => msh do
      h3 "Session ##{msh.music_session_id}: #{msh.created_at.strftime('%b %d %Y, %H:%M')}"
      columns do
        column do
          panel 'Session Details' do
            attributes_table_for(msh) do
              row :description
              row :duration do |msh| "#{msh.duration_minutes} minutes" end
              row :active do |msh| msh.session_removed_at.nil? end
              row :creator do |msh| auto_link(msh.user, msh.user.try(:email)) end
              row :band do |msh| auto_link(msh.band, msh.band.try(:name)) end
              row :genres
            end 
          end
        end
        column do
          panel 'User Details' do
            table_for(msuh = msh.music_session_user_histories) do
              column :user do |msuh| msuh.user_email end
              column :joined do |msuh| msuh.created_at.strftime('%b %d %Y, %H:%M') end
              column :duration do |msuh| "#{msuh.duration_minutes} minutes" end
              column :active do |msuh| msuh.session_removed_at.nil? end
              column :perf_data do |msuh| 
                unless (uu = msuh.perf_uri).blank?
                  link_to('Data Link', uu)
                else
                  ''
                end 
              end
            end
          end
        end
      end
    end
  end

end
