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

require 'rails_helper'

RSpec.describe Service::KnowledgeBase::Answer::SimilaritySearch, :aggregate_failures do
  subject(:service) { described_class.with_current_user(user).execute(embedding:, limit:, locale:) }

  let(:embedding) { [0.1, 0.2, 0.3] }
  let(:limit)     { 3 }
  let(:locale)    { nil }

  # A chunk as the index holds it: the title, a blank line, a part of the text.
  def hit(translation, score, text = "The text of #{translation.title}.")
    {
      '_score'  => score,
      '_source' => {
        'object_name' => 'KnowledgeBase::Answer::Translation',
        'object_id'   => translation.id.to_s,
        'content'     => "#{translation.title}\n\n#{text}",
      },
    }
  end

  def result(translation, score)
    { translation:, score:, passage: "The text of #{translation.title}." }
  end

  def stub_search(hits)
    allow(Service::AI::VectorDB::SimilaritySearch)
      .to receive(:execute)
      .and_return({ 'hits' => { 'hits' => hits } })
  end

  # Capture the filter passed into the vector search (permissions/locale are applied in the query).
  def captured_search_filter
    filter = nil
    allow(Service::AI::VectorDB::SimilaritySearch).to receive(:execute) do |**kwargs|
      filter = kwargs[:filter]
      { 'hits' => { 'hits' => [] } }
    end
    service
    filter
  end

  context 'when the user may see all knowledge base answers' do
    let(:role) { create(:role, permission_names: %w[ticket.agent knowledge_base.editor]) }
    let(:user) { create(:agent, roles: [role]) }

    let(:answer_1) { create(:knowledge_base_answer, :published) }
    let(:answer_2) { create(:knowledge_base_answer, :published) }
    let(:answer_3) { create(:knowledge_base_answer, :internal) }
    let(:answer_4) { create(:knowledge_base_answer, :published) }

    it 'returns the matching translations with score, ordered by relevance, limited to the given limit' do
      stub_search([
                    hit(answer_4.translations.first, 0.82),
                    hit(answer_1.translations.first, 0.98),
                    hit(answer_2.translations.first, 0.92),
                    hit(answer_3.translations.first, 0.86),
                  ])

      expect(service).to eq([
                              result(answer_1.translations.first, 0.98),
                              result(answer_2.translations.first, 0.92),
                              result(answer_3.translations.first, 0.86),
                            ])
    end

    it 'respects a smaller limit' do
      stub_search([
                    hit(answer_1.translations.first, 0.95),
                    hit(answer_2.translations.first, 0.85),
                  ])

      expect(described_class.with_current_user(user).execute(embedding:, limit: 1).pluck(:translation))
        .to eq([answer_1.translations.first])
    end

    it 'deduplicates an answer that matches with multiple chunks, keeping the best score and its passage' do
      translation = answer_1.translations.first

      stub_search([
                    hit(translation, 0.85, 'Remove the account first.'),
                    hit(translation, 0.95, 'Then add it again.'),
                  ])

      expect(service).to eq([{ translation:, score: 0.95, passage: 'Then add it again.' }])
    end

    it 'keeps a passage whole when the title in front of it changed since it was indexed' do
      translation = answer_1.translations.first
      stub_search([hit(translation, 0.95).deep_merge('_source' => { 'content' => "Old title\n\nRemove the account." })])

      expect(service.first[:passage]).to eq("Old title\n\nRemove the account.")
    end

    context 'when the caller passes its own floor' do
      subject(:service) { described_class.with_current_user(user).execute(embedding:, limit:, minimum_score: 0.78) }

      it 'keeps the hits above it, whatever the sidebar\'s setting says' do
        stub_search([
                      hit(answer_1.translations.first, 0.81),
                      hit(answer_2.translations.first, 0.79),
                      hit(answer_3.translations.first, 0.77),
                    ])

        expect(service.pluck(:translation)).to eq([answer_1.translations.first, answer_2.translations.first])
      end
    end

    it 'ignores hits below the configured relevance score' do
      stub_search([
                    hit(answer_1.translations.first, described_class::DEFAULT_RELEVANCE_SCORE / 100.0),
                    hit(answer_2.translations.first, (described_class::DEFAULT_RELEVANCE_SCORE - 10) / 100.0),
                  ])

      expect(service.pluck(:translation)).to eq([answer_1.translations.first])
    end

    context 'when the configured relevance score is lowered' do
      before { Setting.set('ai_assistance_kb_answer_suggestions_relevance_score', 70) }

      it 'also surfaces the previously dropped hits' do
        stub_search([
                      hit(answer_1.translations.first, 0.85),
                      hit(answer_2.translations.first, 0.75),
                    ])

        expect(service.pluck(:translation)).to contain_exactly(answer_1.translations.first, answer_2.translations.first)
      end
    end

    context 'when the configured relevance score is raised' do
      before { Setting.set('ai_assistance_kb_answer_suggestions_relevance_score', 95) }

      it 'drops hits which passed the default threshold' do
        stub_search([
                      hit(answer_1.translations.first, 0.98),
                      hit(answer_2.translations.first, 0.86),
                    ])

        expect(service.pluck(:translation)).to eq([answer_1.translations.first])
      end
    end

    context 'when the relevance score setting holds no value' do
      before { Setting.set('ai_assistance_kb_answer_suggestions_relevance_score', nil, validate: false) }

      it 'falls back to the default threshold' do
        stub_search([
                      hit(answer_1.translations.first, described_class::DEFAULT_RELEVANCE_SCORE / 100.0),
                      hit(answer_2.translations.first, (described_class::DEFAULT_RELEVANCE_SCORE - 10) / 100.0),
                    ])

        expect(service.pluck(:translation)).to eq([answer_1.translations.first])
      end
    end
  end

  context 'when applying permissions' do
    let(:user)             { create(:agent) }
    let(:published_answer) { create(:knowledge_base_answer, :published) }
    let(:draft_answer)     { create(:knowledge_base_answer, :draft) }

    before do
      published_answer
      draft_answer
    end

    it 'searches only within the answers the user is allowed to see' do
      filter = captured_search_filter

      expect(filter[:object_name]).to eq('KnowledgeBase::Answer::Translation')
      expect(filter[:'metadata.answer_id']).to include(published_answer.id)
      expect(filter[:'metadata.answer_id']).not_to include(draft_answer.id)
    end

    context 'with an agent without any knowledge base permission' do
      let(:user)            { create(:agent, roles: [create(:role, permission_names: %w[ticket.agent])]) }
      let(:internal_answer) { create(:knowledge_base_answer, :internal) }

      before { internal_answer }

      it 'searches only within published answers, which they can read on the public help site' do
        filter = captured_search_filter

        expect(filter[:'metadata.answer_id']).to include(published_answer.id)
        expect(filter[:'metadata.answer_id']).not_to include(internal_answer.id, draft_answer.id)
      end
    end

    it 'does not search at all when the user may see no answers' do
      allow(KnowledgeBase::Answer).to receive(:visible_to_user).and_return(KnowledgeBase::Answer.none)
      allow(Service::AI::VectorDB::SimilaritySearch).to receive(:execute)

      expect(service).to eq([])
      expect(Service::AI::VectorDB::SimilaritySearch).not_to have_received(:execute)
    end

    # Neither entry point requires knowledge base access, so this is the only thing keeping a
    #   deactivated knowledge base out of the suggestions — its documents stay in the index.
    #   https://github.com/zammad/zammad/issues/6338
    context 'when the knowledge base is inactive' do
      # The factories create a knowledge base per answer, where production only ever has one — so
      #   switch them all off to express "the knowledge base is deactivated".
      before { KnowledgeBase.find_each { |knowledge_base| knowledge_base.update! active: false } }

      it 'does not search at all' do
        allow(Service::AI::VectorDB::SimilaritySearch).to receive(:execute)

        expect(service).to eq([])
        expect(Service::AI::VectorDB::SimilaritySearch).not_to have_received(:execute)
      end
    end
  end

  context 'when drafts and archived answers exist' do
    let(:draft_answer)     { create(:knowledge_base_answer, :draft) }
    let(:archived_answer)  { create(:knowledge_base_answer, :archived) }
    let(:internal_answer)  { create(:knowledge_base_answer, :internal) }
    let(:published_answer) { create(:knowledge_base_answer, :published) }

    before do
      draft_answer
      archived_answer
      internal_answer
      published_answer
    end

    context 'with a knowledge base editor' do
      let(:role) { create(:role, permission_names: %w[ticket.agent knowledge_base.editor]) }
      let(:user) { create(:agent, roles: [role]) }

      it 'searches only within the internally visible ones' do
        filter = captured_search_filter

        expect(filter[:'metadata.answer_id']).to include(internal_answer.id, published_answer.id)
        expect(filter[:'metadata.answer_id']).not_to include(draft_answer.id, archived_answer.id)
      end

      context 'when drafts and archived answers are requested' do
        subject(:service) { described_class.with_current_user(user).execute(embedding:, limit:, locale:, include_drafts_and_archived: true) }

        it 'searches within drafts and archived answers, too' do
          expect(captured_search_filter[:'metadata.answer_id'])
            .to include(draft_answer.id, archived_answer.id, internal_answer.id, published_answer.id)
        end
      end
    end

    context 'with a user who may not see drafts and archived answers' do
      let(:user) { create(:agent) }

      context 'when drafts and archived answers are requested' do
        subject(:service) { described_class.with_current_user(user).execute(embedding:, limit:, locale:, include_drafts_and_archived: true) }

        it 'still excludes them from the searched ids' do
          filter = captured_search_filter

          expect(filter[:'metadata.answer_id']).to include(published_answer.id)
          expect(filter[:'metadata.answer_id']).not_to include(draft_answer.id, archived_answer.id)
        end
      end
    end

    # Granular permissions make visible_to_user build an OR group (see the excluded category examples
    # below) instead of the flat relation the examples above run through, so the publication state
    # filter has to bind as one AND over the whole group rather than as a condition on one branch.
    context 'with granular permissions' do
      let(:role)             { create(:role, permission_names: %w[ticket.agent knowledge_base.editor]) }
      let(:user)             { create(:agent, roles: [role]) }
      let(:granted_category) { draft_answer.category }

      before do
        # A non-draft answer in the same category, so the default search has ids left to run with.
        create(:knowledge_base_answer, :published, category: granted_category)

        KnowledgeBase::PermissionsUpdate.new(granted_category).update!(role => 'editor')
      end

      it 'takes the granular path' do
        expect(KnowledgeBase.access_for_user(user)).to eq(:granular)
      end

      it 'excludes the draft answer of the granted category' do
        expect(captured_search_filter[:'metadata.answer_id']).not_to include(draft_answer.id)
      end

      context 'when drafts and archived answers are requested' do
        subject(:service) { described_class.with_current_user(user).execute(embedding:, limit:, locale:, include_drafts_and_archived: true) }

        it 'searches within it' do
          expect(captured_search_filter[:'metadata.answer_id']).to include(draft_answer.id)
        end
      end
    end
  end

  # Excluding a category does not retroactively purge what is already indexed, so the search has to
  # drop those answers itself instead of relying on the index being in sync.
  context 'when a category is excluded from the vector index' do
    let(:role)                { create(:role, permission_names: %w[knowledge_base.editor]) }
    let(:user)                { create(:agent, roles: [role]) }
    let(:knowledge_base)      { create(:knowledge_base) }
    let(:excluded_category)   { create(:knowledge_base_category, knowledge_base:) }
    let(:sub_category)        { create(:knowledge_base_category, knowledge_base:, parent: excluded_category) }
    let(:kept_category)       { create(:knowledge_base_category, knowledge_base:) }
    let(:excluded_answer)     { create(:knowledge_base_answer, :published, category: excluded_category) }
    let(:sub_answer)          { create(:knowledge_base_answer, :published, category: sub_category) }
    let(:kept_answer)         { create(:knowledge_base_answer, :published, category: kept_category) }

    before do
      excluded_answer
      sub_answer
      kept_answer
      Setting.set('vectordb_knowledge_base_excluded_category_ids', [excluded_category.id])
    end

    it 'drops answers in the excluded category from the searched ids' do
      expect(captured_search_filter[:'metadata.answer_id']).not_to include(excluded_answer.id)
    end

    it 'drops answers in a sub-category of the excluded category' do
      expect(captured_search_filter[:'metadata.answer_id']).not_to include(sub_answer.id)
    end

    it 'keeps answers outside the excluded subtree' do
      expect(captured_search_filter[:'metadata.answer_id']).to include(kept_answer.id)
    end

    it 'does not search at all when every visible answer is excluded' do
      Setting.set('vectordb_knowledge_base_excluded_category_ids', KnowledgeBase::Category.pluck(:id))
      allow(Service::AI::VectorDB::SimilaritySearch).to receive(:execute)

      expect(service).to eq([])
      expect(Service::AI::VectorDB::SimilaritySearch).not_to have_received(:execute)
    end

    # Granular permissions make visible_to_user build an OR group (an editor branch, a reader branch
    # and a public one) instead of the flat relation the examples above run through. The exclusion
    # then has to bind as one AND over the whole group — as a condition on a single branch it would
    # let the excluded answers back in through the others. Granting the excluded category outright
    # puts them in the widest branch, the one with nothing else to filter them out.
    context 'with granular permissions' do
      before do
        KnowledgeBase::PermissionsUpdate.new(excluded_category).update!(role => 'editor')
        KnowledgeBase::PermissionsUpdate.new(kept_category).update!(role => 'reader')
      end

      # Granular mode turns on as soon as any permission row exists; without this the examples below
      # would quietly fall back to the flat :editor path and prove nothing about the OR group.
      it 'takes the granular path' do
        expect(KnowledgeBase.access_for_user(user)).to eq(:granular)
      end

      it 'drops the excluded category and its subtree, granted though they are' do
        expect(captured_search_filter[:'metadata.answer_id']).not_to include(excluded_answer.id, sub_answer.id)
      end

      it 'keeps the granted category outside the excluded subtree' do
        expect(captured_search_filter[:'metadata.answer_id']).to include(kept_answer.id)
      end
    end
  end

  context 'when restricting to a locale' do
    let(:role)      { create(:role, permission_names: %w[knowledge_base.editor]) }
    let(:user)      { create(:agent, roles: [role]) }
    let(:locale)    { 'en-us' }
    let(:answer_en) { create(:knowledge_base_answer, :published) }

    before { answer_en }

    it 'restricts the search to the given locale' do
      expect(captured_search_filter).to include('metadata.locale': 'en-us')
    end
  end

  context 'when the embedding is blank' do
    let(:user)      { create(:agent) }
    let(:embedding) { nil }

    it 'returns an empty array without performing a search' do
      allow(Service::AI::VectorDB::SimilaritySearch).to receive(:execute)

      expect(service).to eq([])
      expect(Service::AI::VectorDB::SimilaritySearch).not_to have_received(:execute)
    end
  end

  context 'when no answers match' do
    let(:role) { create(:role, permission_names: %w[knowledge_base.editor]) }
    let(:user) { create(:agent, roles: [role]) }

    before { create(:knowledge_base_answer, :published) }

    it 'returns an empty array' do
      stub_search([])

      expect(service).to eq([])
    end
  end
end
