require 'spec_helper'

describe EmailNewMusicianMatch do
  let(:user1) { FactoryGirl.create(:user, subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) }
  let(:user2) { FactoryGirl.create(:user, subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) }
  let(:user3) { FactoryGirl.create(:user, subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) }
  let(:user4) { FactoryGirl.create(:user, subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) }
  let(:user5) { FactoryGirl.create(:user, subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) }
  let(:user6) { FactoryGirl.create(:user, subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) }
  let(:user7) { FactoryGirl.create(:user, subscribe_email: false, user_match_email_sent_at: 7.days.ago) }
  let(:user8) { FactoryGirl.create(:user, subscribe_email: false, subscribe_email_for_user_match: false, user_match_email_sent_at: 7.days.ago) }
  let(:user9) { FactoryGirl.create(:user, email: 'seth@jamkazam.com', subscribe_email: true, subscribe_email_for_user_match: true, user_match_email_sent_at: 7.days.ago) } #a priority user
  let(:user10) { FactoryGirl.create(:user, email: 'david@jamkazam.com', subscribe_email: false) } #a priority user. but not included as he has marked not to receive email notifications

  let(:search_result){ [[user1, user2, user3, user4, user5], [user6, user7, user8, user9, user10] ] }

  let (:mail) { double("Mail") }
  let (:admin_mail) { double("Admin Mail") }

  before(:each) do
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    User.delete_all
    allow(JamRuby::MusicianFilter).to receive(:filter).and_return(search_result)
    Rails.application.config.send_user_match_mail_only_to_jamkazam_team = false
  end

  after(:each) do
    ActionMailer::Base.deliveries.clear
  end

  it "notify admin" do
    allow(AdminMailer).to receive(:ugly).and_return(admin_mail)
    expect(admin_mail).to receive(:deliver_now).exactly(2).times
    JamRuby::EmailNewMusicianMatch.send_new_musicians
  end

  it "does not sent to whom have not been opted to receive emails" do
    JamRuby::EmailNewMusicianMatch.send_new_musicians
    ActionMailer::Base.deliveries.map{|d| d['to'].to_s }.include?("david@example.com").should be_falsey
  end

  it "delivers the new musicians notification email" do
    allow(UserMailer).to receive(:new_musicians_match).and_return(mail)
    expect(mail).to receive(:deliver_now).exactly(7).times
    JamRuby::EmailNewMusicianMatch.send_new_musicians
  end

  it "delivers only to priority recipients" do
    Rails.application.config.send_user_match_mail_only_to_jamkazam_team = true
    allow(UserMailer).to receive(:new_musicians_match).and_return(mail)
    expect(mail).to receive(:deliver_now).exactly(1).times #only to the priority user seth@jamkazam
    JamRuby::EmailNewMusicianMatch.send_new_musicians
  end


  xit "delivers to priority recipients first" do
    JamRuby::EmailNewMusicianMatch.send_new_musicians
    raise ActionMailer::Base.deliveries.map{|d| d['to'].to_s }.inspect #.include?("seth@example.com").should be_truthy
    #ActionMailer::Base.deliveries[1]['to'].to_s.should == "seth@jamkazam.com" #NOTE: the first email is sent to user_match_monitoring_email. The second email should be sent to the first priority user in the priority user list
  end

  describe "halfway done job" do
    before(:each) do
      UserMailer.deliveries.clear
      allow_any_instance_of(UserMatchEmailSending).to receive(:sent_user_ids).and_return([user1.id, user2.id])
    end

   it "does not deliver to already delivered users" do
      allow(UserMailer).to receive(:new_musicians_match).and_return(mail)
      expect(mail).to receive(:deliver_now).exactly(5).times
      JamRuby::EmailNewMusicianMatch.send_new_musicians
    end
  end

  describe "catching errors" do

    before do 
      JamRuby::MusicianFilter.stub(:filter).and_raise
    end
   
    it 'notifies admin about the error' do
      JamRuby::EmailNewMusicianMatch.send_new_musicians
      ActionMailer::Base.deliveries.length.should == 2
      ActionMailer::Base.deliveries[1]['subject'].to_s.should == "Error occured when sending weekly user match email."
    end
  end
  

end