defmodule Mv.Membership.Setting.Changes.NormalizeDefaultFeeTypeId do
  @moduledoc """
  Ash change that normalizes empty strings to nil for default_membership_fee_type_id.

  HTML forms submit empty select values as empty strings (""), but the database
  expects nil for optional UUID fields. This change converts "" to nil.
  """
  use Ash.Resource.Change

  def change(changeset, _opts, _context) do
    default_fee_type_id = Ash.Changeset.get_attribute(changeset, :default_membership_fee_type_id)

    if default_fee_type_id == "" do
      Ash.Changeset.force_change_attribute(changeset, :default_membership_fee_type_id, nil)
    else
      changeset
    end
  end
end
