ActiveAdmin.register JamRuby::User, :as => 'OnboarderManagement' do

  menu :label => 'Onboarder Management', :parent => 'JamClass'

  config.sort_order = 'created_at desc'
  config.batch_actions = true
  config.per_page = 100
  config.paginate = true
  config.filters = true
  config.clear_action_items!
  batch_action :destroy, false

  batch_action :onboarder, form: -> { {
                        support_consultant: (User.where(is_onboarder: true).includes(:onboarding_users).map {|user| ["#{user.name} (#{user.onboarding_users.where('onboarding_status = ? OR onboarding_status = ?', User::ONBOARDING_STATUS_ASSIGNED, User::ONBOARDING_STATUS_EMAILED).count})", user.id]}).to_a.unshift(['Unassign', ''])
                    } } do |ids, inputs|
    onboarder = inputs[:support_consultant]
    if onboarder.blank?
      onboarder = nil
    else
      onboarder = User.find(onboarder)
    end

    ids.each do |id|
      user = User.find(id)
      user.onboarder = onboarder
      user.onboarder_assigned_at = Date.today
      user.save
    end

    if onboarder
      msg = 'Assigned ' + User.find(onboarder).name + " to #{ids.length} users"

    else
      msg = "Unassigned any Support Consultant from #{ids.length} users"
    end

    redirect_to :back, notice: msg
  end

  filter :jamuser_full_name_or_email_cont, label: 'Name Or Email', as: :string
  filter :onboarder, as: :select, :collection => User.where(is_onboarder: true), label: 'Support Consultant'
  filter :onboarder_id_blank, :as => :boolean, label: 'Unassigned'
  filter :onboarding_escalation_reason_present, :as => :boolean, label: 'Escalated'
  scope("Unassigned", default: true) { |scope| scope.joins(:posa_cards).where(onboarding_status: User::ONBOARDING_STATUS_UNASSIGNED).where('posa_cards.lesson_package_type_id in (?)', LessonPackageType::AMAZON_PACKAGES + LessonPackageType::LESSON_PACKAGE_TYPES) }
  scope("Escalated") { |scope| scope.joins(:posa_cards).where('onboarding_status = ?', User::ONBOARDING_STATUS_ESCALATED).where('posa_cards.lesson_package_type_id in (?)', LessonPackageType::AMAZON_PACKAGES + LessonPackageType::LESSON_PACKAGE_TYPES) }
  scope("Needs Manual Email") { |scope| scope.joins(:posa_cards).where(onboarding_status: User::ONBOARDING_STATUS_ONBOARDED).where('(stuck_take_flesson = TRUE AND sent_admin_take_flesson_email_at is NULL) OR (stuck_take_2nd_flesson = TRUE AND sent_admin_take_2nd_flesson_email_at IS NULL) OR (stuck_take_plesson = TRUE AND sent_admin_take_plesson_email_at IS NULL)').where('posa_cards.lesson_package_type_id in (?)', LessonPackageType::AMAZON_PACKAGES + LessonPackageType::LESSON_PACKAGE_TYPES) }
  scope("Assigned") { |scope| scope.joins(:posa_cards).where('onboarding_status = ? OR onboarding_status = ?', User::ONBOARDING_STATUS_ASSIGNED, User::ONBOARDING_STATUS_EMAILED).where('posa_cards.lesson_package_type_id in (?)', LessonPackageType::AMAZON_PACKAGES + LessonPackageType::LESSON_PACKAGE_TYPES) }
  scope("All TestDrive/Amazon Users") { |scope| scope.joins(:posa_cards).where('posa_cards.lesson_package_type_id in (?)', LessonPackageType::AMAZON_PACKAGES + LessonPackageType::LESSON_PACKAGE_TYPES) }


  controller do
    active_admin_config.includes.push :onboarding_users
  end

  index do
    selectable_column
    column "Name" do |user|
      link_to user.name, user.admin_url
    end
    column :email
    column :onboarding_status
    column "Lost Reason", :onboarding_lost_reason
    column "Escalated Reason", :onboarding_escalation_reason
    column "Support Consultant" do |user|
      if user.onboarder
        link_to "#{user.onboarder.name} (#{user.onboarder.onboarding_users.where('onboarding_status = ? OR onboarding_status = ?', User::ONBOARDING_STATUS_ASSIGNED, User::ONBOARDING_STATUS_EMAILED).count})", user.onboarder.admin_url
      else
      end
    end
    column "Manual Email Needed" do |user|

      div do
        if user.stuck_take_plesson
          link_to("sent take paid lesson email", mark_sent_paid_lesson_admin_onboarder_management_path(user.id), {'data-confirm': "You sent an email manually to the user to remind them to take a paid lesson?"})
        elsif user.stuck_take_2nd_flesson
          link_to("sent take 2nd lesson email", mark_sent_2nd_free_lesson_admin_onboarder_management_path(user.id), {'data-confirm': "You sent an email manually to the user to remind them to take 2nd free lesson?"})
        elsif user.stuck_take_flesson
          link_to("sent take 1st lesson email", mark_sent_1st_free_lesson_admin_onboarder_management_path(user.id), {'data-confirm': "You sent an email manually to the user to remind them to take a 1st free lesson?"})
        else
        end
      end
    end

    column "Signup" do |user|
      user.created_at.to_date
    end
    column "Assigned",  :onboarder_assigned_at
    column "Email 1", :onboarding_email_1_sent_at
    column "Email 2", :onboarding_email_2_sent_at
    column "Email 3", :onboarding_email_3_sent_at
    column "Email 4", :onboarding_email_4_sent_at
    column "Email 5", :onboarding_email_5_sent_at
    column "Test Session", :onboarding_test_session_scheduled_at
    column "Admin Actions" do |user|
      div do
        if user.is_waiting_onboarding || user.is_onboarding
          link_to("mark onboarded", mark_onboarded_admin_onboarder_management_path(user.id), {  'data-confirm': "Mark onboarded?"})
        end
        if user.is_waiting_onboarding || user.is_onboarding
          link_to("mark lost", mark_lost_admin_onboarder_management_path(user.id), {  'data-confirm': "Mark lost (reason = Other)?"})
        end
      end
    end
    column "Onboarder Notes", :onboarding_onboarder_notes
  end

  member_action :update_onboarder, :method => :post do
    resource.onboarder = params[:onboarder]
    resource.save!
    redirect_to :back
  end

  member_action :mark_sent_paid_lesson, :method => :get do
    resource.mark_sent_paid_lesson
    redirect_to :back
  end

  member_action :mark_sent_2nd_free_lesson, :method => :get do
    resource.mark_sent_2nd_free_lesson
    redirect_to :back
  end

  member_action :mark_sent_1st_free_lesson, :method => :get do
    resource.mark_sent_1st_free_lesson
    redirect_to :back
  end

  member_action :mark_onboarded, :method => :get do
    resource.mark_onboarded
    redirect_to :back
  end

  member_action :mark_lost, :method => :get do
    resource.mark_lost
    redirect_to :back
  end

end