require 'rails_helper'

RSpec.describe Order::Verbal::TranslatorsQueue, :type => :model do

  describe '.create' do
    let!(:translator1){create :profile}
    let!(:translator2){create :profile}

    subject{Order::Verbal::TranslatorsQueue.create translators: [translator1, translator2]}

    it{expect{subject}.to change{translator1.reload.order_verbal_translators_queues.count}.by(1)}
    it{expect{subject}.to change{translator2.reload.order_verbal_translators_queues.count}.by(1)}
  end


  describe  Order::Verbal::TranslatorsQueue::Factory do
    RSpec.shared_examples 'queue builder' do
      it{is_expected.to be_a Order::Verbal::TranslatorsQueue}
      it{expect{subject}.to change{order.reload.translators_queues.count}.by(1)}
    end

    let(:factory){Order::Verbal::TranslatorsQueue::Factory.new(order)}

    describe '#create_agent_queue' do
      subject{factory.create_agent_queue}

      context 'order has not agents' do
        let(:order){create :order_verbal}

        it{is_expected.to be_nil}
      end

      context 'agent does not support order' do
        let(:order){create :order_verbal, referral_link: create(:user).referral_link}

        it{is_expected.to be_nil}
      end

      context 'agent support order' do
        let(:city){create :city}
        let(:language){create :language, senior: create(:profile)}
        let(:user) do
          create :user, profile: create(:profile,
                                        services: [build(:service, is_approved: true, language: language, level: 'guide')],
                                        city_approves: [build(:city_approve, is_approved: true, city: city)])
        end
        let(:translator){user.profile}
        let(:order){create :order_verbal, referral_link: user.referral_link, location: city, language: language, translators: [translator]}

        it_behaves_like 'queue builder'

        it{expect(subject.lock_to).to eq DateTime.now}
        it{expect(subject.translators).to include translator}
        it{expect{subject}.to change{translator.reload.order_verbal_translators_queues.count}.by(1)}
      end
    end


    describe '#create_senior_queue' do
      let(:city){create :city}
      let(:language){create :language}
      let!(:senior){create :profile, city_approves: [build(:city_approve, is_approved: true, city: city)],
                           services: [build(:service, is_approved: true, language: language, level: 'guide')]}
      let(:private_translator) do
        create :user, profile: create(:profile,
                                      services: [build(:service, is_approved: true, language: language, level: 'guide')],
                                      city_approves: [build(:city_approve, is_approved: true, city: city)]
        )
      end
      context 'senior is selected' do
        let(:order){create :waiting_for_confirmations_order, location: city, language: language, translators: [senior, private_translator.profile]}

        subject{factory.create_senior_queue}

        before(:each){private_translator; language.update_attributes senior: senior}

        it_behaves_like 'queue builder'

        it{expect(subject.translators).to include senior}
        it{expect(subject.translators).not_to include private_translator.profile}
        it{expect{subject}.to change{senior.reload.order_verbal_translators_queues.count}.by(1)}
      end

      context 'senior is not selected' do
        let(:order){create :waiting_for_confirmations_order, location: city, language: language, translators: [private_translator.profile]}

        subject{factory.create_senior_queue}

        before(:each){private_translator; language.update_attributes senior: senior}

        it{is_expected.to be_nil}
      end
    end

    describe '#create_without_surcharge_queue' do
      let(:city){create :city}
      let(:language){create :language}
      let(:translator_1){create(:profile,
                                services: [build(:service, is_approved: true, language: language, level: 'business')],
                                city_approves: [build(:city_approve, is_approved: true, city: city)] )}

      let(:translator_2){create(:profile,
                                services: [build(:service, is_approved: true, language: language, level: 'business')],
                                city_approves: [build(:city_approve, is_approved: true, city: city)] )}


      subject{factory.create_without_surcharge_queue}

      let(:city_approve_1){}

      before(:each) do
        Order::Verbal::TranslatorsQueue.create order_verbal: order, translators: [translator_1]
        create(:city_approve, is_approved: true, city: city, profile: translator_1)
        create(:city_approve, is_approved: true, city: city, profile: translator_2)
        translator_2
      end


      context 'low level order' do
        let(:order){create :waiting_for_confirmations_order, location: city, language: language, level: 'business',
                           include_near_city: false, translators: [translator_1, translator_2]}
        it{expect(subject.translators).to include translator_2}
        it{expect(subject.translators).not_to include translator_1}
        it{expect{subject}.to change{translator_2.reload.order_verbal_translators_queues.count}.by(1)}
        it_behaves_like 'queue builder'
      end

      context 'normal level order' do
        let(:order){create :waiting_for_confirmations_order, location: city, language: language, level: 'guide',
                           include_near_city: false, translators: [translator_1, translator_2]}
        it{expect(subject.translators).to include translator_2}
        it{expect(subject.translators).not_to include translator_1}
        it{expect{subject}.to change{translator_2.reload.order_verbal_translators_queues.count}.by(1)}
        it_behaves_like 'queue builder'
      end

    end
  end

 
end
