class Payjunction::Account < ApplicationRecord
  audited

  enum payment_type: [:ach, :credit_card]

  belongs_to :school

  has_one :convenience_fee

  validates :account_id, uniqueness: true

  validate :account_enablement

  def self.open
    find_by(status: true)
  end

  private
    def account_enablement
      return unless status

      accounts = school.payjunction_accounts
        .where(payment_type: payment_type)
        .where(status: true)

      return if accounts.empty?

      errors.add(:base, 'Only one account type may be enabled at once')
    end
end
