require 'spec_helper'

describe LessonSessionMonthlyPrice do

  let(:user) { FactoryGirl.create(:user) }
  let(:teacher) { FactoryGirl.create(:teacher_user) }
  let(:start) { Time.now }
  let(:lesson_booking_slot_single1) { FactoryGirl.build(:lesson_booking_slot_single) }
  let(:lesson_booking_slot_single2) { FactoryGirl.build(:lesson_booking_slot_single) }
  let(:lesson_booking_slot_recurring1) { FactoryGirl.build(:lesson_booking_slot_recurring) }
  let(:lesson_booking_slot_recurring2) { FactoryGirl.build(:lesson_booking_slot_recurring) }
  let(:valid_single_slots) { [lesson_booking_slot_single1, lesson_booking_slot_single2] }
  let(:valid_recurring_slots) { [lesson_booking_slot_recurring1, lesson_booking_slot_recurring2] }

  describe "price" do
    after do
      Timecop.return
    end

    it "start of the month" do

      jan1 = Date.new(2016, 1, 1)
      Timecop.freeze(jan1)

      booking = LessonBooking.book_normal(user, teacher, valid_recurring_slots, "Monthly time!", true, LessonBooking::PAYMENT_STYLE_MONTHLY, 60)

      booking.booked_price.should eql 30.00

      price, times = LessonSessionMonthlyPrice.price(booking, jan1)
      booking.booked_price.should eql(price)
      times.should eql(5)
    end

    it "middle of the month" do

      jan17 = Date.new(2016, 1, 17)
      Timecop.freeze(jan17)

      booking = LessonBooking.book_normal(user, teacher, valid_recurring_slots, "Monthly time!", true, LessonBooking::PAYMENT_STYLE_MONTHLY, 60)

      booking.booked_price.should eql 30.00

      price, times = LessonSessionMonthlyPrice.price(booking, jan17)
      ((booking.booked_price * 0.75).round(2)).should eql(price)
      times.should eql(3)

    end

    it "end of the month which has a last billable day based on slot" do

      jan17 = Date.new(2016, 1, 31)
      Timecop.freeze(jan17)

      booking = LessonBooking.book_normal(user, teacher, valid_recurring_slots, "Monthly time!", true, LessonBooking::PAYMENT_STYLE_MONTHLY, 60)

      booking.booked_price.should eql 30.00

      price, times = LessonSessionMonthlyPrice.price(booking, jan17)
      ((booking.booked_price * 0.25).round(2)).should eql(price)
      times.should eql(1)
    end

    it "end of the month which is not a last billable days" do

      feb29 = Date.new(2016, 2, -1)
      Timecop.freeze(feb29)

      booking = LessonBooking.book_normal(user, teacher, valid_recurring_slots, "Monthly time!", true, LessonBooking::PAYMENT_STYLE_MONTHLY, 60)

      booking.booked_price.should eql 30.00

      price, times = LessonSessionMonthlyPrice.price(booking, feb29)
      ((booking.booked_price * 0.0).round(2)).should eql(price)
      times.should eql(0)
    end
  end
end
