# frozen_string_literal: true

require 'rails_helper'
require 'rspec_api_documentation/dsl'

resource 'Verifications' do
  before do
    @user = create(:user)
    header_token_for @user
    header 'Content-Type', 'application/json'
    configuration = AppConfiguration.instance
    settings = configuration.settings
    settings['verification'] = {
      allowed: true,
      enabled: true,
      verification_methods: [{
        name: 'id_card_lookup',
        ui_method_name: 'By social security number',
        card_id: 'Social security number',
        card_id_tooltip: 'You can find this number on you card. We just check, we don\'t store it',
        card_id_placeholder: 'xx-xxxxx-xx',
        explainer_image_url: 'https://some.fake/image.png'
      }]
    }
    configuration.save!
  end

  post 'web_api/v1/verification_methods/id_card_lookup/verification' do
    with_options scope: :verification do
      parameter :card_id
    end

    describe do
      before do
        @card_id = '123.46234-78B'
        create(:id_id_card_lookup_id_card, card_id: @card_id)
      end

      let(:card_id) { @card_id }

      example_request 'Verify with id_card_lookup' do
        assert_status 201
        expect(@user.reload.verified).to be true
      end
    end

    describe do
      let(:card_id) { '234.532345-345' }

      example_request '[error] Verify with id_card_lookup without a match' do
        assert_status 422
        json_response = json_parse response_body
        expect(json_response).to include_response_error(:base, 'no_match')
      end
    end

    describe do
      let(:card_id) { '' }

      example_request '[error] Verify with id_card_lookup using empty card_id' do
        assert_status 422
        json_response = json_parse response_body
        expect(json_response).to include_response_error(:card_id, 'invalid')
      end
    end

    describe do
      before do
        other_user = create(:user)
        @card_id = '123.46234-78B'
        create(:id_id_card_lookup_id_card, card_id: @card_id)
        Verification::VerificationService.new.verify_sync(
          user: other_user,
          method_name: 'id_card_lookup',
          verification_parameters: { card_id: @card_id }
        )
      end

      let(:card_id) { '123.4623478B' }

      example_request '[error] Verify with id_card_lookup using credentials that are already taken (2nd call)' do
        assert_status 422
        json_response = json_parse response_body
        expect(json_response).to include_response_error(:base, 'taken')
      end
    end
  end
end
