require 'spec_helper'

describe Teacher do

  let(:user)        { FactoryGirl.create(:user) }
  let(:genre1)      { FactoryGirl.create(:genre) }
  let(:genre2)      { FactoryGirl.create(:genre) }
  let(:subject1)    { FactoryGirl.create(:subject) }
  let(:subject2)    { FactoryGirl.create(:subject) }
  let(:language1)   { FactoryGirl.create(:language) }
  let(:language2)   { FactoryGirl.create(:language) }
  let(:instrument1) { FactoryGirl.create(:instrument, :description => 'a great instrument')}
  let(:instrument2) { FactoryGirl.create(:instrument, :description => 'an ok instrument')}

  describe "match_teacher" do
    it "works" do
      Teacher.match_teacher(user).should be_nil

      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      Teacher.match_teacher(user).should eql teacher

      teacher2 = FactoryGirl.create(:teacher, ready_for_session_at: nil)
      Teacher.match_teacher(user).should eql teacher

      teacher.ready_for_session_at = nil
      teacher.save!
      teacher2.ready_for_session_at = Time.now
      teacher2.save!
      Teacher.match_teacher(user).should eql teacher2
    end
  end
  describe "index" do
    it "no params" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil)[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "sorting" do

      teacher3 = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teacher3.user.email = 'phantom+dogpound@jamkazam.com'
      teacher3.user.save!


      teacher2 = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teacher2.random_order = 1
      teacher2.save!


      teacher1 = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teacher1.random_order = 2
      teacher1.save!

      query = Teacher.index(nil, {})[:query]
      query.count.should eql 3
      query[0].should eq(teacher2.user)
      query[1].should eq(teacher1.user)
      query[2].should eq(teacher3.user)
    end

    it "hide phantoms with guitar as instrument" do
      query = Teacher.index(nil, {})[:query]
      query.count.should eql 0

      teacher3 = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teacher3.teachers_instruments.count.should eql 0
      teacher3.teachers_instruments << TeacherInstrument.new
      teacher3.teachers_instruments[0].teacher = teacher3
      teacher3.teachers_instruments[0].instrument = Instrument.find('electric guitar')
      teacher3.save!
      teacher3.user.email = 'phantom+dogpound@jamkazam.com'
      teacher3.user.save!
      teacher3.user.reload
      teacher3.user.phantom.should be_true

      query = Teacher.index(nil, {})[:query]
      query.count.should eql 0
    end


    it "instruments" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      #teachers = Teacher.index(nil, {instruments: ['acoustic guitar']})[:query]
      #teachers.length.should eq 0

      teacher.instruments << Instrument.find('acoustic guitar')
      teacher.save!
      #teachers = Teacher.index(nil, {instruments: ['acoustic guitar']})[:query]
      #teachers.length.should eq 1
      #teachers[0].should eq(teacher.user)

      #teacher.instruments << Instrument.find('electric guitar')
      #teacher.save!
      teachers = Teacher.index(nil, {instruments: ['acoustic guitar']})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "subjects" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {subjects: ['music-theory']})[:query]
      teachers.length.should eq 0

      teacher.subjects << Subject.find('music-theory')
      teacher.save!
      teachers = Teacher.index(nil, {subjects: ['music-theory']})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "genres" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {genres: ['ambient']})[:query]
      teachers.length.should eq 0

      teacher.genres << Genre.find('ambient')
      teacher.save!
      teachers = Teacher.index(nil, {genres: ['ambient']})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end


    it "languages" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {languages: ['EN']})[:query]
      teachers.length.should eq 0

      teacher.languages << Language.find('EN')
      teacher.save!
      teachers = Teacher.index(nil, {languages: ['EN']})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "country" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {country: 'DO'})[:query]
      teachers.length.should eq 0

      teacher.user.country = 'DO'
      teacher.user.save!
      teachers = Teacher.index(nil, {country: 'DO'})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "region" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {region: 'HE'})[:query]
      teachers.length.should eq 0

      teacher.user.state = 'HE'
      teacher.user.save!
      teachers = Teacher.index(nil, {region: 'HE'})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "years_teaching" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now, years_teaching: 5)
      teachers = Teacher.index(nil, {years_teaching: 10})[:query]
      teachers.length.should eq 0

      teachers = Teacher.index(nil, {years_teaching: 2})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "teaches beginner/intermediate/advanced" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {teaches_beginner: true})[:query]
      teachers.length.should eq 0

      teacher.teaches_beginner = true
      teacher.save!
      teachers = Teacher.index(nil, {teaches_beginner: true})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)

      teachers = Teacher.index(nil, {teaches_intermediate: true})[:query]
      teachers.length.should eq 0

      teachers = Teacher.index(nil, {teaches_beginner: true, teaches_intermediate: true})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)

      teachers = Teacher.index(nil, {teaches_beginner: true, teaches_intermediate: true, teaches_advanced: true})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)
    end

    it "student_age" do
      teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now)
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 1

      teacher.teaches_age_lower = 5
      teacher.save!
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)

      teacher.teaches_age_lower = 6
      teacher.save!
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 0

      teacher.teaches_age_lower = 4
      teacher.teaches_age_upper = 6
      teacher.save!
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)

      teacher.teaches_age_lower = 0
      teacher.teaches_age_upper = 5
      teacher.save!
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)

      teacher.teaches_age_lower = 0
      teacher.teaches_age_upper = 6
      teacher.save!
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 1
      teachers[0].should eq(teacher.user)

      teacher.teaches_age_lower = 0
      teacher.teaches_age_upper = 4
      teacher.save!
      teachers = Teacher.index(nil, {student_age: 5})[:query]
      teachers.length.should eq 0
    end

    describe "guitarcenter" do
      before(:each) do
        gc = GuitarCenter.init
        @gc_owner = gc[:user]
        @gc_school = gc[:school]
        @gc_retailer = gc[:retailer]
      end

      it "student only sees guitar center teachers" do
        teacher = FactoryGirl.create(:teacher, ready_for_session_at: Time.now, random_order: 1)
        gc_teacher = FactoryGirl.create(:teacher, school: @gc_school, ready_for_session_at: Time.now, random_order: 2)

        # TODO: perhaps GC teachers should not come back to non-GC users.  Not sure yet.
        query = Teacher.index(user, {})[:query]
        query.count.should eql 2
        query[0].should eq(teacher.user)
        query[1].should eq(gc_teacher.user)

        user.school = @gc_school
        user.save!

        query = Teacher.index(user, {})[:query]
        query.count.should eql 1
        query[0].should eq(gc_teacher.user)

        # double-check that even if you ask for teachers not in your school, you still get only GC
        query = Teacher.index(user, {onlyMySchool: 'false'})[:query]
        query.count.should eql 1
        query[0].should eq(gc_teacher.user)
      end
    end
  end

  BIO = "Once a man learned a guitar."
  GOOD_YOUTUBE_URL = "http://youtube.com/watch?v=1234567890"
  describe "can create" do
    it "a simple teacher" do
      teacher = Teacher.new
      teacher.user = user
      teacher.biography = BIO
      teacher.introductory_video = GOOD_YOUTUBE_URL
      teacher.save.should be_true
      t = Teacher.find(teacher.id)
      t.biography.should == BIO
      t.introductory_video.should == GOOD_YOUTUBE_URL
    end


    it "with instruments" do
      teacher = Teacher.build_teacher(user, {})
      teacher.save
      teacher.instruments << instrument1
      teacher.instruments << instrument2
      teacher.save.should be_true
      t = Teacher.find(teacher.id)
      t.instruments.should have(2).items
    end

  end

  describe "using save_teacher can create" do
    it "introduction" do
      teacher = Teacher.save_teacher(
        user,
        biography: BIO,
        introductory_video: GOOD_YOUTUBE_URL,
        years_teaching: 21,
        years_playing: 12
      )
      teacher.should_not be_nil
      teacher.user.should eql user
      teacher.errors.should be_empty
      teacher.id.should_not be_nil
      t = Teacher.find(teacher.id)
      t.biography.should == BIO
      t.introductory_video.should == GOOD_YOUTUBE_URL
      t.years_teaching.should == 21
      t.years_playing.should == 12
    end

    it "basics" do
      teacher_user = FactoryGirl.create(:teacher_user)
      teacher = Teacher.save_teacher(
        teacher_user,
        instruments: [instrument1.id, instrument2.id],
        subjects: [subject1.id, subject2.id],
        genres: [genre1.id, genre2.id],
        languages: [language1.id, language2.id],
        teaches_age_lower: 10,
        teaches_age_upper: 20,
        teaches_beginner: true,
        teaches_intermediate: false,
        teaches_advanced: true
      )

      teacher.should_not be_nil
      teacher.errors.should be_empty

      t = Teacher.find(teacher.id)

      # Instruments
      t.instruments.should have(2).items

      # Genres
      t.genres.should have(2).items

      # Subjects
      t.subjects.should have(2).items

      # Languages
      t.languages.should have(2).items

      t.teaches_age_lower.should == 10
      t.teaches_age_upper.should == 20
      t.teaches_beginner.should be_true
      t.teaches_intermediate.should be_false
      t.teaches_advanced.should be_true

    end

    it "experience" do
      experience = [{
          name: "Professor",
          organization: "SHSU",
          start_year: 1994,
          end_year: 2004
        }
      ]

      teacher = Teacher.save_teacher(user, experiences_teaching: experience)
      teacher.should_not be_nil
      teacher.errors.should be_empty


      t = Teacher.find(teacher.id)
      t.should_not be_nil


      t.teacher_experiences.should have(1).items
      t.experiences_teaching.should have(1).items
      t.experiences_education.should have(0).items
      t.experiences_award.should have(0).items

      # Save some awards and re-check teacher object:
      teacher = Teacher.save_teacher(user, experiences_award: experience)
      teacher.should_not be_nil
      teacher.errors.should be_empty

      t.reload

      t.teacher_experiences.should have(2).items
      t.experiences_teaching.should have(1).items
      t.experiences_education.should have(0).items
      t.experiences_award.should have(1).items


    end

    it "lesson pricing" do
      teacher = Teacher.save_teacher(
        user,
        prices_per_lesson: true,
        prices_per_month: true,
        lesson_duration_30: true,
        lesson_duration_45: true,
        lesson_duration_60: true,
        lesson_duration_90: true,
        lesson_duration_120: true,
        price_per_lesson_30_cents: 3000,
        price_per_lesson_45_cents: 3000,
        price_per_lesson_60_cents: 3000,
        price_per_lesson_90_cents: 3000,
        price_per_lesson_120_cents: 3000,
        price_per_month_30_cents: 5000,
        price_per_month_45_cents: 5000,
        price_per_month_60_cents: 5000,
        price_per_month_90_cents: 5000,
        price_per_month_120_cents: 5000
      )

      teacher.should_not be_nil
      teacher.id.should_not be_nil
      teacher.errors.should be_empty

      t = Teacher.find(teacher.id)
      t.prices_per_lesson.should be_true
      t.prices_per_month.should be_true
      t.lesson_duration_30.should be_true
      t.lesson_duration_45.should be_true
      t.lesson_duration_60.should be_true
      t.lesson_duration_90.should be_true
      t.lesson_duration_120.should be_true
      t.price_per_lesson_30_cents.should == 3000
      t.price_per_lesson_45_cents.should == 3000
      t.price_per_lesson_60_cents.should == 3000
      t.price_per_lesson_90_cents.should == 3000
      t.price_per_lesson_120_cents.should == 3000
      t.price_per_month_30_cents.should == 5000
      t.price_per_month_45_cents.should == 5000
      t.price_per_month_60_cents.should == 5000
      t.price_per_month_90_cents.should == 5000
      t.price_per_month_120_cents.should == 5000
    end
  end

  describe "validates" do
    it "barebones" do
      teacher = Teacher.save_teacher(user, validate_introduction: true, biography: "Teaches for dat school")
      teacher.errors.should be_empty

    end
    it "introduction" do
      teacher = Teacher.save_teacher(
        user,
        years_teaching: 21,
        validate_introduction: true
      )

      teacher.should_not be_nil
      teacher.id.should be_nil
      teacher.errors.should_not be_empty

      teacher.errors.should have_key(:biography)
    end

    it "introductory video" do
      teacher = Teacher.save_teacher(
        user,
        biography: BIO,
        introductory_video: "fubar.com/nothing",
        validate_introduction: true
      )

      teacher.should_not be_nil
      teacher.id.should be_nil
      teacher.errors.should_not be_empty

      teacher.errors.should have_key(:introductory_video)

      user.reload
      teacher = Teacher.save_teacher(
        user,
        biography: BIO,
        introductory_video: GOOD_YOUTUBE_URL,
        validate_introduction: true
      )

      teacher.should_not be_nil
      teacher.id.should_not be_nil
      teacher.errors.should be_empty

    end

    it "basics" do
      teacher = Teacher.save_teacher(
        user,
       # instruments: [instrument1, instrument2],
       # subjects: [subject1, subject2],
       # genres: [genre1, genre2],
       # languages: [language1, language2],
        teaches_age_lower: 10,
        teaches_beginner: true,
        teaches_intermediate: false,
        teaches_advanced: true,
        validate_basics: true
      )

      teacher.should_not be_nil
      teacher.errors.should have_key(:instruments)
      teacher.errors.should have_key(:subjects)
      teacher.errors.should have_key(:genres)
      teacher.errors.should have_key(:languages)
      Teacher.find_by_id(teacher.id).should be_nil
    end

    it "pricing" do
      teacher = Teacher.save_teacher(
        user,
        prices_per_lesson: false,
        prices_per_month: false,
        lesson_duration_30: false,
        lesson_duration_45: false,
        lesson_duration_60: false,
        lesson_duration_90: false,
        lesson_duration_120: false,
        #price_per_lesson_30_cents: 3000,
        price_per_lesson_45_cents: 3000,
        #price_per_lesson_60_cents: 3000,
        #price_per_lesson_90_cents: 3000,
        price_per_lesson_120_cents: 3000,
        validate_pricing:true
      )

      teacher.should_not be_nil
      teacher.id.should be_nil
      teacher.errors.should_not be_empty
      teacher.errors.should have_key(:offer_pricing)
      teacher.errors.should have_key(:offer_duration)

      teacher = Teacher.save_teacher(
        user,
        prices_per_month: true,
        lesson_duration_45: true,
        validate_pricing:true
      )

      teacher.should_not be_nil
      teacher.id.should_not be_nil
      teacher.errors.should be_empty

    end # pricing
  end # validates
end # spec
