unsupported
      ˆ¾‘äÇò 8à\ ž å�„þËƒh¹ÊÈÇ      unsupported
  F   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þ[�¬ã�f1�¡n3üÿÌ„/ßË_�uÐb&=LyZËcõÊÉ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class KnowledgeBase::Category < ApplicationModel
  include HasTranslations
  include HasAgentAllowedParams
  include ChecksKbClientNotification
  include ChecksKbClientVisibility
  include HasRecursiveCteQuery
  include TriggersKnowledgeBaseContentUpdates

  AGENT_ALLOWED_ATTRIBUTES       = %i[knowledge_base_id parent_id category_icon].freeze
  AGENT_ALLOWED_NESTED_RELATIONS = %i[translations].freeze

  belongs_to :knowledge_base, inverse_of: :categories

  has_many   :answers,   class_name: 'KnowledgeBase::Answer',
                         inverse_of: :category,
                         dependent:  :restrict_with_exception

  has_many   :children,  class_name:  'KnowledgeBase::Category',
                         foreign_key: :parent_id,
                         inverse_of:  :parent,
                         dependent:   :restrict_with_exception

  belongs_to :parent,    class_name: 'KnowledgeBase::Category',
                         inverse_of: :children,
                         touch:      true,
                         optional:   true

  has_many :permissions, class_name: 'KnowledgeBase::Permission',
                         as:         :permissionable,
                         autosave:   true,
                         dependent:  :destroy

  validates :category_icon, presence: true

  # One mode per list: the subcategories of this category and the answers filed in it are ordered
  #   independently of each other (see KnowledgeBase::SORTING_MODES).
  validates :category_sorting_mode, inclusion: { in: KnowledgeBase::SORTING_MODES }
  validates :answer_sorting_mode,   inclusion: { in: KnowledgeBase::SORTING_MODES }

  before_validation :inherit_sorting_modes, on: :create

  # #inherit_sorting_modes needs to know whether the caller assigned these itself, and dirty
  #   tracking cannot answer that: it cannot tell an assignment equal to the column default from no
  #   assignment at all, so an editor explicitly choosing the default mode would otherwise read the
  #   same as one who never chose anything. Tracked here instead, independent of the value assigned.
  def category_sorting_mode=(value)
    @category_sorting_mode_assigned = true
    super
  end

  def answer_sorting_mode=(value)
    @answer_sorting_mode_assigned = true
    super
  end

  validate :cannot_be_child_of_parent, :parent_must_reach_root, :cannot_exceed_max_depth

  scope :root,   -> { where(parent: nil) }
  scope :sorted, -> { order(position: :asc) }

  # Orders the categories listed inside one node in that node's `category_sorting_mode`. The single
  #   place that order is decided for either stack: the help site orders a listing with it directly
  #   (KnowledgeBase::Public::BaseController#categories_filter), and the desktop view loads its
  #   whole tree through it (Service::KnowledgeBase::Concerns::WalksCategoryTree#all_categories),
  #   so neither sorts categories in Ruby.
  #
  # That matters for anything outside ASCII. Ruby compares strings by codepoint, which files every
  #   accented title after `Z` ("Ã„hre" after "Zebra"); PostgreSQL applies the database collation,
  #   which folds it onto its base letter ("Ã„hre" before "apple"). Deciding it here once means the
  #   two interfaces cannot disagree about it. As elsewhere in Zammad (see Transaction::Trigger),
  #   the exact order of non-ASCII titles is the database's collation to define.
  #
  # `last_update` dates a category by the `edited_at` of the translation it is shown under, the way
  #   the answer listing dates an answer by the `edited_at` of its own â€” one editorial timestamp per
  #   locale, moved by a real edit and by nothing else (KnowledgeBase::Category::Translation
  #   .bump_edited_at, and the callbacks that call it). Deliberately not the record's own
  #   `updated_at`: `touch: true` runs up the tree from every answer save, and a r