defmodule MvWeb.MemberLive.IndexGroupsDisplayTest do
  @moduledoc """
  Tests for displaying groups in the member overview.

  Tests cover:
  - Group badges are displayed for members in groups
  - Multiple badges for members in multiple groups
  - No badge for members without groups
  - Badge shows group name correctly
  """
  # async: false to prevent PostgreSQL deadlocks when creating members and groups
  use MvWeb.ConnCase, async: false
  import Phoenix.LiveViewTest
  require Ash.Query

  alias Mv.Membership.{Group, MemberGroup}

  setup do
    system_actor = Mv.Helpers.SystemActor.get_system_actor()

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

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

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

    {:ok, group1} =
      Group
      |> Ash.Changeset.for_create(:create, %{name: "Board Members"})
      |> Ash.create(actor: system_actor)

    {:ok, group2} =
      Group
      |> Ash.Changeset.for_create(:create, %{name: "Active Members"})
      |> Ash.create(actor: system_actor)

    {:ok, _mg1} =
      MemberGroup
      |> Ash.Changeset.for_create(:create, %{member_id: member1.id, group_id: group1.id})
      |> Ash.create(actor: system_actor)

    {:ok, _mg2} =
      MemberGroup
      |> Ash.Changeset.for_create(:create, %{member_id: member1.id, group_id: group2.id})
      |> Ash.create(actor: system_actor)

    {:ok, _mg3} =
      MemberGroup
      |> Ash.Changeset.for_create(:create, %{member_id: member2.id, group_id: group1.id})
      |> Ash.create(actor: system_actor)

    %{member1: member1, member2: member2, member3: member3, group1: group1, group2: group2}
  end

  test "displays group badges for members in groups", %{
    conn: conn,
    group1: group1,
    group2: group2
  } do
    conn = conn_with_oidc_user(conn)
    {:ok, _view, html} = live(conn, "/members")

    assert html =~ group1.name
    assert html =~ group2.name
  end

  test "displays multiple badges for member in multiple groups", %{
    conn: conn,
    member1: member1,
    group1: group1,
    group2: group2
  } do
    conn = conn_with_oidc_user(conn)
    {:ok, _view, html} = live(conn, "/members")
    assert html =~ member1.first_name
    assert html =~ group1.name
    assert html =~ group2.name
  end

  test "shows placeholder for members without groups", %{conn: conn, member3: member3} do
    conn = conn_with_oidc_user(conn)
    {:ok, _view, html} = live(conn, "/members")
    assert html =~ member3.first_name
  end

  test "displays group name correctly in badge", %{conn: conn, group1: group1} do
    conn = conn_with_oidc_user(conn)
    {:ok, _view, html} = live(conn, "/members")
    assert html =~ group1.name
  end
end
