# frozen_string_literal: true module BrayniacAI class AppearanceReleaseMatching class << self def match_attachments(request_data) # TODO: Send request with request_data and receive real response mock_response request_data end private def mock_response(request_data) attachments = request_data[:files] pdfs = [] images = [] matches = [] errors = [] # Use first file for the error entry first_attachment = attachments.shift unless first_attachment.nil? errors << { file: first_attachment, error_code: 1 } end attachments.each do |attachment| blob = ActiveStorage::Blob.find_signed attachment next if blob.nil? if blob.image? images << attachment else pdfs << attachment end end # Create pairs of matches and single headshot/contract after pairs are exhausted more_pdfs = pdfs.length > images.length pairs = more_pdfs ? pdfs.zip(images) : images.zip(pdfs) pairs.each do |pair_element1, pair_element2| if more_pdfs pdf = pair_element1 image = pair_element2 else pdf = pair_element2 image = pair_element1 end if pdf.nil? || image.nil? matches << { headshot: image } if pdf.nil? matches << { contract: pdf } if image.nil? else matches << { headshot: image, contract: pdf, identifier: '' } end end { request_id: request_data[:request_id], matches: matches, errors: errors } end end end end