require 'rails_helper'

RSpec.describe DocumentCaptureSessionResult do
  let(:id) { SecureRandom.uuid }
  let(:success) { true }
  let(:pii) { { 'first_name' => 'Testy', 'last_name' => 'Testerson' } }

  context 'EncryptedRedisStructStorage' do
    it 'works with EncryptedRedisStructStorage' do
      result = DocumentCaptureSessionResult.new(
        id: id,
        success: success,
        doc_auth_success: success,
        selfie_status: :success,
        pii: pii,
        attention_with_barcode: false,
        attempt: 3,
      )
      EncryptedRedisStructStorage.store(result)
      loaded_result = EncryptedRedisStructStorage.load(id, type: DocumentCaptureSessionResult)

      expect(loaded_result).to have_attributes(
        id:,
        success:,
        pii: pii.deep_symbolize_keys,
        attention_with_barcode: false,
        selfie_status: :success,
        doc_auth_success: true,
        attempt: 3,
      )
    end

    it 'persists mrz_status with EncryptedRedisStructStorage' do
      result = DocumentCaptureSessionResult.new(
        id: id,
        success: success,
        doc_auth_success: success,
        selfie_status: :success,
        mrz_status: :pass,
        pii: pii,
        attention_with_barcode: false,
      )
      EncryptedRedisStructStorage.store(result)
      loaded_result = EncryptedRedisStructStorage.load(id, type: DocumentCaptureSessionResult)

      expect(loaded_result.mrz_status).to eq(:pass)
    end

    it 'add fingerprint with EncryptedRedisStructStorage' do
      result = DocumentCaptureSessionResult.new(
        id: id,
        success: success,
        pii: pii,
        attention_with_barcode: false,
      )
      result.add_failed_front_image!('abcdefg')
      expect(result.failed_front_image_fingerprints.is_a?(Array)).to eq(true)
      expect(result.failed_front_image_fingerprints.length).to eq(1)
      expect(result.failed_front_image?('abcdefg')).to eq(true)
      expect(result.failed_front_image?(nil)).to eq(false)
      expect(result.failed_back_image?(nil)).to eq(false)
    end

    describe '#selfie_status' do
      it 'returns a symbol' do
        result = DocumentCaptureSessionResult.new(
          id: id,
          success: success,
          pii: pii,
          attention_with_barcode: false,
          selfie_status: 'success',
        )
        expect(result.selfie_status).to be_an_instance_of(Symbol)
      end
    end

    describe '#mrz_status' do
      it 'returns a symbol when present' do
        result = DocumentCaptureSessionResult.new(
          id: id,
          success: success,
          pii: pii,
          attention_with_barcode: false,
          mrz_status: 'pass',
        )
        expect(result.mrz_status).to eq(:pass)
      end

      it 'returns nil when not present' do
        result = DocumentCaptureSessionResult.new(
          id: id,
          success: success,
          pii: pii,
          attention_with_barcode: false,
        )
        expect(result.mrz_status).to be_nil
      end
    end

    describe '#success?' do
      it 'returns the value in the success attribute' do
        result = DocumentCaptureSessionResult.new(
          id: id,
          success: true,
          pii: nil,
          attention_with_barcode: false,
          selfie_status: :fail,
          doc_auth_success: false,
        )
        expect(result.success?).to eq(true)
      end
    end
  end
end
