class PhotographerPaymentLine < InvoicingRecord
  include Hashable

  belongs_to :photographer_payment
  belongs_to :photographer_billing

  after_create :set_photographer_billing_paid

  def computed_hash_string
    Digest::SHA2.new(256).hexdigest [
      previous_hash_string,
      photographer_payment_id,
      photographer_billing_id,
      amount,
      photographer_payment&.created_at&.utc&.strftime('%Y-%m-%d %H:%M:%S'),
      photographer_payment&.photographer_billing_company_id,
      photographer_payment&.payment_type_id,
      photographer_payment&.payment_date&.strftime('%Y-%m-%d')
    ].join('/')
  end

  private

  def set_photographer_billing_paid
    photographer_billing.update(paid: photographer_billing.photographer_payment_lines.pluck(:amount).reduce(0, :+))
  end
end
