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

require 'rails_helper'
require 'system/examples/pagination_examples'

RSpec.describe 'Manage > Groups', type: :system do
  context 'when ajax pagination' do
    include_examples 'pagination', model: :group, klass: Group, path: 'manage/groups', create_params: { email_address_id: 1 }
  end

  describe 'with nested groups' do
    let(:group1) { create(:group) }
    let(:group2) { create(:group, parent: group1) }
    let(:group3) { create(:group, parent: group2) }

    before do
      group3 # cascade create
      visit '#manage/groups'
    end

    it 'displays complete group path using chevrons' do
      expect(page).to have_text(group3.name.gsub!(%r{::}, ' › '))
    end

    it 'sorts group paths in correct order' do
      # normalize_ws avoids driver differences in how whitespace between adjacent table cells is rendered
      # (e.g. Playwright's innerText inserts a tab between an empty action cell and the row's newline).
      expect(page).to have_text("#{group1.fullname} #{group2.fullname} #{group3.fullname}", normalize_ws: true)
    end

    describe 'when creating a new group' do
      let(:group_name_last) { Faker::Lorem.unique.word.capitalize }

      before do
        click_on 'New Group'
      end

      it 'creates a nested group' do
        fill_in 'Name', with: group_name_last
        set_tree_select_value('parent_id', group3.fullname)

        # Needed for chrome, when element is outside viewport.
        scroll_into_view('button.js-submit', position: :bottom)

        click_on 'Submit'

        expect(page).to have_no_css('.modal')
        expect(Group.find_by(name_last: group_name_last).name).to eq("#{group3.name}::#{group_name_last}")
      end
    end
  end

  # Fixes GitHub Issue#3129 - Deactivation of signature does not clear it from groups
  describe 'When active status of signature assigned to a group is changed', authenticated_as: -> { user } do
    let(:user)      { create(:admin, groups: [group]) }
    let(:group)     { create(:group, signature_id: signature.id) }
    let(:signature) { create(:signature) }

    it 'does not display warning, when signature is active' do
      visit '#manage/groups'

      click "tr[data-id='#{group.id}']"

      expect(page).to have_select('signature_id', selected: signature.name)
        .and have_no_css('.alert--warning')
    end

    context 'When signature is marked inactive' do
      let(:signature) { create(:signature, active: false) }

      it 'displays warning' do
        visit '#manage/groups'

        click "tr[data-id='#{group.id}']"

        expect(page).to have_select('signature_id', selected: signature.name)
          .and have_css('.alert--warning')
      end
    end
  end

  context "Issue 2544 - Can't remove auto assignment timeout" do
    before do
      visit '/#manage/groups'
    end

    it 'is possible to reset the assignment timeout of a group' do
      find('td', text: 'Users').click

      in_modal do
        fill_in 'Assignment Timeout', with: '30'

        # Needed for chrome, when element is outside viewport.
        scroll_into_view('button.js-submit', position: :bottom)

        click_on 'Submit'
      end

      expect(Group.find_by(name: 'Users').assignment_timeout).to eq(30)

      find('td', text: 'Users').click

      in_modal do
        fill_in 'Assignment Timeout', with: ''

        # Needed for chrome, when element is outside viewport.
        scroll_into_view('button.js-submit', position: :bottom)

        click_on 'Submit'
      end
      expect(Group.find_by(name: 'Users').assignment_timeout).to be_nil
    end
  end

  context 'Issue 4129 - Tooltips are not displayed correctly' do
    before do
      visit '/#manage/groups'
    end

    it 'renders tooltips correctly' do
      find('td', text: 'Users').click

      in_modal do
        find('div.select[data-attribute-name="follow_up_possible"] .js-helpMessage').hover

        expect(page).to have_css('div.tooltip')
      end
    end
  end

  describe 'Issue #4475 - Group edit dialog shows ??? in email select box', authenticated_as: -> { user } do
    let(:user)          { create(:admin, groups: [group]) }
    let(:email_address) { create(:email_address) }
    let(:group)         { create(:group, email_address: email_address) }

    it 'shows correct email address display name' do
      visit '#manage/groups'

      click "tr[data-id='#{group.id}']"

      expect(page).to have_select('email_address_id', text: "#{email_address.name} <#{email_address.email}>")
    end
  end

  describe 'when creating a group while a search is active', searchindex: true do
    before do
      create(:group, name: 'KeepFilter Alpha')
      searchindex_model_reload([Group])

      visit '#manage/groups'
    end

    it 'keeps the search query and shows the new group' do
      find('.js-search').fill_in with: 'KeepFilter'

      expect(page).to have_css('.js-tableBody tr', count: 1)

      click_on 'New Group'

      in_modal do
        fill_in 'Name', with: 'KeepFilter Beta'
        click_on 'Submit'
      end

      expect(page).to have_css('.js-tableBody tr', count: 2)
      expect(find('.js-search').value).to eq('KeepFilter')
    end

    it 'keeps the search query when editing a group' do
      find('.js-search').fill_in with: 'KeepFilter'

      expect(page).to have_css('.js-tableBody tr', count: 1)

      find('.js-tableBody tr', text: 'KeepFilter Alpha').click

      in_modal do
        fill_in 'Name', with: 'KeepFilter Alpha Renamed'
        click_on 'Submit'
      end

      expect(page).to have_css('.js-tableBody tr', text: 'KeepFilter Alpha Renamed', count: 1)
      expect(find('.js-search').value).to eq('KeepFilter')
    end

    it 'notifies about a new group hidden by the search' do
      find('.js-search').fill_in with: 'KeepFilter'

      expect(page).to have_css('.js-tableBody tr', count: 1)

      click_on 'New Group'

      in_modal do
        fill_in 'Name', with: 'Other Group'
        click_on 'Submit'
      end

      expect(page).to have_text('The entry was created, but it is hidden by the current search or filter.')
      expect(page).to have_css('.js-tableBody tr', count: 1)
      expect(find('.js-search').value).to eq('KeepFilter')
    end

    context 'when the search index cannot be updated inline' do
      before do
        allow(SearchIndexBackend).to receive(:update_inline).and_raise('Elasticsearch is not available')
      end

      it 'keeps the search and warns about the outdated list' do
        find('.js-search').fill_in with: 'KeepFilter'

        expect(page).to have_css('.js-tableBody tr', count: 1)

        click_on 'New Group'

        in_modal do
          fill_in 'Name', with: 'KeepFilter Beta'
          click_on 'Submit'
        end

        expect(page).to have_text('The search index could not be updated yet, the list may be outdated.')
        expect(find('.js-search').value).to eq('KeepFilter')
      end
    end

    context 'when the search result spans more than one page' do
      before do
        49.times { |index| create(:group, name: format('KeepFilter %02d', index)) }
        searchindex_model_reload([Group])
      end

      it 'does not notify about a new group on another page' do
        find('.js-search').fill_in with: 'KeepFilter'

        expect(page).to have_current_route('manage/groups/1/KeepFilter')
        expect(page).to have_css('.js-tableBody tr', count: 50)

        click_on 'New Group'

        in_modal do
          fill_in 'Name', with: 'KeepFilter Zebra'
          click_on 'Submit'
        end

        expect(page).to have_css('.js-page', text: '2')
        expect(page).to have_no_text('The entry was created, but it is hidden by the current search or filter.')
      end
    end
  end
end
