# frozen_string_literal: true

class User < ApplicationRecord
  include ::Searchable

  extend FriendlyId
  friendly_id :full_name, use: :slugged

  LATIN_LETTERS_REGEX = /[a-zA-Z]+/.freeze

  mount_uploader :cover, SpeakerCoverUploader

  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable,
         :validatable, :omniauthable, omniauth_providers: Devise.omniauth_providers

  has_many :identities,                      dependent: :destroy
  has_many :talks, foreign_key: :speaker_id, dependent: :nullify
  has_many :visit_requests,                  dependent: :destroy

  validates :first_name, :last_name, :slug, presence: true
  validates :first_name, :last_name, format: {
    with: LATIN_LETTERS_REGEX, message: I18n.t('errors.only_latin_letters')
  }

  scope :synthetic, -> { where(synthetic: true) }
  scope :real,      -> { where(synthetic: false) }
  scope :verified,  -> { where(verified:  true) }
  scope :newbies,   -> { where(verified:  false) }
  scope :sorted,    -> { order(:first_name, :last_name) }
  scope :admin,     -> { where(admin: true) }

  def full_name
    "#{first_name} #{last_name}"
  end

  def reverse_full_name
    "#{last_name} #{first_name}"
  end

  def should_generate_new_friendly_id?
    slug.blank? || first_name_changed? || last_name_changed?
  end

  def normalize_friendly_id(string)
    count = User.where(first_name: first_name, last_name: last_name).count

    count.positive? ? super + '-' + (count + 1).to_s : super
  end

  # for sending emails in background
  def send_devise_notification(notification, *args)
    devise_mailer.send(notification, self, *args).deliver_later
  end
end
