defmodule Mv.Membership.CustomFieldShowInOverviewTest do
  @moduledoc """
  Tests for CustomField show_in_overview attribute.

  Tests cover:
  - Creating custom fields with show_in_overview: true
  - Creating custom fields with show_in_overview: false (default)
  - Updating show_in_overview to true
  - Updating show_in_overview to false
  """
  use Mv.DataCase, async: true

  alias Mv.Membership.CustomField

  setup do
    system_actor = Mv.Helpers.SystemActor.get_system_actor()
    %{actor: system_actor}
  end

  describe "show_in_overview attribute" do
    test "creates custom field with show_in_overview: true", %{actor: actor} do
      assert {:ok, custom_field} =
               CustomField
               |> Ash.Changeset.for_create(:create, %{
                 name: "test_field_show",
                 value_type: :string,
                 show_in_overview: true
               })
               |> Ash.create(actor: actor)

      assert custom_field.show_in_overview == true
    end

    test "creates custom field with show_in_overview: true (default)", %{actor: actor} do
      assert {:ok, custom_field} =
               CustomField
               |> Ash.Changeset.for_create(:create, %{
                 name: "test_field_hide",
                 value_type: :string
               })
               |> Ash.create(actor: actor)

      assert custom_field.show_in_overview == true
    end

    test "updates show_in_overview to true", %{actor: actor} do
      {:ok, custom_field} =
        CustomField
        |> Ash.Changeset.for_create(:create, %{
          name: "test_field_update",
          value_type: :string,
          show_in_overview: false
        })
        |> Ash.create(actor: actor)

      assert {:ok, updated_field} =
               custom_field
               |> Ash.Changeset.for_update(:update, %{show_in_overview: true})
               |> Ash.update(actor: actor)

      assert updated_field.show_in_overview == true
    end

    test "updates show_in_overview to false", %{actor: actor} do
      {:ok, custom_field} =
        CustomField
        |> Ash.Changeset.for_create(:create, %{
          name: "test_field_update2",
          value_type: :string,
          show_in_overview: true
        })
        |> Ash.create(actor: actor)

      assert {:ok, updated_field} =
               custom_field
               |> Ash.Changeset.for_update(:update, %{show_in_overview: false})
               |> Ash.update(actor: actor)

      assert updated_field.show_in_overview == false
    end
  end
end
