# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Edools::Invitation do
  let(:school) {
    Edools::School.create name: 'Test',
                          email: 'test@test.com',
                          password: '12345678'
  }

  let(:invitation) {
    Edools::Invitation.new first_name: 'test',
                           email: 'student@test.com',
                           password: '123456',
                           password_confirmation: '123456',
                           enrollments:[
                             {
                               id: 1,
                               status: 'active',
                               school_product_id: 1111
                             },
                             {
                               id: 2,
                               status: 'active',
                               school_product_id: 2222
                             },
                             {
                               id: 3,
                               status: 'inactive',
                               school_product_id: 3333
                             },
                             {
                               id: 4,
                               status: 'active',
                               school_product_id: 3333
                             }
                           ]
  }

  describe '#create' do
    context 'when valid params' do
      it 'returns an instance of Edools::Invitation' do
        VCR.use_cassette('invitation/create_valid') do
          school.set_as_global_environment
          expect(invitation).to be_an_instance_of Edools::Invitation
        end
      end
    end

    context 'when invalid params' do
      let(:invitation) { Edools::Invitation.create foo: '1' }

      it 'returns an instance of Edools::Invitation' do
        VCR.use_cassette('invitation/create_invalid_instance') do
          school.set_as_global_environment
          expect(invitation).to be_an_instance_of Edools::Invitation
        end
      end

      it 'has errors' do
        VCR.use_cassette('invitation/create_invalid_errors') do
          school.set_as_global_environment
          expect(invitation.errors).not_to be_empty
        end
      end
    end


    xcontext 'when without params' do
      let(:invitation) { Edools::Invitation.create }

      it 'raises Edools::BadRequest' do
        school.set_as_global_environment
        expect { invitation }.to raise_error Edools::BadRequest
      end
    end
  end

  describe '#active_enrollment_by' do
    subject { invitation.active_enrollment_by(school_product_id) }

    context 'when all enrollment are active' do
      let(:school_product_id) { 2222 }

      it { expect(subject[:id]).to eq(2) }
      it { expect(subject[:status]).to eq('active') }
    end

    context 'when some enrollment is inactive' do
      let(:school_product_id) { 3333 }

      it { expect(subject[:id]).not_to be_between(1, 3) }
      it { expect(subject[:id]).to eq(4) }
      it { expect(subject[:status]).to eq('active') }
    end

    context 'when school_product_id has no enrollment' do
      let(:school_product_id) { 1234 }

      it { expect(subject).to eq(nil) }
    end
  end
end
