defmodule MvWeb.UserLive.IndexTest do
  use MvWeb.ConnCase, async: true
  import Phoenix.LiveViewTest

  describe "basic functionality" do
    @tag :ui
    test "displays users in a table with basic UI elements", %{conn: conn} do
      # Create test users
      user1 = create_test_user(%{email: "alice@example.com", oidc_id: "alice123"})
      _user2 = create_test_user(%{email: "bob@example.com", oidc_id: "bob456"})

      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Basic table rendering
      assert html =~ "alice@example.com"
      assert html =~ "bob@example.com"

      # UI elements: New User button, action links
      assert html =~ "New User"
      assert html =~ "Edit"
      assert html =~ "Delete"
      assert html =~ ~r/href="[^"]*\/users\/#{user1.id}\/edit"/
    end

    @tag :ui
    test "shows translated titles in different locales", %{conn: conn} do
      # Test German translation
      conn = conn_with_oidc_user(conn)
      conn = Plug.Test.init_test_session(conn, locale: "de")
      {:ok, _view, html_de} = live(conn, "/users")
      assert html_de =~ "Benutzer*innen auflisten"

      # Test English translation
      conn = Plug.Test.init_test_session(conn, locale: "en")
      {:ok, _view, html_en} = live(conn, "/users")
      assert html_en =~ "Listing Users"
    end
  end

  describe "sorting functionality" do
    setup do
      # Create users with different emails for sorting tests
      user_a = create_test_user(%{email: "alpha@example.com", oidc_id: "alpha"})
      user_z = create_test_user(%{email: "zulu@example.com", oidc_id: "zulu"})
      user_m = create_test_user(%{email: "mike@example.com", oidc_id: "mike"})

      %{users: [user_a, user_z, user_m]}
    end

    @tag :slow
    test "initially sorts by email ascending", %{conn: conn} do
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Should show ascending indicator (up arrow)
      assert html =~ "hero-chevron-up"

      # Test actual sort order: alpha should appear before mike, mike before zulu
      alpha_pos = html |> :binary.match("alpha@example.com") |> elem(0)
      mike_pos = html |> :binary.match("mike@example.com") |> elem(0)
      zulu_pos = html |> :binary.match("zulu@example.com") |> elem(0)

      assert alpha_pos < mike_pos, "alpha@example.com should appear before mike@example.com"
      assert mike_pos < zulu_pos, "mike@example.com should appear before zulu@example.com"
    end

    @tag :slow
    test "can sort email descending by clicking sort button", %{conn: conn} do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/users")

      # Click on email sort button and get rendered result
      html = view |> element("button[phx-value-field='email']") |> render_click()

      # Should now show descending indicator (down arrow)
      assert html =~ "hero-chevron-down"

      # Test actual sort order reversed: zulu should now appear before mike, mike before alpha
      alpha_pos = html |> :binary.match("alpha@example.com") |> elem(0)
      mike_pos = html |> :binary.match("mike@example.com") |> elem(0)
      zulu_pos = html |> :binary.match("zulu@example.com") |> elem(0)

      assert zulu_pos < mike_pos,
             "zulu@example.com should appear before mike@example.com when sorted desc"

      assert mike_pos < alpha_pos,
             "mike@example.com should appear before alpha@example.com when sorted desc"
    end

    @tag :ui
    test "toggles sort direction and shows correct icons", %{conn: conn} do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/users")

      # Initially ascending - should show up arrow
      html = render(view)
      assert html =~ "hero-chevron-up"

      # After clicking, should show down arrow
      view |> element("button[phx-value-field='email']") |> render_click()
      html = render(view)
      assert html =~ "hero-chevron-down"

      # Click again to toggle back to ascending
      html = view |> element("button[phx-value-field='email']") |> render_click()
      assert html =~ "hero-chevron-up"

      # Should be back to original ascending order
      alpha_pos = html |> :binary.match("alpha@example.com") |> elem(0)
      mike_pos = html |> :binary.match("mike@example.com") |> elem(0)
      zulu_pos = html |> :binary.match("zulu@example.com") |> elem(0)

      assert alpha_pos < mike_pos, "Should be back to ascending: alpha before mike"
      assert mike_pos < zulu_pos, "Should be back to ascending: mike before zulu"
    end
  end

  describe "checkbox selection functionality" do
    setup do
      user1 = create_test_user(%{email: "user1@example.com", oidc_id: "user1"})
      user2 = create_test_user(%{email: "user2@example.com", oidc_id: "user2"})
      %{users: [user1, user2]}
    end

    @tag :ui
    test "shows checkbox UI elements", %{conn: conn, users: [user1, user2]} do
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Check select all checkbox exists
      assert html =~ ~s(name="select_all")
      assert html =~ ~s(phx-click="select_all")

      # Check individual user checkboxes exist
      assert html =~ ~s(name="#{user1.id}")
      assert html =~ ~s(name="#{user2.id}")
      assert html =~ ~s(phx-click="select_user")
    end

    @tag :ui
    test "can select and deselect individual users", %{conn: conn, users: [user1, user2]} do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/users")

      # Initially, individual checkboxes should exist but not be checked
      assert view |> element("input[type='checkbox'][name='#{user1.id}']") |> has_element?()
      assert view |> element("input[type='checkbox'][name='#{user2.id}']") |> has_element?()

      refute view
             |> element("input[type='checkbox'][name='select_all'][checked]")
             |> has_element?()

      # Select first user checkbox
      html = view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
      assert html =~ "Email"
      assert html =~ to_string(user1.email)

      # The select_all checkbox should still not be checked (not all users selected)
      refute view
             |> element("input[type='checkbox'][name='select_all'][checked]")
             |> has_element?()

      # Deselect user
      html = view |> element("input[type='checkbox'][name='#{user1.id}']") |> render_click()
      assert html =~ "Email"

      refute view
             |> element("input[type='checkbox'][name='select_all'][checked]")
             |> has_element?()
    end

    @tag :ui
    test "select all and deselect all functionality", %{conn: conn, users: [user1, user2]} do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/users")

      # Initially no checkboxes should be checked
      refute view
             |> element("input[type='checkbox'][name='select_all'][checked]")
             |> has_element?()

      refute view
             |> element("input[type='checkbox'][name='#{user1.id}'][checked]")
             |> has_element?()

      refute view
             |> element("input[type='checkbox'][name='#{user2.id}'][checked]")
             |> has_element?()

      # Click select all
      html = view |> element("input[type='checkbox'][name='select_all']") |> render_click()

      # After selecting all, the select_all checkbox should be checked
      assert view
             |> element("input[type='checkbox'][name='select_all'][checked]")
             |> has_element?()

      assert html =~ "Email"
      assert html =~ to_string(user1.email)
      assert html =~ to_string(user2.email)

      # Then deselect all
      html = view |> element("input[type='checkbox'][name='select_all']") |> render_click()

      # After deselecting all, no checkboxes should be checked
      refute view
             |> element("input[type='checkbox'][name='select_all'][checked]")
             |> has_element?()

      refute view
             |> element("input[type='checkbox'][name='#{user1.id}'][checked]")
             |> has_element?()

      refute view
             |> element("input[type='checkbox'][name='#{user2.id}'][checked]")
             |> has_element?()

      assert html =~ "Email"
    end

    @tag :slow
    test "select all automatically checks when all individual users are selected", %{
      conn: conn,
      users: [_user1, _user2]
    } do
      conn = conn_with_oidc_user(conn)
      {:ok, view, html} = live(conn, "/users")

      # Get all user IDs from the rendered HTML by finding all checkboxes with phx-click="select_user"
      # Extract user IDs from the HTML (they appear as name attributes on checkboxes)
      user_ids =
        html
        |> String.split("phx-click=\"select_user\"")
        |> Enum.flat_map(fn part ->
          case Regex.run(~r/name="([^"]+)"[^>]*phx-value-id/, part) do
            [_, user_id] -> [user_id]
            _ -> []
          end
        end)
        |> Enum.uniq()

      # Skip if no users found (shouldn't happen, but be safe)
      if user_ids != [] do
        # Initially nothing should be checked
        refute view
               |> element("input[type='checkbox'][name='select_all'][checked]")
               |> has_element?()

        # Select all users one by one
        Enum.each(user_ids, fn user_id ->
          view |> element("input[type='checkbox'][name='#{user_id}']") |> render_click()
        end)

        # Now select all should be automatically checked (all individual users are selected)
        assert view
               |> element("input[type='checkbox'][name='select_all'][checked]")
               |> has_element?()
      end
    end
  end

  describe "delete functionality" do
    test "can delete a user", %{conn: conn} do
      _user = create_test_user(%{email: "delete-me@example.com"})
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/users")

      # Confirm user is displayed
      assert render(view) =~ "delete-me@example.com"

      # Click the delete button (phx-click="delete" event)
      view |> element("tbody tr:first-child a[data-confirm]") |> render_click()

      # Verify user was actually deleted (should not appear in HTML anymore)
      html = render(view)
      refute html =~ "delete-me@example.com"
      # Table header should still be there
      assert html =~ "Email"
    end

    test "shows delete confirmation", %{conn: conn} do
      _user = create_test_user(%{email: "confirm-delete@example.com"})
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Check that delete link has confirmation attribute
      assert html =~ ~s(data-confirm="Are you sure?")
    end
  end

  describe "navigation" do
    @tag :ui
    test "navigation links point to correct pages", %{conn: conn} do
      user = create_test_user(%{email: "navigate@example.com"})
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Check that user row contains link to show page
      assert html =~ ~s(/users/#{user.id})

      # Check edit link points to correct edit page
      assert html =~ ~s(/users/#{user.id}/edit)

      # Check new user button points to correct new page
      assert html =~ ~s(/users/new)
    end
  end

  describe "translations" do
    @tag :ui
    test "shows translations for selection in different locales", %{conn: conn} do
      conn = conn_with_oidc_user(conn)

      # Test German translations
      conn = Plug.Test.init_test_session(conn, locale: "de")
      {:ok, _view, html_de} = live(conn, "/users")
      assert html_de =~ "Alle Benutzer*innen auswählen"
      assert html_de =~ "Benutzer*in auswählen"

      # Test English translations
      Gettext.put_locale(MvWeb.Gettext, "en")
      {:ok, _view, html_en} = live(conn, "/users")
      # Check that aria-label attributes exist (structure is there)
      assert html_en =~ ~s(aria-label=)
    end
  end

  describe "edge cases" do
    test "handles empty user list gracefully", %{conn: conn} do
      # Don't create any users besides the authenticated one
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Should still show the table structure
      assert html =~ "Email"
    end

    test "handles users with missing OIDC ID", %{conn: conn} do
      _user = create_test_user(%{email: "no-oidc@example.com", oidc_id: nil})
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      assert html =~ "no-oidc@example.com"
      # Should handle nil OIDC ID gracefully
    end

    test "handles very long email addresses", %{conn: conn} do
      long_email = "very.long.email.address.that.might.break.layouts@example.com"
      _user = create_test_user(%{email: long_email})
      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      assert html =~ long_email
    end
  end

  describe "system actor user" do
    test "does not show system actor user in list", %{conn: conn} do
      # Ensure system actor exists (e.g. via get_system_actor in conn_with_oidc_user)
      _system_actor = Mv.Helpers.SystemActor.get_system_actor()
      system_email = Mv.Helpers.SystemActor.system_user_email()

      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      refute html =~ system_email,
             "System actor user (#{system_email}) must not appear in the user list"
    end

    test "destroying system actor user returns error", %{current_user: current_user} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()

      assert {:error, %Ash.Error.Invalid{}} =
               Ash.destroy(system_actor, domain: Mv.Accounts, actor: current_user)
    end
  end

  describe "Password column display" do
    test "user without password shows em dash in Password column", %{conn: conn} do
      # User created with hashed_password: nil (no password) - must not get default password
      user_no_pw =
        create_test_user(%{
          email: "no-password@example.com",
          hashed_password: nil
        })

      conn = conn_with_oidc_user(conn)
      {:ok, view, html} = live(conn, "/users")

      assert html =~ "no-password@example.com"

      # Password column must show "—" (em dash) for user without password, not "Enabled"
      row = view |> element("tr#row-#{user_no_pw.id}") |> render()
      assert row =~ "—", "Password column should show em dash for user without password"

      refute row =~ "Enabled",
             "Password column must not show Enabled when user has no password"
    end

    test "user with password shows Enabled in Password column", %{conn: conn} do
      user_with_pw =
        create_test_user(%{
          email: "with-password@example.com",
          password: "test123"
        })

      conn = conn_with_oidc_user(conn)
      {:ok, view, html} = live(conn, "/users")

      assert html =~ "with-password@example.com"

      row = view |> element("tr#row-#{user_with_pw.id}") |> render()
      assert row =~ "Enabled", "Password column should show Enabled when user has password"
    end
  end

  describe "member linking display" do
    @tag :slow
    test "displays linked member name in user list", %{conn: conn} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()

      # Create member
      {:ok, member} =
        Mv.Membership.create_member(
          %{
            first_name: "Alice",
            last_name: "Johnson",
            email: "alice@example.com"
          },
          actor: system_actor
        )

      # Create user linked to member
      user = create_test_user(%{email: "user@example.com"})

      {:ok, _updated_user} =
        Mv.Accounts.update_user(user, %{member: %{id: member.id}}, actor: system_actor)

      # Create another user without member
      _unlinked_user = create_test_user(%{email: "unlinked@example.com"})

      conn = conn_with_oidc_user(conn)
      {:ok, _view, html} = live(conn, "/users")

      # Should show linked member name
      assert html =~ "Alice Johnson"
      # Should show user email
      assert html =~ "user@example.com"
      # Should show unlinked user
      assert html =~ "unlinked@example.com"
      # Should show "No member linked" or similar for unlinked user
      assert html =~ "No member linked"
    end
  end
end
