# frozen_string_literal: true

# == Schema Information
#
# Table name: tags
#
#  id             :bigint           not null, primary key
#  articles_count :integer          default(0)
#  deleted_at     :datetime
#  name           :string(191)
#  ord            :integer
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  user_id        :bigint
#
# Indexes
#
#  index_tags_on_articles_count  (articles_count)
#  index_tags_on_user_id         (user_id)
#
class Tag < ApplicationRecord
  # Update postcss.config.js together for tailwindcss purge problems
  COLORS = %w[gray red yellow green blue indigo purple pink].freeze

  # belongs_to :user, counter_cache: true
  has_many :taggings
  has_many :articles, through: :taggings


  validates :name, uniqueness: true
  belongs_to :user, optional: true

  def self.list_names(limit)
    Tag.where(deleted_at: nil).order(ord: :desc).limit(limit).map(&:name).join(",")
  end

  def color
    @color ||= COLORS[id % COLORS.size]
  end
end
