<commit_before># ## Schema Information
#
# Table name: `escrow_transactions`
#
# ### Columns
#
# Name                   | Type               | Attributes
# ---------------------- | ------------------ | ---------------------------
# **`id`**               | `integer`          | `not null, primary key`
# **`from_id`**          | `integer`          |
# **`to_id`**            | `integer`          |
# **`state`**            | `integer`          |
# **`amount`**           | `decimal(, )`      |
# **`created_at`**       | `datetime`         | `not null`
# **`updated_at`**       | `datetime`         | `not null`
# **`conversation_id`**  | `integer`          |
#
# ### Indexes
#
# * `index_escrow_transactions_on_conversation_id`:
#     * **`conversation_id`**
# * `index_escrow_transactions_on_from_id`:
#     * **`from_id`**
# * `index_escrow_transactions_on_to_id`:
#     * **`to_id`**
#
# ### Foreign Keys
#
# * `fk_rails_0c4e4a285d`:
#     * **`to_id => accounts.id`**
# * `fk_rails_abcc2686a1`:
#     * **`from_id => accounts.id`**
#

# A record of funds held, paid or reversed escrow
class EscrowTransaction < ApplicationRecord
  belongs_to :from, foreign_key: :from_id, class_name: 'Account'
  belongs_to :to, foreign_key: :to_id, class_name: 'Account'
  belongs_to :opening_transaction, foreign_key: :opening_transaction_id, class_name: 'Transaction'
  belongs_to :closing_transaction, foreign_key: :closing_transaction_id, class_name: 'Transaction'

  before_validation :determine_amount
  before_create :set_defaults
  before_create :create_escrow_transaction

  validates :amount,
            presence: true,
            numericality: { greater_than_or_equal_to: 0 }

  enum state: {
    pending: 1, # money is in escrow
    completed: 2, # money was delivered to original recepient
    reversed: 3 # money was reversed and delivered to original sender
  }

  # mark the transaction as completed and pay out the rate
  def complete
    EscrowTransaction.transaction do
      self.state = "completed"
      Account.find_by(account_type: 'escrow').transfer(
        amount,
        to,
        'payment'
      )
    end
  end

  def reverse
    EscrowTransaction.transaction do
      self.state = 'reversed'
      Account.find_by(account_type: 'escrow').transfer(<commit_msg>Fix linter error<commit_after># ## Schema Information
#
# Table name: `escrow_transactions`
#
# ### Columns
#
# Name                   | Type               | Attributes
# ---------------------- | ------------------ | ---------------------------
# **`id`**               | `integer`          | `not null, primary key`
# **`from_id`**          | `integer`          |
# **`to_id`**            | `integer`          |
# **`state`**            | `integer`          |
# **`amount`**           | `decimal(, )`      |
# **`created_at`**       | `datetime`         | `not null`
# **`updated_at`**       | `datetime`         | `not null`
# **`conversation_id`**  | `integer`          |
#
# ### Indexes
#
# * `index_escrow_transactions_on_conversation_id`:
#     * **`conversation_id`**
# * `index_escrow_transactions_on_from_id`:
#     * **`from_id`**
# * `index_escrow_transactions_on_to_id`:
#     * **`to_id`**
#
# ### Foreign Keys
#
# * `fk_rails_0c4e4a285d`:
#     * **`to_id => accounts.id`**
# * `fk_rails_abcc2686a1`:
#     * **`from_id => accounts.id`**
#

# A record of funds held, paid or reversed escrow
class EscrowTransaction < ApplicationRecord
  belongs_to :from, foreign_key: :from_id, class_name: 'Account'
  belongs_to :to, foreign_key: :to_id, class_name: 'Account'
  belongs_to :opening_transaction, foreign_key: :opening_transaction_id, class_name: 'Transaction'
  belongs_to :closing_transaction, foreign_key: :closing_transaction_id, class_name: 'Transaction'

  before_validation :determine_amount
  before_create :set_defaults
  before_create :create_escrow_transaction

  validates :amount,
            presence: true,
            numericality: { greater_than_or_equal_to: 0 }

  enum state: {
    pending: 1, # money is in escrow
    completed: 2, # money was delivered to original recepient
    reversed: 3 # money was reversed and delivered to original sender
  }

  # mark the transaction as completed and pay out the rate
  def complete
    EscrowTransaction.transaction do
      self.state = 'completed'
      Account.find_by(account_type: 'escrow').transfer(
        amount,
        to,
        'payment'
      )
    end
  end

  def reverse
    EscrowTransaction.transaction do
      self.state = 'reversed'
      Account.find_by(account_type: 'escrow').transfer(
{'fraction_non_alphanumeric': 0.15795230802887772, 'fraction_numerical': 0.00656311529205863, 'mean_word_length': 2.574667709147772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '223555', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1460, 'n_words': 311}