# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

module HasSearchIndexBackend
  extend ActiveSupport::Concern

  included do
    after_commit  :search_index_update, if: :persisted?
    after_destroy :search_index_destroy
  end

=begin

update search index, if configured - will be executed automatically

  model = Model.find(123)
  model.search_index_update

=end

  def search_index_update
    return true if ignore_search_indexing?(:update)

    # start background job to transfer data to search index
    return true if !SearchIndexBackend.enabled?

    return true if previous_changes.blank?

    SearchIndexJob.perform_later(self.class.to_s, id)
    SearchIndexAssociationsJob.perform_later(self.class.to_s, id)
    true
  end

  def search_index_indexable
    Models.indexable.reject { |local_class| local_class == self.class }
  end

  def search_index_indexable_attributes(index_class)
    result = []
    index_class.new.attributes.each_key do |key|
      attribute_name = key.to_s
      next if attribute_name.blank?

      # due to performance reasons, we only want to process some attributes for specific classes (e.g. tickets)
      next if !index_class.search_index_attribute_relevant?(attribute_name)

      attribute_ref_name = index_class.search_index_attribute_ref_name(attribute_name)
      next if attribute_ref_name.blank?

      association = index_class.reflect_on_association(attribute_ref_name)
      next if association.blank?
      next if association.options[:polymorphic]

      attribute_class = association.klass
      next if attribute_class.blank?
      next if attribute_class != self.class

      result << {
        name:     attribute_name,
        ref_name: attribute_ref_name,
      }
    end
    result
  end

  def search_index_update_delta(index_class:, value:, attribute:)
    raise "Attribute lookup data needs updated_at for delta updates (object: #{self.class}, #{id})" if value['updated_at'].blank?

    data = {
      attribute[:ref_name] => value,
    }
    where = {
      bool: {
        must: [
          {
            term: {
              attribute[:name] => id,
            },
          },
          {
            range: {
              "#{attribute[:ref_name]}.updated_at" => {
                lt: value['updated_at'].strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
              },
            },
          },
        ],
      },
    }
    SearchIndexBackend.update_by_query(index_class.to_s, data, where)
  end

=begin

update search index, if configured - will be executed automatically

  model = Organizations.find(123)
  result = model.search_index_update_associations

returns

  # Updates asscociation data for users and tickets of the organization in this example
  result = true

=end

  def search_index_update_associations
    return if !SearchIndexBackend.enabled?

    new_search_index_value = search_index_attribute_lookup_with_refresh(include_references: false)
    return if new_search_index_value.blank?

    search_index_indexable.each_with_object([]) do |index_class, result|
      search_index_indexable_attributes(index_class).each do |attribute|
        result << search_index_update_delta(index_class: index_class, value: new_search_index_value, attribute: attribute)
      end
    end
  end

=begin

delete search index object, will be executed automatically

  model = Model.find(123)
  model.search_index_destroy

=end

  def search_index_destroy
    return true if ignore_search_indexing?(:destroy)

    SearchIndexBackend.remove(self.class.to_s, id)
    true
  end

=begin

collect data to index and send to backend

  ticket = Ticket.find(123)
  result = ticket.search_index_update_backend

returns

  result = true # false

=end

  def search_index_update_backend
    # fill up with search data
    attributes = search_index_attribute_lookup_with_refresh
    return true if !attributes

    # update backend
    SearchIndexBackend.add(self.class.to_s, attributes)
    true
  end

=begin

update the search index right away, e.g. inside a request, the object is searchable when
the call returns - models can leave expensive references to the background job via
search_index_inline_include_references?, opt out via search_index_inline_supported? and
report records changed by their callbacks via search_index_inline_cascaded?

  ticket = Ticket.find(123)
  result = ticket.search_index_update_inline

returns

  result = true  # the search index reflects the record
  result = false # the record, or records changed with it, are left to the background job

=end

  def search_index_update_inline
    return true if ignore_search_indexing?(:update)
    return true if !SearchIndexBackend.enabled?
    return false if !search_index_inline_supported?

    attributes = search_index_attribute_lookup_with_refresh(include_references: search_index_inline_include_references?)
    return true if !attributes

    SearchIndexBackend.update_inline(self.class.to_s, search_index_inline_attributes(attributes))
    !search_index_inline_cascaded?
  end

  def search_index_inline_include_references?
    true
  end

  # documents with attachments depend on the ingest pipeline, which a partial update cannot run
  def search_index_inline_supported?
    true
  end

  # records changed by callbacks of this save are left to the background job
  def search_index_inline_cascaded?
    false
  end

  # drops ignored attributes and clears nil references, which a partial update would keep otherwise
  def search_index_inline_attributes(attributes)
    attributes.delete_if { |attribute_name, _| self.class.search_index_attribute_ignored?(attribute_name) }
    return attributes if !search_index_inline_include_references?

    self.attributes.each do |attribute_name, value|
      next if !value.nil?
      next if self.class.search_index_attribute_ignored?(attribute_name)
      next if !search_index_attribute_method(attribute_name)

      attribute_ref_name = self.class.search_index_attribute_ref_name(attribute_name)
      next if self.class.search_index_attribute_ignored?(attribute_ref_name)

      attributes[attribute_ref_name] = nil
    end

    attributes
  end

  def ignore_search_indexing?(_action)
    false
  end

  # methods defined here are going to extend the class, not the instance of it
  class_methods do

=begin

serve method to ignore model attributes in search index

class Model < ApplicationModel
  include HasSearchIndexBackend
  search_index_attributes_ignored :password, :image
end

=end

    def search_index_attributes_ignored(*attributes)
      @search_index_attributes_ignored = attributes
    end

    def search_index_attributes_relevant(*attributes)
      @search_index_attributes_relevant = attributes
    end

=begin

reload search index with full data

  Model.search_index_reload

=end

    def search_index_reload(silent: false, worker: 0)
      printed = 0

      SearchIndexBackend::Reload.new([self], worker:).perform do |records|
        next if silent || records.count == printed

        printed = records.count
        print "\r    #{printed}/#{records.total}" # rubocop:disable Rails/Output
      end
    end
  end
end
