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

require 'rails_helper'

RSpec.describe 'Data Privacy', authenticated_as: :authenticate, type: :system do
  let(:customer) { create(:customer, firstname: 'Frank1') }
  let(:ticket)   { create(:ticket, customer: customer, group: Group.find_by(name: 'Users')) }

  def authenticate
    customer
    ticket
    true
  end

  context 'when data privacy admin interface' do
    before do
      visit 'system/data_privacy'
      ensure_websocket
      click '.js-new'
    end

    it 'deletes customer' do
      find(:css, '.js-input').send_keys(customer.firstname)
      expect(page).to have_css('.searchableSelect-option-text')
      click '.searchableSelect-option-text'
      fill_in 'Are you sure?', with: 'DELETE'
      expect(page).to have_no_text('DELETE ORGANIZATION?')
      click '.js-submit'

      expect(page).to have_text('in process')

      # 'completed' only arrives via a websocket push (no fallback if missed) -
      # reconfirm the connection is still live right before the job triggers it.
      ensure_websocket(check_if_pinged: false)
      DataPrivacyTaskJob.perform_now
      expect(page).to have_text('completed')
    end

    context 'when customer is the single user of the organization' do
      let(:organization) { create(:organization) }
      let(:customer) { create(:customer, firstname: 'Frank2', organization: organization) }

      def authenticate
        organization
        customer
        ticket
        true
      end

      it 'deletes customer' do
        find(:css, '.js-input').send_keys(customer.firstname)
        expect(page).to have_css('.searchableSelect-option-text')
        click '.searchableSelect-option-text'
        fill_in 'Are you sure?', with: 'DELETE'
        expect(page).to have_text('DELETE ORGANIZATION?')
        click '.js-submit'

        expect(page).to have_text('in process')

        # 'completed' only arrives via a websocket push (no fallback if missed) -
        # reconfirm the connection is still live right before the job triggers it.
        ensure_websocket(check_if_pinged: false)
        DataPrivacyTaskJob.perform_now
        expect(page).to have_text('completed')
      end

      it 'deletes customer by email' do
        find(:css, '.js-input').send_keys(customer.email)
        expect(page).to have_css('.searchableSelect-option-text')
        click '.searchableSelect-option-text'
        fill_in 'Are you sure?', with: 'DELETE'
        expect(page).to have_text('DELETE ORGANIZATION?')
        click '.js-submit'

        expect(page).to have_text('in process')

        # 'completed' only arrives via a websocket push (no fallback if missed) -
        # reconfirm the connection is still live right before the job triggers it.
        ensure_websocket(check_if_pinged: false)
        DataPrivacyTaskJob.perform_now
        expect(page).to have_text('completed')
      end
    end
  end

  context 'when user profile' do
    it 'deletes customer' do
      visit "user/profile/#{customer.id}"
      ensure_websocket

      click '.dropdown--actions'
      click_on 'Delete'

      # Wait for the customer ticket preview and the confirmation input to finish
      # loading. The preview is rendered only after two nested AJAX calls complete.
      # Use the name attribute selector to avoid label-id ambiguity with the
      # special characters (::) in the generated form element id.
      find('input[name="preferences::sure"]').set('DELETE')
      click '.js-submit'

      expect(page).to have_text('in process')

      # 'completed' only arrives via a websocket push (no fallback if missed) -
      # reconfirm the connection is still live right before the job triggers it.
      ensure_websocket(check_if_pinged: false)
      DataPrivacyTaskJob.perform_now
      expect(page).to have_text('completed')
    end
  end

  context 'when ticket zoom' do
    it 'deletes customer' do
      visit "ticket/zoom/#{ticket.id}"
      ensure_websocket

      click '.tabsSidebar-tab[data-tab=customer]'
      click 'h2.js-headline'
      click_on 'Delete Customer'

      fill_in 'Are you sure?', with: 'DELETE'
      click '.js-submit'

      expect(page).to have_text('in process')

      # 'completed' only arrives via a websocket push (no fallback if missed) -
      # reconfirm the connection is still live right before the job triggers it.
      ensure_websocket(check_if_pinged: false)
      DataPrivacyTaskJob.perform_now
      expect(page).to have_text('completed')
    end
  end
end
