defmodule MvWeb.GroupLive.ShowAccessibilityTest do
  @moduledoc """
  Accessibility tests for Add/Remove Member functionality.
  Tests ARIA labels, keyboard navigation, and screen reader support.
  """

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

  alias Mv.Membership
  alias Mv.Fixtures

  describe "ARIA labels and roles" do
    test "search input has proper ARIA attributes", %{conn: conn} do
      group = Fixtures.group_fixture()

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

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

      # OR-chain: at least one of these ARIA/role attributes must be present
      assert has_element?(view, "[data-testid=group-show-member-search-input][aria-label]") or
               has_element?(
                 view,
                 "[data-testid=group-show-member-search-input][aria-autocomplete]"
               ) or
               has_element?(view, "[data-testid=group-show-member-search-input][role=combobox]")
    end

    test "search input has correct aria-label and aria-autocomplete attributes", %{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-member-search-input][aria-autocomplete=list]"
             )
    end

    test "remove button has aria-label with tooltip text", %{conn: conn} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()
      group = Fixtures.group_fixture()

      {:ok, member} =
        Membership.create_member(
          %{
            first_name: "Alice",
            last_name: "Smith",
            email: "alice@example.com"
          },
          actor: 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][aria-label]")
    end

    test "add button has correct aria-label", %{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][aria-label]")
    end
  end

  describe "keyboard navigation" do
    test "tab navigation works in inline add member area", %{conn: conn} do
      # This test verifies that keyboard navigation is possible
      # Actual tab order testing would require more complex setup
      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-member-search-input]")
    end

    test "inline input can be closed", %{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-member-search-input]")
    end

    test "enter/space activates buttons when focused", %{conn: conn} do
      # This test verifies that buttons can be activated via keyboard
      # Actual keyboard event testing would require more complex setup
      system_actor = Mv.Helpers.SystemActor.get_system_actor()
      group = Fixtures.group_fixture()

      {:ok, member} =
        Membership.create_member(
          %{
            first_name: "Bob",
            last_name: "Jones",
            email: "bob@example.com"
          },
          actor: system_actor
        )

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

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

      view
      |> element("[data-testid=group-show-member-search-input]")
      |> render_focus()

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

      view
      |> element("[data-member-id='#{member.id}']")
      |> render_click()

      view
      |> element("[data-testid=group-show-add-selected-members-btn]")
      |> render_click()

      assert has_element?(view, "[data-testid=group-show-members-table]", "Bob")
    end

    test "focus management: focus is set to input when opened", %{conn: conn} do
      # This test verifies that focus is properly managed
      # When inline input opens, focus should move to input field
      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-member-search-input]")
    end
  end

  describe "screen reader support" do
    test "search input has proper label for screen readers", %{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-member-search-input][aria-label]")
    end

    test "search results are properly announced", %{conn: conn} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()
      group = Fixtures.group_fixture()

      {:ok, _member} =
        Membership.create_member(
          %{
            first_name: "Charlie",
            last_name: "Brown",
            email: "charlie@example.com"
          },
          actor: system_actor
        )

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

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

      view
      |> element("[data-testid=group-show-member-search-input]")
      |> render_focus()

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

      assert has_element?(view, "#member-dropdown[role=listbox]")
      assert has_element?(view, "#member-dropdown", "Charlie")
    end

    test "flash messages are properly announced", %{conn: conn} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()
      group = Fixtures.group_fixture()

      {:ok, member} =
        Membership.create_member(
          %{
            first_name: "David",
            last_name: "Wilson",
            email: "david@example.com"
          },
          actor: system_actor
        )

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

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

      view
      |> element("[data-testid=group-show-member-search-input]")
      |> render_focus()

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

      view
      |> element("[data-member-id='#{member.id}']")
      |> render_click()

      view
      |> element("[data-testid=group-show-add-selected-members-btn]")
      |> render_click()

      assert has_element?(view, "[data-testid=group-show-members-table]", "David")
    end
  end
end
