class Spacer
  def self.spacer(val, row)

    percentage = ((val * 100) / row.total.to_f).round(1).to_s
    ('%-5.5s' % percentage).gsub(' ', '&nbsp;') + '%&nbsp;-&nbsp;' + val.to_s
  end
end

ActiveAdmin.register_page "Subscription Cohorts" do
  menu :parent => 'Reports'


  content :title => "Subscription Cohorts" do
    h2 "Users Grouped By Month as Paying Subscribers"
    table_for User.select(%Q{date_trunc('month', created_at) as month,
                      count(id) as total,
                      count(first_downloaded_client_at) as downloaded,
                      count(first_ran_client_at) as ran_client,
                      count(first_certified_gear_at) as ftue,
                      count(first_music_session_at) as any_session,
                      count(first_real_music_session_at) as real_session,
                      count(first_good_music_session_at) as good_session,
                      count(first_invited_at) as invited,
                      count(first_friended_at) as friended,
                      count(recurly_subscription_id) as subscribed
                  }).group("date_trunc('month', created_at)").order("date_trunc('month', created_at) DESC") do |row|
      column "Month", Proc.new { |user| user.month.strftime('%B %Y') }
      column "Total", :total
      column "Subscribed", Proc.new { |user| raw(Spacer.spacer(user.subscribed, user)) }
      column "Downloaded", Proc.new { |user| raw(Spacer.spacer(user.downloaded, user)) }
      column "Ran Client", Proc.new { |user| raw(Spacer.spacer(user.ran_client, user)) }
      column "FTUE", Proc.new { |user| raw(Spacer.spacer(user.ftue, user)) }
      column "Any Session", Proc.new { |user| raw(Spacer.spacer(user.any_session, user)) }
      column "2+ Session", Proc.new { |user| raw(Spacer.spacer(user.real_session, user)) }
      column "Good Session", Proc.new { |user| raw(Spacer.spacer(user.good_session, user)) }
      column "Invited", Proc.new { |user| raw(Spacer.spacer(user.invited, user)) }
      column "Friended", Proc.new { |user| raw(Spacer.spacer(user.friended, user)) }
    end
  end

end