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

require 'rails_helper'

# What the search finds, and for whom, is covered by spec/services/service/knowledge_base/search_spec.rb —
#   this covers the GraphQL surface only: arguments, authorization, the payload, and its query count.
RSpec.describe Gql::Queries::KnowledgeBase::Search, :searchindex, type: :graphql do
  include_context 'basic Knowledge Base'

  let(:gql_query) do
    <<~GQL
      query knowledgeBaseSearch($query: String!, $entity: EnumKnowledgeBaseSearchEntity!, $categoryId: ID, $locale: String, $first: Int) {
        knowledgeBaseSearch(query: $query, entity: $entity, categoryId: $categoryId, locale: $locale, first: $first) {
          totalCount
          edges {
            node {
              item {
                ... on KnowledgeBaseAnswer { id translation { title } }
                ... on KnowledgeBaseCategory { id translation { title } visibility }
              }
              titlePreview { text highlight }
              bodyPreview { text highlight }
              categoryPath { id title }
            }
          }
          pageInfo { endCursor hasNextPage }
        }
      }
    GQL
  end

  let(:search)      { 'ocarina' }
  # Always passed, so a context only has to switch it; that the argument may be *omitted* is
  #   covered on its own, with a document that leaves it out.
  let(:entity)      { 'answer' }
  let(:category_id) { nil }
  let(:locale)      { nil }
  let(:first)       { nil }
  let(:variables)   { { query: search, entity:, categoryId: category_id, locale:, first: }.compact }

  let(:published_ocarina) do
    create(:knowledge_base_answer, :published, category: category, translation_attributes: { title: 'Ocarina tuning' })
  end

  let(:internal_ocarina) do
    create(:knowledge_base_answer, :internal, category: category, translation_attributes: { title: 'Ocarina repair' })
  end

  let(:draft_ocarina) do
    create(:knowledge_base_answer, :draft, category: category, translation_attributes: { title: 'Ocarina draft' })
  end

  let(:subcategory_ocarina) do
    create(:knowledge_base_answer, :published, category: subcategory, translation_attributes: { title: 'Ocarina cleaning' })
  end

  let(:other_category_ocarina) do
    create(:knowledge_base_answer, :published, category: other_category, translation_attributes: { title: 'Ocarina storage' })
  end

  let(:ocarina_category) do
    create(:knowledge_base_category, knowledge_base: knowledge_base, parent: category).tap do |elem|
      elem.translations.first.update!(title: 'Ocarina department')
    end
  end

  # Its answer is titled so that it does not match the search itself — the category is the hit
  #   here, and its published answer only gives the category something to be visible from.
  let(:ocarina_category_with_content) do
    create(:knowledge_base_category, knowledge_base: knowledge_base, parent: category).tap do |elem|
      elem.translations.first.update!(title: 'Ocarina archive')
      create(:knowledge_base_answer, :published, category: elem, translation_attributes: { title: 'Woodwind storage' })
    end
  end

  # Overridden where extra hits are needed in the index, which the shared setup below builds.
  let(:extra_ocarina_categories) { [] }

  def item_ids
    gql.result.nodes.map { |node| node.dig('item', 'id') }
  end

  def node_for(record)
    gql.result.nodes.find { |node| node.dig('item', 'id') == gql.id(record) }
  end

  before do
    published_ocarina
    internal_ocarina
    draft_ocarina
    subcategory_ocarina
    other_category_ocarina
    ocarina_category
    ocarina_category_with_content
    extra_ocarina_categories
    searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
    gql.execute(gql_query, variables:)
  end

  context 'with an admin (editor)', authenticated_as: :admin do
    let(:admin) { create(:admin) }

    it 'finds answers of every publication state' do
      expect(item_ids).to include(gql.id(published_ocarina), gql.id(internal_ocarina), gql.id(draft_ocarina))
    end

    it 'marks the matched run of the title' do
      segments = node_for(published_ocarina)['titlePreview']

      expect(segments.select { |segment| segment['highlight'] }.pluck('text')).to eq(['Ocarina'])
    end

    it 'ids each category path segment for linking the breadcrumb' do
      expect(node_for(subcategory_ocarina)['categoryPath'].pluck('id'))
        .to eq([category, subcategory].map { |elem| gql.id(elem) })
    end

    it 'counts every hit the user may see' do
      expect(gql.result.data['totalCount']).to eq(gql.result.nodes.size)
    end

    context 'when searching for categories' do
      let(:entity) { 'category' }

      it 'finds categories' do
        expect(item_ids).to include(gql.id(ocarina_category))
      end

      it 'counts the categories it returns' do
        expect(gql.result.data['totalCount']).to eq(gql.result.nodes.size)
      end

      it 'reports the subtree visibility of a category hit' do
        expect(node_for(ocarina_category_with_content)['item']).to include('visibility' => 'published')
      end
    end

    # Omitting the argument is what the schema's default is for, and the tab control starts on the
    #   answers.
    context 'without a kind of content given' do
      let(:gql_query) do
        <<~GQL
          query knowledgeBaseSearch($query: String!) {
            knowledgeBaseSearch(query: $query) {
              edges { node { item { ... on KnowledgeBaseAnswer { id } ... on KnowledgeBaseCategory { id } } } }
            }
          }
        GQL
      end

      let(:variables) { { query: search } }

      it 'searches the answers' do
        expect(item_ids).to include(gql.id(published_ocarina)).and(not_include(gql.id(ocarina_category)))
      end
    end

    context 'with a kind of content the schema does not know' do
      let(:entity) { 'knowledge_base' }

      it 'is rejected by the enum' do
        expect(gql.result.error_message).to match(%r{entity}i)
      end
    end
  end

  # The visibility of a category hit is batched in Service::KnowledgeBase::Search; without it,
  #   CategoryType#visibility walks the subtree with a recursive query once per publication state,
  #   for every category on the page.
  context 'when many categories match', authenticated_as: :admin do
    let(:admin)  { create(:admin) }
    let(:entity) { 'category' }

    let(:extra_ocarina_categories) do
      create_list(:knowledge_base_category, 5, knowledge_base:, parent: category).each_with_index.map do |elem, index|
        elem.tap { elem.translations.first.update!(title: "Ocarina shelf #{index}") }
      end
    end

    def answer_queries
      queries = []
      subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |*, payload|
        queries << payload[:sql] if payload[:sql].include?('knowledge_base_answers')
      end
      gql.execute(gql_query, variables:)
      queries.size
    ensure
      ActiveSupport::Notifications.unsubscribe(subscriber)
    end

    it 'keeps the answer queries constant as more categories are hit', :aggregate_failures do
      baseline = answer_queries
      baseline_ids = item_ids

      create_list(:knowledge_base_category, 5, knowledge_base:, parent: category).each_with_index do |elem, index|
        elem.translations.first.update!(title: "Ocarina crate #{index}")
      end
      searchindex_model_reload([KnowledgeBase::Category::Translation])

      count = answer_queries

      expect(item_ids.size).to be > baseline_ids.size
      expect(count).to eq(baseline)
    end
  end

  context 'when scoped to a category', authenticated_as: :admin do
    let(:admin)       { create(:admin) }
    let(:category_id) { gql.id(category) }

    it 'includes only hits from the category and its subcategories', :aggregate_failures do
      expect(item_ids).to include(gql.id(published_ocarina), gql.id(subcategory_ocarina))
      expect(item_ids).not_to include(gql.id(other_category_ocarina))
    end
  end

  context 'without authentication' do
    it 'is rejected' do
      expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
    end
  end

  # Scoping is authorized with the same rule as browsing the category, so a user cannot search
  #   inside (by URL) a category that is hidden from them.
  context 'when the scope category is not browsable by the user', authenticated_as: :customer do
    let(:customer)    { create(:customer) }
    let(:category_id) { gql.id(other_category) }

    before do
      other_category.answers.each { |answer| answer.update!(published_at: nil, internal_at: Time.zone.now) }
      gql.execute(gql_query, variables:)
    end

    it 'is forbidden' do
      expect(gql.result.error_type).to eq(Exceptions::Forbidden)
    end
  end

  context 'when the knowledge base is inactive', authenticated_as: :admin do
    let(:admin) { create(:admin) }

    before do
      knowledge_base.update!(active: false)
      gql.execute(gql_query, variables:)
    end

    # As every other knowledge base query answers it: there is at most one, and the new stack
    #   works on it only while it is active.
    it 'is rejected' do
      expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
    end
  end

  context 'with pagination', authenticated_as: :admin do
    let(:admin) { create(:admin) }
    let(:first) { 1 }

    it 'limits the page and reports that more pages exist', :aggregate_failures do
      expect(gql.result.data['edges'].size).to eq(1)
      expect(gql.result.data['pageInfo']).to include('hasNextPage' => true)
    end

    it 'still reports the full total, not the page size' do
      expect(gql.result.data['totalCount']).to be > 1
    end
  end
end
