defmodule MvWeb.GroupLiveHelpers do
  @moduledoc """
  Helpers for Group LiveView tests (e.g. group show add/remove member flow).

  Use these to reduce duplication in tests that open the add member area,
  search, select, and add members.
  """

  import Phoenix.LiveViewTest

  @doc """
  Opens the inline add member area by clicking "Add Member".
  """
  def open_add_member(view) do
    view
    |> element("button[phx-click='show_add_member_input']")
    |> render_click()
  end

  @doc """
  Triggers member search by focusing the input and sending a form change with the given query.
  """
  def search_member(view, query) do
    view
    |> element("[data-testid=group-show-member-search-input]")
    |> render_focus()

    view
    |> element("form[phx-change='search_members']")
    |> render_change(%{"member_search" => query})
  end

  @doc """
  Clicks the option for the given member in the dropdown (by data-member-id).
  """
  def select_member(view, member) do
    view
    |> element("[data-member-id='#{member.id}']")
    |> render_click()
  end

  @doc """
  Clicks the "Add" button (add_selected_members).
  """
  def add_selected(view) do
    view
    |> element("[data-testid=group-show-add-selected-members-btn]")
    |> render_click()
  end

  @doc """
  Clicks the "Cancel" button to close the inline add member area.
  """
  def cancel_add_member(view) do
    view
    |> element("button[phx-click='hide_add_member_input']")
    |> render_click()
  end
end
