511
  7   !ˆÐ�þC#m,ã�f1�¡‘¦™þÕƒu°=ÔÏa–ßi~” T¾R( 	Å�~ã,©‹FÿÓ T    !# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

# A knowledge base answer in a tool's result, named by the answer's ID and the locale of its text, by which the
#   desktop view opens it.
class Service::AI::Tool::Output::KnowledgeBaseAnswer < Service::AI::Tool::Output
  CATEGORY_SEPARATOR = ' / '.freeze

  # Written for the model, like every text a tool returns, so not translated.
  # rubocop:disable Zammad/DetectTranslatableString
  CATEGORY_SCHEMA = {
    type:       'object',
    properties: {
      id:   { type: 'integer' },
      path: { type: 'string', description: 'The titles of the category and the categories above it, the top one first, as in "Setup / Email".' },
    },
    required:   %w[id path],
  }.freeze

  SCHEMA = {
    type:       'object',
    properties: {
      type:     { const: 'KnowledgeBase::Answer' },
      id:       { type: 'integer' },
      title:    { type: 'string' },
      locale:   { type: 'string' },
      category: CATEGORY_SCHEMA,
      url:      { type: 'string', description: 'Link to the answer in Zammad.' },
    },
    required:   %w[type id title locale category],
  }.freeze
  # rubocop:enable Zammad/DetectTranslatableString

  # The facts and the text of #part.
  # rubocop:disable-next Zammad/DetectTranslatableString
  PART_PROPERTIES = {
    visibility:  { enum: %w[public internal draft archived], description: 'public: customers may read the answer too.' },
    edited_at:   { type: 'string', format: 'date-time' },
    tags:        { type: 'array', items: { type: 'string' } },
    text:        { type: 'string', description: 'The answer\'s text, as the knowledge base\'s editors wrote it.' },
    truncated:   { type: 'boolean' },
    part:        { enum: %w[passage start], description: 'Which part of a cut text is shown: the passage that matched, or the start.' },
    full_length: { type: 'integer', description: 'The length of a cut text in full, in characters.' },
  }.freeze

  class << self
    # @param properties [Hash] the schemas of the tool's own facts in the row, such as how the answer is linked
    def schema(**properties)
      SCHEMA.merge(properties: SCHEMA[:properties].merge(properties))
    end

    # The schema of #part, with the tool's own facts.
    def part_schema(**properties)
      schema(**PART_PROPERTIES, **properties).merge(required: SCHEMA[:required] + %w[visibility text truncated])
    end

    # The answer as a row: one line of its facts, and its title below it, marked as the knowledge base's.
    #
    # @param translation [KnowledgeBase::Answer::Translation]
    # @param category [Hash, nil] the answer's category as #categories gives it, which rows of many answers load together
    # @param content [Hash] the tool's own facts for the content, such as how the answer is linked
    # @param facts [Array<String, nil>] the same facts for the line
    def row(translation, category: nil, content: {}, facts: [])
      locale     = translation.kb_locale.system_locale.locale
      category ||= categories([translation]).fetch(translation.id)

      build_row(
        content: { type: 'KnowledgeBase::Answer', id: translation.answer_id, title: translation.title, locale:, category:, **content },
        facts:   ["Knowledge base answer #{translation.answer_id}", locale, "category #{category[:path]}", *facts],
        title:   translation.title,
        source:  'knowledge_base',
      )
    end

    # The answer as a row with its text below it, marked as the knowledge base's. A text longer than the given length
    #   is shown by the passage that matched a search where there is one, by its start otherwise, and says how much of
    #   it is shown.
    #
    # @param passage [String, nil] the part of the text that matched a search
    # @param edited_at [Time, nil] when the text was last edited, where the user may see it
    # @param category [Hash, nil] as #row takes it
    # @param tags [Array<String>, nil] the answer's tags, where the tool shows them
    # @param c