defmodule MvWeb.Helpers.MemberHelpersTest do
  @moduledoc """
  Tests for the display_name/1 helper function in MemberHelpers.
  """
  use Mv.DataCase, async: true

  alias Mv.Membership.Member
  alias MvWeb.Helpers.MemberHelpers

  describe "display_name/1" do
    test "returns full name when both first_name and last_name are present" do
      member = %Member{
        first_name: "John",
        last_name: "Doe",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "John Doe"
    end

    test "returns email when both first_name and last_name are nil" do
      member = %Member{
        first_name: nil,
        last_name: nil,
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "john@example.com"
    end

    test "returns first_name only when last_name is nil" do
      member = %Member{
        first_name: "John",
        last_name: nil,
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "John"
    end

    test "returns last_name only when first_name is nil" do
      member = %Member{
        first_name: nil,
        last_name: "Doe",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "Doe"
    end

    test "returns email when first_name and last_name are empty strings" do
      member = %Member{
        first_name: "",
        last_name: "",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "john@example.com"
    end

    test "returns email when first_name and last_name are whitespace only" do
      member = %Member{
        first_name: "   ",
        last_name: "  \t  ",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "john@example.com"
    end

    test "trims whitespace from name parts" do
      member = %Member{
        first_name: "  John  ",
        last_name: "  Doe  ",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "John Doe"
    end

    test "handles one empty string and one nil" do
      member = %Member{
        first_name: "",
        last_name: nil,
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "john@example.com"
    end

    test "handles one nil and one empty string" do
      member = %Member{
        first_name: nil,
        last_name: "",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "john@example.com"
    end

    test "handles one whitespace and one nil" do
      member = %Member{
        first_name: "   ",
        last_name: nil,
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "john@example.com"
    end

    test "handles one valid name and one whitespace" do
      member = %Member{
        first_name: "John",
        last_name: "   ",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "John"
    end

    test "handles member with only first_name containing whitespace" do
      member = %Member{
        first_name: "  John  ",
        last_name: nil,
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "John"
    end

    test "handles member with only last_name containing whitespace" do
      member = %Member{
        first_name: nil,
        last_name: "  Doe  ",
        email: "john@example.com"
      }

      assert MemberHelpers.display_name(member) == "Doe"
    end
  end
end
