class User < ApplicationRecord
  has_secure_password

  belongs_to :invited_by, class_name: "User", optional: true
  has_many :invited_users, class_name: "User", foreign_key: :invited_by_id, dependent: :nullify

  has_many :created_entries, class_name: "Entry", foreign_key: :created_by_id, dependent: :nullify
  has_many :updated_entries, class_name: "Entry", foreign_key: :updated_by_id, dependent: :nullify
  has_many :submitted_suggested_meanings,
           class_name: "SuggestedMeaning",
           foreign_key: :submitted_by_id,
           dependent: :nullify
  has_many :reviewed_suggested_meanings,
           class_name: "SuggestedMeaning",
           foreign_key: :reviewed_by_id,
           dependent: :nullify
  has_many :entry_versions, dependent: :nullify
  has_many :comments, dependent: :nullify

  enum :role, %i[contributor reviewer admin]

  validates :email, presence: true, uniqueness: true
  validates :password, length: { minimum: 12 }, if: -> { password.present? }
end
