require 'spec_helper'

describe "Single Lesson", :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, first_name: "Teacher1", ready_for_session_at: Time.now)}
  let(:teacher_user2) {FactoryGirl.create(:teacher_user, ready_for_session_at: Time.now)}
  let(:teacher_user3) {FactoryGirl.create(:teacher_user, ready_for_session_at: Time.now)}
  let(:school) {FactoryGirl.create(:school)}


  before(:each) do
    LessonPackagePurchase.destroy_all
    LessonBooking.destroy_all
    Recording.destroy_all
    Diagnostic.destroy_all
    User.destroy_all

    UserMailer.deliveries.clear
    emulate_client
    # create an old test drive and fakely use up all the credits so that we can book the lesson


    teacher_user.teacher.ready_for_session_at = Time.now
    teacher_user.teacher.save!
    teacher_user.teacher.price_per_lesson_60_cents.should eql 3000
    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 normal" do

      Timecop.travel(Date.new(2016, 03, 01))
      testdrive_lesson(user, teacher_user3)
      user.remaining_test_drives = 0
      user.save!
      user.reload

      # the testdrive_lesson helper fakes out a credit card, but we need to stop faking and let the web UI drive us into the payment form
      user.stored_credit_card = false
      user.save!

      sign_in_poltergeist user

      visit "/client#/teachers/search"

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

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

      # book the lesson
      fill_in "slot-1-date", with: "Sun Apr 17 2016"
      #find('.slot.slot-1 input.hasDatepicker').trigger(:click)
      # click 4-6
      find('td a', text: '17').trigger(:click)

      #find('.slot.slot-2 input.hasDatepicker').trigger(:click)
      # click 4-7
      fill_in "slot-2-date", with: "Mon Apr 18 2016"
      find('td a', text: '18').trigger(:click)

      fill_in 'user-description', with: 'abc def dog neck'

      select('60 Minute Lesson for $30.00', :from => "booking-options-for-teacher")
      sleep 3

      find('a.book-lesson-btn', text: 'BOOK LESSON').trigger(:click)

      fill_out_payment('Your card will not be charged until the day of the 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.reload
      user.stripe_customer_id.should_not be_nil
      user.student_lesson_bookings.count.should eql 2 # this single one, and the test drive created in the before section of the test
      lesson_booking = user.student_lesson_bookings.order(:created_at).last
      lesson_booking.is_requested?.should be true
      lesson_booking.card_presumed_ok.should be true
      lesson_booking.recurring.should be false



      lesson_booking = LessonBooking.where(teacher_id: teacher_user).order(:created_at).last
      lesson_booking.should_not be_nil
      lesson_booking.same_school.should be false
      lesson_session = LessonSession.where(teacher_id: teacher_user).order(:created_at).last
      lesson_session.teacher.should eql teacher_user

      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id, teacher_id: teacher_user.id).order(:created_at).first
      lesson_package_purchase.should be_nil
      user.reload
      user.remaining_test_drives.should eql 0
      user.sales.count.should eql 0

      # jamclass scren
      find('h2', text: 'my lessons')
      find('tr[data-lesson-session-id="' + lesson_session.id + '"] .displayStatusColumn', text: 'Requested')
      find('tr[data-lesson-session-id="' + lesson_session.id + '"] .first_name', text: teacher_user.first_name)

      # open up hover
      find('tr[data-lesson-session-id="' + lesson_session.id + '"] .lesson-session-actions-btn').trigger(:click)

      # check out the status
      find('li[data-lesson-option="status"] a', text: 'View Status').trigger(:click)

      # and now go to the session status screen
      find('h2', text: 'your lesson has been requested')

      # let's make a second request to a different teacher now

      # let's make sure we can ask for another lesson 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-normal').trigger(:click)


      # book the lesson
      fill_in "slot-1-date", with: "Sun Apr 17 2016"
      #find('.slot.slot-1 input.hasDatepicker').trigger(:click)
      # click 4-6
      find('td a', text: '17').trigger(:click)

      #find('.slot.slot-2 input.hasDatepicker').trigger(:click)
      # click 4-7
      fill_in "slot-2-date", with: "Mon Apr 18 2016"
      find('td a', text: '18').trigger(:click)

      fill_in 'user-description', with: 'abc def dog neck'

      select('60 Minute Lesson for $30.00', :from => "booking-options-for-teacher")
      sleep 3

      find('a.book-lesson-btn', text: 'BOOK LESSON').trigger(:click)

      # 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)

      lesson_session1 = lesson_session
      lesson_booking = LessonBooking.where(teacher_id: teacher_user2).first
      lesson_booking.should_not be_nil
      lesson_session = LessonSession.where(teacher_id: teacher_user2).first
      lesson_session.teacher.should eql teacher_user2
      lesson_session2 = lesson_session

      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id, teacher_id: teacher_user.id).order(:created_at).first
      lesson_package_purchase.should be_nil
      user.reload
      user.remaining_test_drives.should eql 0
      user.sales.count.should eql 0

      # 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.success.should be true
      lesson_session1.billing_attempts.should eql 1
      lesson_session1.billed.should eql true

      LessonBooking.hourly_check

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

      # check the second session, which no one went too


      lesson_session2.reload
      lesson_session2.analysed.should be false

      # approve by teacher:
      teacher_approve(lesson_session2)
      LessonSession.hourly_check
      LessonBooking.hourly_check

      lesson_session2.reload
      lesson_session2.analysed.should be true
      analysis = lesson_session2.analysis
      analysis["reason"].should eql LessonSessionAnalyser::TEACHER_FAULT
      lesson_session2.billing_attempts.should eql 0
      lesson_session2.billed.should eql false
      lesson_session2.success.should be false

      lesson_session2.teacher_distribution.should be_nil
    end


    it "succeeds (school-on-school)" do

      teacher_user.teacher.school = school
      teacher_user.teacher.save
      user.school = school
      user.save!

      # the testdrive_lesson helper fakes out a credit card, but we need to stop faking and let the web UI drive us into the payment form
      user.stored_credit_card = false
      user.save!

      sign_in_poltergeist user

      visit "/client#/teachers/search"

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

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

      # book the lesson
      fill_in "slot-1-date", with: "Sun Apr 17 2016"
      #find('.slot.slot-1 input.hasDatepicker').trigger(:click)
      # click 4-6
      find('td a', text: '17').trigger(:click)

      #find('.slot.slot-2 input.hasDatepicker').trigger(:click)
      # click 4-7
      fill_in "slot-2-date", with: "Mon Apr 18 2016"
      find('td a', text: '18').trigger(:click)

      fill_in 'user-description', with: 'abc def dog neck'

      select('60 Minute Lesson for $30.00', :from => "booking-options-for-teacher")
      sleep 3

      find('a.book-lesson-btn', text: 'BOOK LESSON').trigger(:click)

      # 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 1 # this single one
      lesson_booking = user.student_lesson_bookings.order(:created_at).last
      lesson_booking.is_requested?.should be true
      lesson_booking.card_presumed_ok.should be false
      lesson_booking.recurring.should be false



      lesson_booking = LessonBooking.where(teacher_id: teacher_user).order(:created_at).last
      lesson_booking.should_not be_nil
      lesson_booking.school.should eql school
      lesson_booking.same_school.should be true
      lesson_session = LessonSession.where(teacher_id: teacher_user).order(:created_at).last
      lesson_session.teacher.should eql teacher_user

      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id, teacher_id: teacher_user.id).order(:created_at).first
      lesson_package_purchase.should be_nil
      user.reload
      user.remaining_test_drives.should eql 0
      user.sales.count.should eql 0

      # jamclass scren
      find('h2', text: 'my lessons')
      find('tr[data-lesson-session-id="' + lesson_session.id + '"] .displayStatusColumn', text: 'Requested')
      find('tr[data-lesson-session-id="' + lesson_session.id + '"] .first_name', text: teacher_user.first_name)

      # open up hover
      find('tr[data-lesson-session-id="' + lesson_session.id + '"] .lesson-session-actions-btn').trigger(:click)

      # check out the status
      find('li[data-lesson-option="status"] a', text: 'View Status').trigger(:click)

      # and now go to the session status screen
      find('h2', text: 'your lesson has been requested')

      # let's make a second request to a different teacher now

      # let's make sure we can ask for another lesson too!
      teacher_user2.teacher.ready_for_session_at = Time.now
      teacher_user2.teacher.save!

      visit "/client#/teachers/search"
      find('span.search-summary', text: 'From My School Only')
      find('a.teacher-search-options').trigger(:click)
      find('.icheckbox_minimal.onlyMySchool ins', visible: false).trigger(:click) # uncheck
      find('a.search-btn').trigger(:click)
      find('span.search-summary', text: 'all teachers')
      find('.teacher-search-result[data-teacher-id="' + teacher_user2.id + '"] .try-test-drive').trigger(:click)

      select_test_drive(2)

      fill_out_single_lesson

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

      sleep 5

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

      user.reload
      user.has_stored_credit_card?.should be true
      lesson_session1 = lesson_session
      lesson_booking = LessonBooking.where(teacher_id: teacher_user2.id).first
      lesson_booking.should_not be_nil
      lesson_booking.card_presumed_ok.should be true
      lesson_session = LessonSession.where(teacher_id: teacher_user2.id).first
      lesson_session.teacher.should eql teacher_user2
      lesson_session2 = lesson_session

      lesson_package_purchase = LessonPackagePurchase.where(user_id: user.id, teacher_id: teacher_user.id).order(:created_at).first
      lesson_package_purchase.should be_nil
      user.reload
      user.remaining_test_drives.should eql 1
      user.sales.count.should eql 1

      bookings = LessonBooking.where(teacher_id: lesson_session1.teacher.id)

      LessonSession.index(lesson_session1.teacher, {as_teacher:true})[:query].count.should eql 1
      # 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.success.should be true
      lesson_session1.billing_attempts.should eql nil
      lesson_session1.billed.should eql false

      LessonBooking.hourly_check

      lesson_session1.reload
      teacher_distribution = lesson_session1.teacher_distribution
      teacher_distribution.should be_nil

      # check the second session, which no one went too


      lesson_session2.reload
      lesson_session2.analysed.should be false

      # approve by teacher:
      teacher_approve(lesson_session2)
      LessonSession.hourly_check
      LessonBooking.hourly_check

      lesson_session2.reload
      lesson_session2.analysed.should be true
      analysis = lesson_session2.analysis
      analysis["reason"].should eql LessonSessionAnalyser::TEACHER_FAULT
      lesson_session2.billing_attempts.should eql 0
      lesson_session2.billed.should eql false
      lesson_session2.success.should be false

      lesson_session2.teacher_distribution.should be_nil
    end
  end
end
