=begin

# verifies that teacher directed emails go to the right location
require "spec_helper"


# https://jamkazam.atlassian.net/browse/VRFS-4128#add-comment
# * *teacher-always* : the teacher always gets this email
# * *school-over-teacher*: if there is a school, then the school gets the email if it has the scheduling communication pref set to true; otherwise, just teacher
# * *school-and-teacher*: if there is a school, send to both school and teacher; otherwise, just teacher



describe "TeacherLessonEmails" do

  def school_over_teacher
    if @noSchool
      mail.to.should eql [teacher.email]
    elsif @schoolDirectComm
      mail.to.should eql [teacher.email]
    else
      mail.to.should eql [school.communication_email]
    end
  end

  def teacher_always
    mail.to.should eql [teacher.email]
  end

  def student_always
    mail.to.should eql [user.email]
  end

  def school_and_teacher
    if @noSchool
      mail.to.should eql [teacher.email]
    elsif @schoolDirectComm
      mail.to.should eql [teacher.email]
    else
      mail.to.should eql [school.communication_email, teacher.email]
    end
  end

  before(:each) {
    UserMailer.deliveries.clear
    @noSchool = @schooDirectComm = @schoolSchedulingComm = false
  }
  # lessons with no school affiliation
  describe "no-school" do

    let(:user) { FactoryGirl.create(:user) }
    let(:teacher) { FactoryGirl.create(:teacher_user) }
    let(:lesson) { normal_lesson(user, teacher) }
    let(:mail) { UserMailer.deliveries[-1] }

    before(:each) {
      @noSchool = true
    }

    it "teacher_lesson_request" do
      UserMailer.teacher_lesson_request(lesson.lesson_booking).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_accepted" do
      UserMailer.teacher_lesson_accepted(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_update_all" do
      UserMailer.teacher_lesson_update_all(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_counter" do
      UserMailer.teacher_lesson_counter(lesson, lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_normal_done" do
      UserMailer.teacher_lesson_normal_done(lesson).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_completed" do
      UserMailer.teacher_lesson_completed(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_test_drive_no_bill" do
      UserMailer.teacher_test_drive_no_bill(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_normal_no_bill" do
      UserMailer.teacher_lesson_normal_no_bill(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_unable_charge_monthly" do
      pending "insufficient test setup"
      paid = monthly_lesson(user, teacher, {accept:true, finish: true})
      paid.reload
      UserMailer.teacher_unable_charge_monthly(paid.lesson_package_purchase).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_monthly_charged" do
      pending "insufficient test setup"
      paid = monthly_lesson(user, teacher, {accept:true, finish: true})
      paid.reload
      UserMailer.teacher_lesson_monthly_charged(paid.lesson_package_purchase).deliver_now
      school_over_teacher
    end

    it "teacher_distribution_done" do
      teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson))
      UserMailer.teacher_distribution_done(teacher_payment).deliver_now
      school_over_teacher
    end

    it "teacher_distribution_fail" do
      teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson))
      UserMailer.teacher_distribution_fail(teacher_payment).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_booking_canceled" do
      canceled_lesson = normal_lesson(user, teacher, {cancel: true})
      UserMailer.teacher_lesson_booking_canceled(canceled_lesson.lesson_booking, "Sorry I can't teach anymore!").deliver_now
      school_and_teacher
    end

    it "teacher_lesson_canceled" do
      canceled_lesson = normal_lesson(user, teacher, {cancel: true})
      UserMailer.teacher_lesson_canceled(canceled_lesson, "Sorry I can't teach anymore!").deliver_now
      school_and_teacher
    end

    it "lesson_chat" do
      msg = ChatMessage.create(user, nil, "nathuntoehun ", "lesson", "abc", teacher, lesson)
      UserMailer.lesson_chat(msg).deliver_now
    end

    it "teacher_counter_reminder" do
      UserMailer.teacher_counter_reminder(lesson).deliver_now
      school_over_teacher
    end

    it "lesson_starting_soon_teacher" do
      UserMailer.lesson_starting_soon_teacher(lesson).deliver_now
      teacher_always
    end
  end

  # lessons with school affiliation, but teacher gets all communication
  describe "school-but-direct-comm" do
    before(:each) {
      @schoolDirectComm = true
    }
    let(:school) { FactoryGirl.create(:school, scheduling_communication: School::SCHEDULING_COMM_TEACHER) }
    let(:user) { FactoryGirl.create(:user) }
    let(:teacher_obj) { FactoryGirl.create(:teacher, school: school) }
    let(:teacher) { FactoryGirl.create(:user, teacher: teacher_obj) }
    let(:lesson) { normal_lesson(user, teacher) }
    let(:mail) { UserMailer.deliveries[-1] }


    it "teacher_lesson_request" do
      UserMailer.teacher_lesson_request(lesson.lesson_booking).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_accepted" do
      UserMailer.teacher_lesson_accepted(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_update_all" do
      UserMailer.teacher_lesson_update_all(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_counter" do
      UserMailer.teacher_lesson_counter(lesson, lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_normal_done" do
      UserMailer.teacher_lesson_normal_done(lesson).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_completed" do
      UserMailer.teacher_lesson_completed(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_test_drive_no_bill" do
      UserMailer.teacher_test_drive_no_bill(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_normal_no_bill" do
      UserMailer.teacher_lesson_normal_no_bill(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_unable_charge_monthly" do
      pending "insufficient test setup"
      paid = monthly_lesson(user, teacher, {accept:true, finish: true})
      paid.reload
      UserMailer.teacher_unable_charge_monthly(paid.lesson_package_purchase).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_monthly_charged" do
      pending "insufficient test setup"
      paid = monthly_lesson(user, teacher, {accept:true, finish: true})
      paid.reload
      UserMailer.teacher_lesson_monthly_charged(paid.lesson_package_purchase).deliver_now
      school_over_teacher
    end

    it "teacher_distribution_done" do
      teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson))
      UserMailer.teacher_distribution_done(teacher_payment).deliver_now
      school_over_teacher
    end

    it "teacher_distribution_fail" do
      teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson))
      UserMailer.teacher_distribution_fail(teacher_payment).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_booking_canceled" do
      canceled_lesson = normal_lesson(user, teacher, {cancel: true})
      UserMailer.teacher_lesson_booking_canceled(canceled_lesson.lesson_booking, "Sorry I can't teach anymore!").deliver_now
      school_and_teacher
    end

    it "teacher_lesson_canceled" do
      canceled_lesson = normal_lesson(user, teacher, {cancel: true})
      UserMailer.teacher_lesson_canceled(canceled_lesson, "Sorry I can't teach anymore!").deliver_now
      school_and_teacher
    end

    it "lesson_chat" do
      msg = ChatMessage.create(user, nil, "nathuntoehun ", "lesson", "abc", teacher, lesson)
      UserMailer.lesson_chat(msg).deliver_now
    end

    it "teacher_counter_reminder" do
      UserMailer.teacher_counter_reminder(lesson).deliver_now
      school_over_teacher
    end

    it "lesson_starting_soon_teacher" do
      UserMailer.lesson_starting_soon_teacher(lesson).deliver_now
      teacher_always
    end

  end

  # lessons with school affiliation, and they want communication
  describe "school-with-scheduling-comm" do
    before(:each) {
      @schoolSchedulingComm = true
    }
    let(:school) { FactoryGirl.create(:school, scheduling_communication: School::SCHEDULING_COMM_SCHOOL) }
    let(:user) { FactoryGirl.create(:user) }
    let(:teacher_obj) { FactoryGirl.create(:teacher, school: school) }
    let(:teacher) { FactoryGirl.create(:user, teacher: teacher_obj) }
    let(:lesson) { normal_lesson(user, teacher) }
    let(:mail) { UserMailer.deliveries[-1] }

    it "teacher_lesson_request" do
      UserMailer.teacher_lesson_request(lesson.lesson_booking).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_accepted" do
      UserMailer.teacher_lesson_accepted(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_update_all" do
      UserMailer.teacher_lesson_update_all(lesson, "come along now and teach", lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_counter" do
      UserMailer.teacher_lesson_counter(lesson, lesson.lesson_booking.lesson_booking_slots[0]).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_normal_done" do
      UserMailer.teacher_lesson_normal_done(lesson).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_completed" do
      UserMailer.teacher_lesson_completed(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_test_drive_no_bill" do
      UserMailer.teacher_test_drive_no_bill(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_lesson_normal_no_bill" do
      UserMailer.teacher_lesson_normal_no_bill(lesson).deliver_now
      school_and_teacher
    end

    it "teacher_unable_charge_monthly" do
      pending "insufficient test setup"
      paid = monthly_lesson(user, teacher, {accept:true, finish: true})
      paid.reload
      UserMailer.teacher_unable_charge_monthly(paid.lesson_package_purchase).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_monthly_charged" do
      pending "insufficient test setup"
      paid = monthly_lesson(user, teacher, {accept:true, finish: true})
      paid.reload
      UserMailer.teacher_lesson_monthly_charged(paid.lesson_package_purchase).deliver_now
      school_over_teacher
    end

    it "teacher_distribution_done" do
      teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson))
      UserMailer.teacher_distribution_done(teacher_payment).deliver_now
      school_over_teacher
    end

    it "teacher_distribution_fail" do
      teacher_payment = FactoryGirl.create(:teacher_payment, teacher: teacher, teacher_distribution: FactoryGirl.create(:teacher_distribution, lesson_session: lesson))
      UserMailer.teacher_distribution_fail(teacher_payment).deliver_now
      school_over_teacher
    end

    it "teacher_lesson_booking_canceled" do
      canceled_lesson = normal_lesson(user, teacher, {cancel: true})
      UserMailer.teacher_lesson_booking_canceled(canceled_lesson.lesson_booking, "Sorry I can't teach anymore!").deliver_now
      school_and_teacher
    end

    it "teacher_lesson_canceled" do
      canceled_lesson = normal_lesson(user, teacher, {cancel: true})
      UserMailer.teacher_lesson_canceled(canceled_lesson, "Sorry I can't teach anymore!").deliver_now
      school_and_teacher
    end

    it "lesson_chat" do
      msg = ChatMessage.create(user, nil, "nathuntoehun ", "lesson", "abc", teacher, lesson)
      UserMailer.lesson_chat(msg).deliver_now
    end

    it "teacher_counter_reminder" do
      UserMailer.teacher_counter_reminder(lesson).deliver_now
      school_over_teacher
    end

    it "lesson_starting_soon_teacher" do
      UserMailer.lesson_starting_soon_teacher(lesson).deliver_now
      teacher_always
    end
  end
end
=end