ActiveAdmin.register JamRuby::MusicSession, :as => 'Music Session' do

  config.filters = false
  config.per_page = 50
  config.clear_action_items!
  config.sort_order = 'created_at_desc'
  menu :parent => 'Sessions', :label => 'Sessions'

  controller do
    def scoped_collection
      if params['admin'].blank? || '1' == params['admin']
        @music_sessions ||= end_of_association_chain
          .includes([:creator, :band])
          .order('created_at DESC')
      else
        @music_sessions ||= end_of_association_chain
          .joins('INNER JOIN users AS uu ON uu.id = music_sessions.user_id')
          .where(['uu.admin = ?','f'])
          .includes([:creator, :band])
          .order('created_at DESC')
      end
    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')}"
      h4 "(append URL with ?admin=1 to show admin sessions)"
      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.creator, msh.creator.try(:email)) end
              row :band do |msh| auto_link(msh.band, msh.band.try(:name)) end
              row :genre
            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

  show do
    attributes_table do
      row :id
      row :name
      row :description
      row :creator do |session|
        link_to(session.creator.admin_name, session.creator.admin_url)
      end
      row :created_at
      row :started_at
      row :session_ended_at do |session| session.session_removed_at end
      row :genre
      row :recurring_mode
      row :timezone
      row :fan_access
      row :music_access
      row :approval_required
      row :open_rsvps
      row :is_unstructured_rsv
      row :canceled
      row :lesson_session do |session|
        lesson_session = session.lesson_session
        if lesson_session
          link_to("Lesson", lesson_session.admin_url)
        else
          ''
        end
      end
      row 'Session Attendances' do |session|

          table_for(msuh = session.music_session_user_histories) do
            column :user do |msuh| msuh.user.admin_name end
            column :joined do |msuh| msuh.created_at.strftime('%b %d %Y, %H:%M') end
            column :duration do |msuh| "#{msuh.duration_minutes.round} minutes" end
            column :perf_data do |msuh|
              unless (uu = msuh.perf_uri).blank?
                link_to('Per Data Link', uu)
              else
                'No Perf Data'
              end
            end
          end

      end
    end
  end

end
