ActiveAdmin.register JamRuby::LessonSession, :as => 'LessonSessions' do

  menu :label => 'Lesson Session', :parent => 'JamClass'

  config.sort_order = 'created_at desc'
  config.batch_actions = false
  config.per_page = 100
  config.paginate = true
  config.filters = true

  scope("All", default: true) { |scope| scope.order('created_at desc') }
  scope("Requested") { |scope| scope.where(status: LessonBooking::STATUS_REQUESTED).order('created_at desc') }
  scope("Approved") { |scope| scope.approved.order('created_at desc') }
  scope("Suspended") { |scope| scope.suspended.order('created_at desc') }
  scope("Canceled") { |scope| scope.canceled.order('created_at desc') }
  scope("Missed") { |scope| scope.missed.order('created_at desc') }
  scope("Completed") { |scope| scope.completed.order('created_at desc') }

  #filter :jamuser_full_name_or_user_email_cont, label: 'Student Name', as: :string

  #filter :teacher_user_email_cont, label: 'Student Name', as: :string

  filter :student_full_name_or_user_email_cont, label: 'Student Name Or Email', as: :string
  filter :by_search_teacher_in, label: 'Teacher Name Or Email', as: :string
  filter :by_lesson_package_type_in, label: 'Card Type', as: :select, collection: -> { LessonPackageType.all.map { |t| [t.admin_name, t.id] } }

  controller do
    active_admin_config.includes.push :teacher, :music_session
  end

  action_item :only => [:show] do
    link_to('Mark As Success', mark_success_admin_lesson_session_path(resource.id))
  end

  member_action :mark_success, :method => :get do
    if !resource.success
      resource.mark_lesson(true, true)
    end
    redirect_to :back

  end

  index do
    column "Actions" do |teacher|
      links = ''.html_safe
      links << link_to("View", resource_path(teacher), :class => "member_link view_link")
      links << link_to("Edit", edit_resource_path(teacher), :class => "member_link edit_link")
      links
    end
    column "App Link" do |lesson_session|
      lesson_booking = lesson_session.lesson_booking
      span do
        link_to "link", "#{Rails.application.config.external_root_url}/client#/jamclass/lesson-booking/#{lesson_booking.id}"
      end
    end
    column "Status" do |lesson_session|
      link_to lesson_session.status, admin_lesson_session_path(lesson_session.id)
    end
    column "Start Time" do |lesson_session|
      span do
        if lesson_session.music_session.nil?
          raise "Lessonsesison with no id #{lesson_session.id}"
        else
          lesson_session.music_session.pretty_scheduled_start(true)
        end
      end
      br
      span do
        lesson_session.music_session.scheduled_start
      end
    end
    column "Duration" do |lesson_session|
      lesson_session.duration
    end
    column "Teacher" do |lesson_session|
      teacher = lesson_session.teacher
      span do
        # link_to "#{teacher.name} (#{teacher.email})", "#{Rails.application.config.external_root_url}/client#/profile/teacher/#{teacher.id}"
        link_to teacher.admin_name, teacher.admin_url
      end
    end
    column "Student" do |lesson_session|
      student = lesson_session.student
      span do
        #link_to "#{student.name} (#{student.email})", "#{Rails.application.config.external_root_url}/client#/profile/#{student.id}"
        link_to student.admin_name, student.admin_url
      end
    end
  end

  show do
    attributes_table do
      row "Lesson Booking" do |lesson_session|
        span do
          link_to "Lesson Booking", lesson_session.lesson_booking.admin_url
        end
      end
      row "App Link" do |lesson_session|
        lesson_booking = lesson_session.lesson_booking
        span do
          link_to "link", "#{Rails.application.config.external_root_url}/client#/jamclass/lesson-booking/#{lesson_booking.id}"
        end
      end
      row "Status" do |lesson_session|
        lesson_session.status
      end
      row "Start Time" do |lesson_session|
        span do
          lesson_session.music_session.pretty_scheduled_start(true)
        end
        br
        span do
          lesson_session.music_session.scheduled_start
        end
      end
      row "Duration" do |lesson_session|
        lesson_session.duration
      end
      row "Teacher" do |lesson_session|
        teacher = lesson_session.teacher
        span do
          # link_to teacher.admin_name, "#{Rails.application.config.external_root_url}/client#/profile/teacher/#{teacher.id}"
          link_to teacher.admin_name, teacher.admin_url
        end
      end
      row "Student" do |lesson_session|
        student = lesson_session.student
        span do
          # link_to student.admin_name, "#{Rails.application.config.external_root_url}/client#/profile/#{student.id}"
          link_to student.admin_name, student.admin_url
        end
      end
      row "Followup Emails Sent" do |lesson_session|
        span do
          lesson_session.sent_notices
        end
      end
      row "Success" do |lesson_session|
        span do
          lesson_session.success
        end
      end
      row "Billed" do |lesson_session|
        span do
          if lesson_session.is_test_drive?
            'N/A (because is Test Drive)'
          else
            lesson_session.billed
          end
        end
      end
      row "Distribution" do |lesson_session|
        distribution = lesson_session.teacher_distribution

        if distribution
          payment = distribution.teacher_payment

          if payment
            charge = payment.teacher_payment_charge
          end
        end

        div do
          if distribution
            table_for distribution do
              column :amount_in_cents
              column :distributed
              column :ready
              column :teacher_fee_in_cents
              column :reduced_roll_forward_amount_in_cents
              column :created_at
              column :updated_at
              column do
                link_to "Link", distribution.admin_url
              end
            end
          else
            ''
          end
        end

      end
      row "Teacher Payment" do |lesson_session|
        distribution = lesson_session.teacher_distribution

        if distribution
          payment = distribution.teacher_payment
        end

        div do
          if payment
            table_for payment do
              column :amount_in_cents
              column :fee_in_cents
              column :created_at
              column :updated_at
              column do
                link_to "Link", payment.admin_url
              end
            end
          else
            ''
          end
        end
      end
      row "Stripe Charge" do |lesson_session|
        distribution = lesson_session.teacher_distribution

        if distribution
          payment = distribution.teacher_payment
          if payment
            charge = payment.teacher_payment_charge
          end
        end

        div do
          if charge
            table_for charge do
              column :amount_in_cents
              column :fee_in_cents
              column :billed
              column :billed_at
              column :post_processed
              column :post_processed_at
              column :billing_error_reason
              column :billing_error_detail
              column :billing_should_retry
              column :billing_attempts
              column :stripe_charge_id
              column :created_at
              column :updated_at
              column do
                link_to "Stripe", "https://dashboard.stripe.com/payments/#{charge.stripe_charge_id}" if charge.stripe_charge_id
              end
              column do
                link_to "Link", charge.admin_url
              end
            end
          else
            ''
          end
        end
      end



      row "Admin Marked" do |lesson_session|
        span do
          lesson_session.admin_marked
        end
      end
      row "Description" do |lesson_session|
        span do
          lesson_session.timed_description
        end
      end
      row "Session" do |lesson_session|
        span do
          link_to "Session", lesson_session.music_session.admin_url
        end
      end
      row "Analysis" do |lesson_session|
        if lesson_session.analysed
          span style: "white-space: pre;" do
            begin
              JSON.pretty_generate(lesson_session.analysis_json)
            rescue
              "barf"
            end
          end
        end
      end
      row "Chat Messages" do |lesson_session|
        div do
          table_for ChatMessage.unscoped.where(lesson_session: lesson_session).order(:created_at) do
            column :created_at
            column do |chat_msg|
              chat_msg.purpose ? chat_msg.purpose : 'Chat Msg'
            end
            column 'Sender' do |chat_msg|
              link_to(chat_msg.user.name, chat_msg.user.admin_url)
            end
            column :target_user do |chat_msg|
              chat_msg.target_user ? link_to(chat_msg.target_user.name, chat_msg.target_user.admin_url) : ''
            end
            column :message
            column :id
          end
        end
      end
    end

  end

end