require 'spec_helper'

describe "Test Drive", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) { FactoryGirl.create(:user, traditional_band: true,paid_sessions: true, paid_sessions_hourly_rate: 1,  paid_sessions_daily_rate:1 ) }
  let(:teacher_user) {FactoryGirl.create(:teacher_user, ready_for_session_at: Time.now)}
  let(:teacher_user2) {FactoryGirl.create(:teacher_user, ready_for_session_at: Time.now)}
  let(:card_lessons) {FactoryGirl.create(:posa_card_lesson_4)}
  let(:retailer) {FactoryGirl.create(:retailer)}

  before(:each) do
    LessonSession.update_all(lesson_package_purchase_id:nil)
    LessonPackagePurchase.destroy_all
    LessonBooking.destroy_all
    Recording.destroy_all
    Diagnostic.destroy_all
    User.destroy_all

    UserMailer.deliveries.clear
    emulate_client
    sign_in_poltergeist user


    teacher_user.teacher.ready_for_session_at = Time.now
    teacher_user.teacher.save!

    Teacher.index(user, {})[:query].count.should eql 1
  end

  describe "register via showing interesting in teacher 1st" do
    after(:each) do
      Timecop.return
    end

    it "succeeds" do

      visit "/client#/teachers/search"

      Timecop.travel(Date.new(2016, 04, 01))

      find('.teacher-search-result[data-teacher-id="' + teacher_user.id + '"] .try-test-drive').trigger(:click)

      # no longer true
      # TryTestDriveDialog shows
      #find('.purchase-testdrive-now').trigger(:click)

      select_test_drive(4)

      fill_out_single_lesson

      fill_out_payment('This purchase entitles you to take 4 private online music')

      # we tell user they have test drive purchased, and take them to the teacher screen
      find('#banner h1', text: 'TestDrive Purchased')
      find('#banner .dialog-inner', text: "You have purchased #{4} TestDrive credits and have used 1 credit to request a JamClass with #{teacher_user.name}")

      # dismiss banner
      find('a.button-orange', text:'CLOSE').trigger(:click)

      # validate that we made a test drive purchase
      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id).first
      lesson_package_purchase.should_not be_nil
      lesson_package_purchase.lesson_package_type.is_test_drive?.should be true
      lesson_package_purchase.lesson_payment_charge.should be_nil
      user.reload
      user.remaining_test_drives.should eql 3
      #lesson_package_purchase.amount_charged.should eql 49.99
      user.sales.count.should eql 1
      sale = user.sales.first
      sale.recurly_total_in_cents.should eql 5411


      find('h2', text: 'my lessons')

      user.reload
      user.student_lesson_bookings.count.should eql 1
      lesson_booking = user.student_lesson_bookings.first
      lesson_booking.is_requested?.should be true
      user.remaining_test_drives.should eql 3
      lesson_booking.lesson_sessions.count.should eql 1
      lesson_session1 = lesson_booking.lesson_sessions.first

      user.most_recent_test_drive_purchase.should_not be_nil
      # let's make sure we can ask for another test drive too!
      teacher_user2.teacher.ready_for_session_at = Time.now
      teacher_user2.teacher.save!

      visit "/client#/teachers/search"
      find('a.teacher-search-options').trigger(:click)
      find('a.search-btn').trigger(:click)

      find('.teacher-search-result[data-teacher-id="' + teacher_user2.id + '"] .try-test-drive').trigger(:click)

      find('h2', text: 'book testdrive lesson')
      find('.booking-info', text: '3 TestDrive lesson credits')

      fill_out_single_lesson

      # we tell user they have test drive purchased, and take them to the teacher screen
      find('#banner h1', text: 'Lesson Requested')
      # dismiss banner
      find('a.button-orange', text:'CLOSE').trigger(:click)

      user.student_lesson_bookings.count.should eql 2
      lesson_booking2 = user.student_lesson_bookings.order(:created_at).last
      lesson_booking2.teacher.should eql teacher_user2
      lesson_booking2.lesson_sessions[0].lesson_package_purchase.should_not be_nil

      # approve by teacher:
      teacher_approve(lesson_session1)

      successful_lesson(lesson_session1)

      LessonSession.hourly_check
      lesson_session1.reload
      lesson_session1.analysed.should be true
      analysis = lesson_session1.analysis
      analysis["reason"].should eql LessonSessionAnalyser::SUCCESS
      lesson_session1.billing_attempts.should be_nil
      lesson_session1.billed.should eql false
      lesson_session1.success.should be true

      LessonBooking.hourly_check

      lesson_session1.reload
      teacher_distribution = lesson_session1.teacher_distribution
      teacher_distribution.amount_in_cents.should eql 1000
      teacher_distribution.ready.should be true
      teacher_distribution.distributed.should be false

    end

    it "succeeds with posa card" do

      PosaCard.activate(card_lessons, retailer)
      card_lessons.reload
      card_lessons.claim(user)
      card_lessons.errors.any?.should be false


      visit "/client#/teachers/search"

      Timecop.travel(Date.new(2016, 04, 01))

      find('.teacher-search-result[data-teacher-id="' + teacher_user.id + '"] .try-test-drive').trigger(:click)

      # no longer true
      # TryTestDriveDialog shows
      #find('.purchase-testdrive-now').trigger(:click)

      # dismiss banner

      fill_out_single_lesson

      find('#banner h1', text: 'Lesson Requested')
      find('a.button-orange', text:'CLOSE').trigger(:click)


      # we tell user they have test drive purchased, and take them to the teacher screen
      #find('#banner h1', text: 'TestDrive Purchased')
      #find('#banner .dialog-inner', text: "You have purchased #{4} TestDrive credits and have used 1 credit to request a JamClass with #{teacher_user.name}")

      # dismiss banner
      #find('a.button-orange', text:'CLOSE').trigger(:click)

      # validate that we made a test drive purchase
      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id).first
      lesson_package_purchase.should_not be_nil
      lesson_package_purchase.lesson_package_type.is_test_drive?.should be true
      lesson_package_purchase.posa_card.should eql card_lessons
      lesson_package_purchase.lesson_payment_charge.should be_nil
      user.reload
      user.remaining_test_drives.should eql 0
      user.jamclass_credits.should eql 3
      #lesson_package_purchase.amount_charged.should eql 49.99
      user.sales.count.should eql 1
      sale = user.sales.first
      sale.order_total.should eql 49.99
      sale.recurly_total_in_cents.should be_nil


      find('h2', text: 'my lessons')

      user.reload
      user.student_lesson_bookings.count.should eql 1
      lesson_booking = user.student_lesson_bookings.first
      lesson_booking.is_requested?.should be true
      user.remaining_test_drives.should eql 0
      lesson_booking.lesson_sessions.count.should eql 1
      lesson_session1 = lesson_booking.lesson_sessions.first

      user.most_recent_test_drive_purchase.should_not be_nil
      # let's make sure we can ask for another test drive too!
      teacher_user2.teacher.ready_for_session_at = Time.now
      teacher_user2.teacher.save!

      visit "/client#/teachers/search"
      find('a.teacher-search-options').trigger(:click)
      find('a.search-btn').trigger(:click)

      find('.teacher-search-result[data-teacher-id="' + teacher_user2.id + '"] .try-test-drive').trigger(:click)

      find('h2', text: 'book testdrive lesson')
      find('.booking-info', text: '3 TestDrive lesson credits')

      fill_out_single_lesson

      # we tell user they have test drive purchased, and take them to the teacher screen
      find('#banner h1', text: 'Lesson Requested')
      # dismiss banner
      find('a.button-orange', text:'CLOSE').trigger(:click)

      user.student_lesson_bookings.count.should eql 2
      lesson_booking2 = user.student_lesson_bookings.order(:created_at).last
      lesson_booking2.teacher.should eql teacher_user2
      lesson_booking2.lesson_sessions[0].lesson_package_purchase.should_not be_nil

      # approve by teacher:
      teacher_approve(lesson_session1)

      successful_lesson(lesson_session1)

      LessonSession.hourly_check
      lesson_session1.reload
      lesson_session1.analysed.should be true
      analysis = lesson_session1.analysis
      analysis["reason"].should eql LessonSessionAnalyser::SUCCESS
      lesson_session1.billing_attempts.should be_nil
      lesson_session1.billed.should eql false
      lesson_session1.success.should be true

      LessonBooking.hourly_check

      lesson_session1.reload
      teacher_distribution = lesson_session1.teacher_distribution
      teacher_distribution.amount_in_cents.should eql 1000
      teacher_distribution.ready.should be true
      teacher_distribution.distributed.should be false

    end

    it "same teacher with posa card succeeds" do

      PosaCard.activate(card_lessons, retailer)
      card_lessons.reload
      card_lessons.claim(user)
      card_lessons.errors.any?.should be false


      visit "/client#/teachers/search"

      #Timecop.travel(Date.new(2016, 04, 01))

      find('.teacher-search-result[data-teacher-id="' + teacher_user.id + '"] .try-test-drive').trigger(:click)

      # no longer true
      # TryTestDriveDialog shows
      #find('.purchase-testdrive-now').trigger(:click)

      # dismiss banner

      fill_out_single_lesson(Time.now.to_date + 1, Time.now.to_date + 2)

      find('#banner h1', text: 'Lesson Requested')
      find('a.button-orange', text:'CLOSE').trigger(:click)


      # we tell user they have test drive purchased, and take them to the teacher screen
      #find('#banner h1', text: 'TestDrive Purchased')
      #find('#banner .dialog-inner', text: "You have purchased #{4} TestDrive credits and have used 1 credit to request a JamClass with #{teacher_user.name}")

      # dismiss banner
      #find('a.button-orange', text:'CLOSE').trigger(:click)

      # validate that we made a test drive purchase
      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id).first
      lesson_package_purchase.should_not be_nil
      lesson_package_purchase.lesson_package_type.is_test_drive?.should be true
      lesson_package_purchase.posa_card.should eql card_lessons
      lesson_package_purchase.lesson_payment_charge.should be_nil
      user.reload
      user.remaining_test_drives.should eql 0
      user.jamclass_credits.should eql 3
      #lesson_package_purchase.amount_charged.should eql 49.99
      user.sales.count.should eql 1
      sale = user.sales.first
      sale.order_total.should eql 49.99
      sale.recurly_total_in_cents.should be_nil






      user.reload
      user.onboarding_status.should eql User::ONBOARDING_STATUS_UNASSIGNED
      user.student_lesson_bookings.count.should eql 1
      lesson_booking = user.student_lesson_bookings.first
      lesson_booking.is_requested?.should be true
      user.remaining_test_drives.should eql 0
      lesson_booking.lesson_sessions.count.should eql 1

      lesson_session1 = lesson_booking.lesson_sessions.first
      # approve by teacher:
      teacher_approve(lesson_session1)
      successful_lesson(lesson_session1)

      # then log back in as student
      switch_user(user, "/client#/teachers/search")


      user.most_recent_test_drive_purchase.should_not be_nil
      # let's make sure we can ask for another test drive too!
      teacher_user.teacher.ready_for_session_at = Time.now
      teacher_user.teacher.save!

      find('a.teacher-search-options').trigger(:click)
      find('a.search-btn').trigger(:click)

      find('.teacher-search-result[data-teacher-id="' + teacher_user.id + '"] .try-test-drive').trigger(:click)

      find('h2', text: 'book testdrive lesson')
      find('.booking-info', text: '3 TestDrive lesson credits')

      fill_out_single_lesson(Time.now.to_date + 1, Time.now.to_date + 2)

      # we tell user they have test drive purchased, and take them to the teacher screen
      find('#banner h1', text: 'Lesson Requested')
      # dismiss banner
      find('a.button-orange', text:'CLOSE').trigger(:click)

      user.student_lesson_bookings.count.should eql 2
      lesson_booking2 = user.student_lesson_bookings.order(:created_at).last
      lesson_booking2.teacher.should eql teacher_user
      lesson_session2 = lesson_booking2.lesson_sessions[0]
      lesson_session2.lesson_package_purchase.should_not be_nil

      # approve by teacher:
      teacher_approve(lesson_session2)

      successful_lesson(lesson_session2)

      LessonSession.hourly_check
      lesson_session1.reload
      lesson_session1.analysed.should be true
      analysis = lesson_session1.analysis
      analysis["reason"].should eql LessonSessionAnalyser::SUCCESS
      lesson_session1.billing_attempts.should be_nil
      lesson_session1.billed.should eql false
      lesson_session1.success.should be true


      LessonBooking.hourly_check

      lesson_session1.reload
      teacher_distribution = lesson_session1.teacher_distribution
      teacher_distribution.amount_in_cents.should eql 1000
      teacher_distribution.ready.should be true
      teacher_distribution.distributed.should be false

      lesson_session2.reload
      teacher_distribution = lesson_session2.teacher_distribution
      teacher_distribution.amount_in_cents.should eql 1000
      teacher_distribution.ready.should be true
      teacher_distribution.distributed.should be false

      user.reload
      user.first_onboarding_paid_lesson_at.should_not be_nil
      user.onboarding_status.should eql User::ONBOARDING_STATUS_PAID_LESSON

    end
  end
end
