defmodule MvWeb.MemberLive.IndexCustomFieldsEdgeCasesTest do
  @moduledoc """
  Edge case tests for custom fields in the member overview.

  Tests cover:
  - Custom field without values (all members have no value)
  - Very long custom field values are correctly displayed
  """
  use MvWeb.ConnCase, async: true
  import Phoenix.LiveViewTest
  require Ash.Query

  alias Mv.Membership.CustomField

  @tag :slow
  test "displays custom field column even when no members have values", %{conn: conn} do
    system_actor = Mv.Helpers.SystemActor.get_system_actor()

    # Create test members without custom field values
    {: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
      )

    # Create custom field with show_in_overview: true but no values
    {:ok, field} =
      CustomField
      |> Ash.Changeset.for_create(:create, %{
        name: "membership_number",
        value_type: :string,
        show_in_overview: true
      })
      |> Ash.create(actor: system_actor)

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

    # Check that the custom field column header is still displayed
    assert html =~ field.name
  end

  @tag :slow
  test "displays very long custom field values correctly", %{conn: conn} do
    system_actor = Mv.Helpers.SystemActor.get_system_actor()

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

    # Create custom field
    {:ok, field} =
      CustomField
      |> Ash.Changeset.for_create(:create, %{
        name: "long_note",
        value_type: :string,
        show_in_overview: true
      })
      |> Ash.create(actor: system_actor)

    # Create very long value (but within limits)
    long_value = String.duplicate("A", 500)

    {:ok, _cfv} =
      Mv.Membership.CustomFieldValue
      |> Ash.Changeset.for_create(:create, %{
        member_id: member.id,
        custom_field_id: field.id,
        value: %{"_union_type" => "string", "_union_value" => long_value}
      })
      |> Ash.create(actor: system_actor)

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

    # Check that the value is displayed (may be truncated in UI, but should be present)
    # We check for at least part of the value
    assert html =~ "A" or html =~ long_value
  end

  @tag :slow
  test "handles multiple custom fields with show_in_overview correctly", %{conn: conn} do
    system_actor = Mv.Helpers.SystemActor.get_system_actor()

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

    # Create multiple custom fields with show_in_overview: true
    {:ok, field1} =
      CustomField
      |> Ash.Changeset.for_create(:create, %{
        name: "field1",
        value_type: :string,
        show_in_overview: true
      })
      |> Ash.create(actor: system_actor)

    {:ok, field2} =
      CustomField
      |> Ash.Changeset.for_create(:create, %{
        name: "field2",
        value_type: :string,
        show_in_overview: true
      })
      |> Ash.create(actor: system_actor)

    {:ok, field3} =
      CustomField
      |> Ash.Changeset.for_create(:create, %{
        name: "field3",
        value_type: :string,
        show_in_overview: true
      })
      |> Ash.create(actor: system_actor)

    # Create values for all fields
    {:ok, _cfv1} =
      Mv.Membership.CustomFieldValue
      |> Ash.Changeset.for_create(:create, %{
        member_id: member.id,
        custom_field_id: field1.id,
        value: %{"_union_type" => "string", "_union_value" => "Value1"}
      })
      |> Ash.create(actor: system_actor)

    {:ok, _cfv2} =
      Mv.Membership.CustomFieldValue
      |> Ash.Changeset.for_create(:create, %{
        member_id: member.id,
        custom_field_id: field2.id,
        value: %{"_union_type" => "string", "_union_value" => "Value2"}
      })
      |> Ash.create(actor: system_actor)

    {:ok, _cfv3} =
      Mv.Membership.CustomFieldValue
      |> Ash.Changeset.for_create(:create, %{
        member_id: member.id,
        custom_field_id: field3.id,
        value: %{"_union_type" => "string", "_union_value" => "Value3"}
      })
      |> Ash.create(actor: system_actor)

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

    # Check that all custom field columns are displayed
    assert html =~ field1.name
    assert html =~ field2.name
    assert html =~ field3.name

    # Check that all values are displayed
    assert html =~ "Value1"
    assert html =~ "Value2"
    assert html =~ "Value3"
  end
end
