# == Schema Information
#
# Table name: complaints
#
#  id                  :integer          not null, primary key
#  description         :text
#  user_id             :integer
#  complainable_id    :integer
#  complainable_type  :string
#  complaint_reason_id :integer
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#

class Complaint < ApplicationRecord
  # *current_user* create complaint about *complaint_reason* for *complaintable*
  include Trackable

  belongs_to :complaint_reason
  belongs_to :user
  belongs_to :complainable, polymorphic: true

  validates_presence_of :user, :complainable, :description, :complaint_reason
  validates :user_id, uniqueness: { scope: [:complainable_id, :complainable_type] }

  acts_as_paranoid
  has_paper_trail

  def reason
    complaint_reason.reason
  end

  def recipient
    complainable
  end
end