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

module BrowserTestHelper # rubocop:disable Metrics/ModuleLength

  # Sometimes tests refer to elements that get removed/re-added to the DOM when
  # updating the UI. This causes the driver to raise a StaleReferenceError exception.
  # This method catches this error and retries the given amount of times re-raising
  # the exception if the element is still stale.
  #
  # @example
  #  retry_on_stale do
  #    find('.now-here-soon-gone').click
  #  end
  #
  #  retry_on_stale(retries: 10) do
  #    find('.now-here-soon-gone').click
  #  end
  #
  # @raise [Capybara::Playwright::Node::StaleReferenceError] If element is still stale after given number of retries
  def retry_on_stale(retries: 3)
    tries ||= 0

    yield
  rescue Capybara::Playwright::Node::StaleReferenceError
    raise if tries == retries

    wait_time = tries
    tries += 1

    Rails.logger.info "Stale element found. Retry #{tries}/#{retries} (sleeping: #{wait_time})"
    sleep wait_time

    retry
  end

  # Get the current cookies from the browser with the driver object.
  #
  def cookies
    page.driver.with_playwright_page do |pw_page|
      pw_page.context.cookies.map { |cookie| normalize_playwright_cookie(cookie) }
    end
  end

  # Symbol keys and expires as Time or nil (Playwright reports session cookies as -1).
  #
  def normalize_playwright_cookie(cookie)
    normalized = cookie.transform_keys { |key| key.underscore.to_sym }
    normalized[:expires] = normalized[:expires].negative? ? nil : Time.zone.at(normalized[:expires])
    normalized
  end

  # Get a single cookie by the given name (regex possible)
  #
  # @example
  #  cookie('cookie-name')
  #
  def cookie(name)
    cookies.find { |cookie| cookie[:name].match?(name) }
  end

  # Delete a single cookie by the given name (regex possible)
  #
  # @example
  #  delete_cookie('cookie-name')
  #
  def delete_cookie(name)
    cookie = cookie(name)

    return if !cookie

    page.driver.with_playwright_page do |pw_page|
      pw_page.context.clear_cookies(name: cookie[:name])
    end
  end

  # Finds an element and clicks it - wrapped in one method.
  #
  # @example
  #  click('.js-channel .btn.email')
  #
  #  click(:href, '#settings/branding')
  #
  def click(...)
    find(...).click
  end

  # Finds svg icon in Mobile View
  #
  # @example
  #  icon = find_icon('home')
  #  icon.click
  #
  def find_icon(name)
    find("[href=\"#icon-#{name}\"]").find(:xpath, '..')
  end

  # Polls the given block until it returns a truthy value.
  # @see BrowserTestHelper::Waiter
  #
  # @example
  #  wait.until { ... }
  #
  # @example
  #  wait(5, interval: 0.5).until { ... }
  #
  def wait(seconds = Capybara.default_max_wait_time, **)
    Waiter.new(timeout: seconds, **)
  end

  # Setting's class-level cache can serve a stale value for a while after a write
  # (e.g. a real browser action, or this test's own Setting.set racing a concurrent
  # reader) - wait for our own read to settle on the expected value before relying on
  # frontend behavior that depends on it, rather than a blind sleep.
  #
  # Note: Setting.get round-trips hash values with string keys, even if the setting was
  # written with symbol keys - use `key:` to compare a specific key instead of the whole
  # hash if that's a concern.
  #
  # @example
  #  wait_for_setting('icinga_sender', icinga_sender)
  #
  # @example
  #  wait_for_setting('ai_assistance_ticket_summary_config', 'on_ticket_detail_opening', key: 'generate_on')
  #
  def wait_for_setting(name, value, key: nil)
    wait.until do
      current = Setting.get(name)
      current = current[key] if key
      current == value
    end
  end

  # This checks the number of queued AJAX requests in the frontend JS is zero.
  # It comes in handy when waiting for AJAX requests to be completed
  # before performing further actions.
  #
  # @example
  #  await_empty_ajax_queue
  #
  def await_empty_ajax_queue

    # Waiting not supported/required by mobile app.
    return if %i[desktop_view mobile].include?(self.class.metadata[:app]) # self.class needed to get metadata from within an `it` block.

    # skip on non app related context
    return if page.evaluate_script('typeof(App) !== "function" || typeof($) !== "function"')

    # Always wait a little bit to allow for triggering of requests.
    sleep 0.1

    wait(5).until do
      page.evaluate_script('App.Ajax.queue().length === 0 && $.active === 0 && Object.keys(App.FormHandlerCoreWorkflow.getRequests()).length === 0').eql? true
    end
  rescue Waiter::TimeoutError, Playwright::Error
    nil # Page may navigate away mid-check (e.g. SAML redirect), making App undefined.
    #   The guard above cannot prevent this, as the document can be replaced
    #   between it and the check below.
  end

  # Waits until all pending Core Workflow requests of the legacy frontend are
  # resolved, so their visibility, restriction or value changes are applied.
  #
  # `await_empty_ajax_queue` only waits up to 5 seconds for a Core Workflow
  # request to resolve and silently swallows a timeout, so a slow backend
  # round trip can let execution reach an assertion before the result is
  # applied. `have_css` alone can't tell the difference between "not applied
  # yet" and "never going to match", so give the pending request the full
  # default wait here first.
  #
  # @example
  #  wait_for_core_workflow
  #
  def wait_for_core_workflow
    wait.until { page.evaluate_script('Object.keys(App.FormHandlerCoreWorkflow.getRequests()).length === 0') }
  end

  # Moves the mouse from its current position by the given offset.
  # If the coordinates provided are outside the viewport (the mouse will end up outside the browser window)
  # then the viewport is scrolled to match.
  #
  # @example
  # move_mouse_by(x, y)
  # move_mouse_by(100, 200)
  #
  def move_mouse_by(x_axis, y_axis)
    page.driver.with_playwright_page do |pw_page|
      position = playwright_mouse_position
      playwright_mouse_move(pw_page, position[:x] + x_axis, position[:y] + y_axis)
    end
  end

  # Moves the mouse to element.
  #
  # @example
  # move_mouse_to(page.find('button.hover_me'))
  #
  def move_mouse_to(element)
    page.driver.with_playwright_page do |pw_page|
      box = playwright_fixed_bounding_box(element)
      playwright_mouse_move(pw_page, box['x'], box['y'])
    end
  end

  # Places the collapsed text cursor immediately before the given element by
  # setting the browser's Selection directly, instead of clicking at the
  # element's pixel location. A coordinate click is unreliable for a collapsed,
  # zero-content element such as the <br> of an empty line: its bounding box
  # can collapse to a height of 0, so a click at its coordinates can land on a
  # different line.
  #
  # @example
  # place_cursor_before(blockquote_empty_line)
  #
  def place_cursor_before(element)
    page.execute_script(<<~JS, element.native)
      var range = document.createRange();
      range.setStartBefore(arguments[0]);
      range.collapse(true);

      var selection = window.getSelection();
      selection.removeAllRanges();
      selection.addRange(range);
    JS
  end

  # Clicks and hold (without releasing) in the middle of the given element.
  #
  # @example
  # click_and_hold(ticket)
  # click_and_hold(tr[data-id='1'])
  #
  def click_and_hold(element)
    # element is the native node here, i.e. a Playwright::ElementHandle.
    page.driver.with_playwright_page do |pw_page|
      element.scroll_into_view_if_needed
      box = element.bounding_box
      playwright_mouse_move(pw_page, box['x'] + (box['width'] / 2), box['y'] + (box['height'] / 2))
      pw_page.mouse.down
    end
  end

  # Clicks and hold (without releasing) in the middle of the given element
  # and moves it to the top left of the page to show marcos batches in
  # overview section.
  #
  # @example
  # display_macro_batches(Ticket.first)
  #
  def display_macro_batches(ticket)

    # Get the ticket row DOM element
    element = page.find(:table_row, ticket.id).native

    # Drag the element to the top of the screen, in order to display macro batches.
    #  First, move the mouse to the middle left part of the element to avoid popups interfering with the action.
    #  Then, click and hold the left mouse button.
    #  Next, move the mouse vertically, just below the top edge of the browser.
    #  Finally, move the mouse slightly horizontally to simulate a non-linear drag.
    page.driver.with_playwright_page do |pw_page|
      box = element.bounding_box
      playwright_mouse_move(pw_page, box['x'] + 50, box['y'] + 10)
      pw_page.mouse.down
      playwright_mouse_move(pw_page, box['x'] + 50, 13, steps: 5)
      playwright_mouse_move(pw_page, box['x'] + 53, 13)
    end
  end

  # Counts the macro entries of the batch overlay that are geometrically visible,
  # i.e. whose bounding box overlaps the overlay's clipped area.
  #
  # The entries flex-wrap inside a container capped by `max-height` with
  # `overflow: hidden`, so only some of them fall within the visible clipped
  # area - the rest are still rendered in the DOM, just occluded. The
  # Playwright driver's visibility check only walks the CSS
  # display/visibility/opacity chain and does not consider clipping by an
  # ancestor's overflow, so it would count all of them.
  #
  # @example
  # wait.until { visible_macro_entry_count == 32 }
  #
  def visible_macro_entry_count
    page.evaluate_script(<<~JS)
      (function() {
        var containerRect = document.querySelector('.batch-overlay-macro .batch-overlay-box-inner').getBoundingClientRect();
        return Array.from(document.querySelectorAll('.batch-overlay-macro-entry')).filter(function(el) {
          var r = el.getBoundingClientRect();
          return r.bottom > containerRect.top && r.top < containerRect.bottom;
        }).length;
      })()
    JS
  end

  # Moves the mouse to a position derived from an element's location on the x-axis,
  # combined with an explicit y-coordinate - e.g. sliding a dragged item down to a
  # fixed position near the bottom of the viewport to reveal a drop target that
  # starts below the fold, regardless of the element's own vertical position.
  #
  # @example
  # slide_mouse_to(circle, x_offset: 20, y_coord: target_y)
  #
  def slide_mouse_to(element, x_offset:, y_coord:)
    page.driver.with_playwright_page do |pw_page|
      # The element may still be sliding into place (e.g. a drop target
      #   revealed by a preceding hover/drag step) - #bounding_box also
      #   returns nil while it's not yet visible, so read its settled
      #   position instead of racing the animation.
      box = playwright_fixed_bounding_box(element)
      playwright_mouse_move(pw_page, box['x'] + x_offset, y_coord)
    end
  end

  # Releases the depressed left mouse button at the current mouse location.
  #
  # @example
  # release_mouse
  #
  def release_mouse
    page.driver.with_playwright_page { |pw_page| pw_page.mouse.up }
    await_empty_ajax_queue
  end

  # Playwright's Mouse#move is absolute - remember the pointer position so
  # relative moves can be reproduced.
  #
  def playwright_mouse_move(pw_page, x_coord, y_coord, steps: 1)
    pw_page.mouse.move(x_coord, y_coord, steps: steps)
    @playwright_mouse_position = { x: x_coord, y: y_coord }
  end

  def playwright_mouse_position
    @playwright_mouse_position ||= { x: 0, y: 0 }
  end

  # Playwright counterpart of Capybara::Node::Element#in_fixed_position:
  # waits until an (animated) element stopped moving and returns its bounding box.
  #
  # Takes a Capybara element (not a native handle): an unrelated app update can
  # replace the underlying DOM node while we're waiting (e.g. a collection
  # controller re-rendering a list item), which permanently detaches a fixed
  # native handle - #bounding_box would then return nil forever instead of
  # settling. Reloading before each read re-resolves the element's original
  # query, picking up whatever node currently matches instead of polling a
  # handle that's gone for good.
  #
  def playwright_fixed_bounding_box(element, checks: 100, wait: 0.2)
    previous = element.native.bounding_box

    (checks + 1).times do
      sleep wait

      current = element.reload.native.bounding_box

      # #bounding_box returns nil while the element is detached/not laid out
      # (e.g. mid-transition). Two nil reads in a row are not "stable" - keep
      # waiting for a real box instead of returning nil to the caller.
      return current if current && previous == current

      previous = current
    end

    raise "Element still moving after #{checks} checks"
  end

  class Waiter
    class TimeoutError < StandardError; end

    def initialize(timeout:, interval: 0.2, message: nil)
      @timeout  = timeout
      @interval = interval
      @message  = message
    end

    # Returns the first truthy result of the given block.
    #
    # @raise [Waiter::TimeoutError] If the block returned no truthy result in time
    #
    # @example
    #  wait.until { Ticket.last.state.name == 'closed' }
    #
    def until
      deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout

      while Process.clock_gettime(Process::CLOCK_MONOTONIC) <= deadline
        result = yield
        return result if result

        sleep @interval
      end

      raise TimeoutError, @message || "timed out after #{@timeout} seconds"
    end

    # This method is a derivation of #until
    # which ignores Capybara::ElementNotFound exceptions raised
    # in the given block.
    #
    # @example
    #  wait.until_exists { find('[data-title="example"]') }
    #
    def until_exists
      self.until do

        yield
      rescue Capybara::ElementNotFound
        # doesn't exist yet
      end
    rescue TimeoutError => e
      # cleanup backtrace
      e.set_backtrace(e.backtrace.drop(3))
      raise e
    end

    # This method is a derivation of #until
    # which ignores Capybara::ElementNotFound exceptions raised
    # in the given block.
    #
    # @example
    #  wait.until_disappear { find('[data-title="example"]') }
    #
    def until_disappears
      self.until do

        yield
        false
      rescue Capybara::ElementNotFound
        true
      end
    rescue TimeoutError => e
      # cleanup backtrace
      e.set_backtrace(e.backtrace.drop(3))
      raise e
    end

    # This method loops a given block until the result of it is constant.
    #
    # @example
    #  wait.until_constant { find('.total').text }
    #
    def until_constant
      previous = nil
      rounds   = (@timeout / @interval).to_i

      rounds.times do
        sleep @interval

        latest = yield

        next if latest.nil?
        break if latest == previous

        previous = latest
      end
    end
  end
end

RSpec.configure do |config|
  config.include BrowserTestHelper, type: :system
end
