module MembershipsHelper
  def get_name_or_email(user)
    user.full_name.present? ? user.full_name : user.email
  end

  def image_or_text_avatar(user)
    if user.avatar.attached?
      image_avatar(user)
    else
      text_avatar(user)
    end
  end

  def image_avatar(user)
    image_tag(tiny_variant(user.avatar), class: "rounded-circle", alt: "User Avatar")
  end

  def text_avatar(user, size: 2.5)
    font_size = size / 2.5

    if user.first_name.present? && user.last_name.present?
      initials = (user.first_name.first + user.last_name.first).upcase
    else
      initials = user.email.first(2).upcase
    end
    
    content_tag(:div, 
                class: "bg-light border rounded-circle d-inline-flex justify-content-center align-items-center", 
                style: "width:#{size}rem;height:#{size}rem;") do
      content_tag(:span, initials, class: "h-5 m-0 p-0 text-muted no-line-height", style: "font-size:#{font_size}rem")
    end
  end

  def team_member_list(members)
    list_classes = "list-unstyled d-flex flex-row justify-content-start align-items-center flex-nowrap overflow-hidden"

    content_tag(:ul, class: list_classes) do
      members.each.with_index do |member, index|
        concat content_tag(:li, image_or_text_avatar(member), 
                           class: class_string("rounded-circle border border-white border-thicker", "ml-n3" => !index.zero?),
                           style: "z-index:#{100-index}")
      end
    end
  end
end
