defmodule MvWeb.GroupLive.ShowAddRemoveMembersTest do
  @moduledoc """
  UI tests for Add/Remove Member buttons visibility and inline add member display.
  Tests UI rendering and permission-based visibility.
  """

  use MvWeb.ConnCase, async: false
  import Phoenix.LiveViewTest
  use Gettext, backend: MvWeb.Gettext

  alias Mv.Membership
  alias Mv.Fixtures

  describe "Add Member button visibility" do
    @tag role: :read_only
    test "read_only user can access group show page (page permission)", %{conn: conn} do
      group = Fixtures.group_fixture()
      conn = get(conn, "/groups/#{group.slug}")
      assert conn.status == 200
    end

    test "Add Member button is visible for users with :update permission", %{conn: conn} do
      group = Fixtures.group_fixture()

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      assert has_element?(view, "button[phx-click='show_add_member_input']")
    end

    @tag role: :read_only
    test "Add Member button is NOT visible for users without :update permission", %{conn: conn} do
      group = Fixtures.group_fixture()

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      refute has_element?(view, "button[phx-click='show_add_member_input']")
    end

    test "Add Member button is positioned above member table", %{conn: conn} do
      group = Fixtures.group_fixture()

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      # Button should exist
      assert has_element?(view, "button", gettext("Add Member")) ||
               has_element?(view, "a", gettext("Add Member"))
    end
  end

  describe "Remove button visibility" do
    test "Remove button is visible for each member for users with :update permission", %{
      conn: conn
    } do
      group = Fixtures.group_fixture()
      member = Fixtures.member_fixture(%{first_name: "Alice", last_name: "Smith"})
      system_actor = Mv.Helpers.SystemActor.get_system_actor()

      Membership.create_member_group(%{member_id: member.id, group_id: group.id},
        actor: system_actor
      )

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      assert has_element?(view, "[data-testid=group-show-remove-member]")
    end

    @tag role: :read_only
    test "Remove button is NOT visible for users without :update permission", %{conn: conn} do
      group = Fixtures.group_fixture()
      member = Fixtures.member_fixture(%{first_name: "Bob", last_name: "Jones"})
      system_actor = Mv.Helpers.SystemActor.get_system_actor()

      Membership.create_member_group(%{member_id: member.id, group_id: group.id},
        actor: system_actor
      )

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      refute has_element?(view, "[data-testid=group-show-remove-member]")
    end
  end

  describe "inline add member input" do
    test "inline input appears when Add Member button is clicked", %{conn: conn} do
      group = Fixtures.group_fixture()

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      # Click Add Member button
      view
      |> element("button", gettext("Add Member"))
      |> render_click()

      # Inline input should be visible
      assert has_element?(view, "#member-search-input")
    end

    test "search input has correct placeholder", %{conn: conn} do
      group = Fixtures.group_fixture()

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      # Open inline input
      view
      |> element("button", gettext("Add Member"))
      |> render_click()

      assert has_element?(view, "[data-testid=group-show-member-search-input]")
    end

    test "Add button (plus icon) is disabled until member selected", %{conn: conn} do
      group = Fixtures.group_fixture()

      {:ok, view, _html} = live(conn, "/groups/#{group.slug}")

      view
      |> element("button[phx-click='show_add_member_input']")
      |> render_click()

      assert has_element?(view, "[data-testid=group-show-add-selected-members-btn][disabled]")
    end
  end
end
