# frozen_string_literal: true

module Idv
  module FormStateIdValidator
    extend ActiveSupport::Concern

    # rubocop:disable Metrics/BlockLength
    included do
      validates :first_name,
                :last_name,
                :dob,
                :identity_doc_address1,
                :identity_doc_city,
                :state_id_jurisdiction,
                :state_id_number,
                :id_expiration,
                :same_address_as_id,
                presence: true

      validates_with UspsInPersonProofing::TransliterableValidator,
                     fields: [:first_name, :last_name, :identity_doc_city],
                     reject_chars: /[^A-Za-z\-' ]/,
                     message: ->(invalid_chars) do
                       I18n.t(
                         'in_person_proofing.form.state_id.errors.unsupported_chars',
                         char_list: invalid_chars.join(', '),
                       )
                     end

      validates_with UspsInPersonProofing::TransliterableValidator,
                     fields: [:identity_doc_address1, :identity_doc_address2],
                     reject_chars: /[^A-Za-z0-9\-' .\/#]/,
                     message: ->(invalid_chars) do
                       I18n.t(
                         'in_person_proofing.form.state_id.errors.unsupported_chars',
                         char_list: invalid_chars.join(', '),
                       )
                     end
      # rubocop:disable Layout/LineLength
      validates_with UspsInPersonProofing::DateValidator,
                     attributes: [:dob], less_than_or_equal_to: ->(_rec) {
                       Time.zone.today - IdentityConfig.store.idv_min_age_years.years
                     },
                     message: ->(_, _) do
                       I18n.t(
                         'in_person_proofing.form.state_id.memorable_date.errors.date_of_birth.range_min_age',
                         app_name: APP_NAME,
                       )
                     end
      # rubocop:enable Layout/LineLength
      # rubocop:disable Layout/LineLength
      validates_with UspsInPersonProofing::DateValidator,
                     attributes: [:id_expiration], greater_than_or_equal_to: ->(_rec) {
                       Time.zone.today + 2.days
                     },
                     message: ->(_, _) do
                       I18n.t(
                         'in_person_proofing.form.state_id.memorable_date.errors.expiration_date.expired',
                         app_name: APP_NAME,
                       )
                     end
      # rubocop:enable Layout/LineLength
    end
    # rubocop:enable Metrics/BlockLength
  end
end
