# frozen_string_literal: true

# == Schema Information
#
# Table name: user_payout_method_checks
#
#  id                  :bigint           not null, primary key
#  address_city        :text             not null
#  address_country     :text             not null
#  address_line1       :text             not null
#  address_line2       :text
#  address_postal_code :text             not null
#  address_state       :text             not null
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#
class User
  module PayoutMethod
    class Check < ApplicationRecord
      include Shared

      self.table_name = "user_payout_method_checks"
      validates_presence_of :address_line1, :address_city, :address_postal_code
      validates_presence_of :address_state, message: "Please select a state!"
      validates :address_state, inclusion: { in: ISO3166::Country.new("US").subdivisions.keys, message: "This isn't a valid US state!", allow_blank: true }
      validates :address_postal_code, format: { with: /\A\d{5}(?:[-\s]\d{4})?\z/, message: "This isn't a valid ZIP code." }
      attribute :address_country, :text, default: "US"

      validate do
        combined_length = [address_line1, address_line2].filter(&:present?).sum(&:length)

        if combined_length > 50
          errors.add(:base, "Address line one and line two's combined length can not exceed 50 characters.")
        end
      end

      def kind
        "check"
      end

      def icon
        "email"
      end

      def name
        "a check"
      end

      def human_kind
        "check"
      end

      def title_kind
        "Check"
      end

      def currency
        "USD"
      end

    end
  end

end
