{% macro badge(check=none, url=none, solo=false, last=false, header=false, color='primary', rgb=none, blank=none, icon=none, alt=none, collapsible=none, text=none, tooltip=none) %}
  {% if check or url %}
    {% if url %}
      <a href="{{ url }}" {% if blank %}target="_blank"{% endif %}
    {% else %}
      <span
    {% endif %}
      class="badge text-bg-{{ color }} fw-normal {% if not header %}mb-1{% endif %} {% if solo and not header %}fs-6{% endif %}" {% if alt %}alt="{{ alt }}"{% endif %}
      {% if tooltip %} data-bs-toggle="tooltip" title="{{ tooltip }}"{% endif %}
    >
    {% if icon %}<i class="ri-{{ icon }}"></i>{% endif %}
    {% if rgb %}<span class="color-rgb {% if rgb == 'any' %}color-any{% endif %} align-bottom border border-black" {% if rgb != 'any' %}style="background-color: #{{ rgb }};"{% endif %}></span>{% endif %}
    {% if collapsible and not last %}<span {% if not solo %}class="d-none d-md-inline"{% endif %}> {{ collapsible }} </span>{% endif %}
    {% if text %}{{ text }}{% endif %}
    {% if url %}
      </a>
    {% else %}
      </span>
    {% endif %}
  {% endif %}
{% endmacro %}

{% macro bricklink(item, solo=false, last=false) %}
  {{ badge(url=item.url_for_bricklink(), solo=solo, last=last, blank=true, color='light border', icon='external-link-line', collapsible='Bricklink', alt='Bricklink') }}
{% endmacro %}

{% macro color(item, icon=none, solo=false, last=false, header=false) %}
  {% if item.fields.color == 9999 %}
    {% set rgb = 'any' %}
  {% else %}
      {% set rgb = item.fields.color_rgb %}
  {% endif %}
  {{ badge(check=item.fields.color_name, solo=solo, last=last, header=header, color=' bg-white text-black border', rgb=rgb, icon='palette-line', collapsible=item.fields.color_name) }}
  {{ badge(check=item.fields.color_transparent, solo=solo, last=last, header=header, color='light border', icon='blur-off-line', collapsible='Transparent') }}
{% endmacro %}

{% macro identifier(id, icon=none, solo=false, last=false, header=false) %}
  {{ badge(check=id, solo=solo, last=last, header=header, color='secondary', icon=icon, text=id) }}
{% endmacro %}

{% macro instructions(item, solo=false, last=false) %}
  {{ badge(url=item.url_for_instructions(), solo=solo, last=last, blank=true, color='light border', icon='file-line', collapsible='Instructions:', text=item.instructions | length, alt='Instructions') }}
{% endmacro %}

{% macro parts(parts, solo=false, last=false) %}
  {{ badge(check=parts, solo=solo, last=last, color='success', icon='shapes-line', collapsible='Parts:', text=parts, alt='Parts') }}
{% endmacro %}

{% macro quantity(quantity, solo=false, last=false) %}
  {{ badge(check=quantity, solo=solo, last=last, color='success', icon='close-line', collapsible='Quantity:', text=quantity, alt='Quantity') }}
{% endmacro %}

{% macro owner(item, owner, solo=false, last=false) %}
  {% if last %}
    {% set tooltip=owner.fields.name %}
  {% else %}
    {% set text=owner.fields.name %}
  {% endif %}
  {{ badge(check=item.fields[owner.as_column()], solo=solo, last=last, color='light text-success-emphasis bg-success-subtle border border-success-subtle', icon='user-line', text=text, alt='Owner', tooltip=tooltip) }}
{% endmacro %}

{% macro print(item, solo=false, last=false, header=false) %}
  {% if item.fields.print %}
    {{ badge(url=item.url_for_print(), solo=solo, last=last, color='light border', icon='paint-brush-line', collapsible='Print') }}
  {% endif %}
{% endmacro %}

{% macro purchase_date(date, solo=false, last=false, date_max_formatted=none) %}
  {% if date_max_formatted and date_max_formatted != date %}
    {# Date range for consolidated sets #}
    {% set date_range = date ~ ' - ' ~ date_max_formatted %}
    {% if last %}
      {% set tooltip=date_range %}
    {% else %}
      {% set text=date_range %}
    {% endif %}
  {% else %}
    {# Single date #}
    {% if last %}
      {% set tooltip=date %}
    {% else %}
      {% set text=date %}
    {% endif %}
  {% endif %}
  {{ badge(check=date, solo=solo, last=last, color='light border', icon='calendar-line', text=text, tooltip=tooltip, collapsible='Date:') }}
{% endmacro %}

{% macro purchase_location(item, purchase_locations, solo=false, last=false) %}
  {% if purchase_locations and item.fields.purchase_location %}
    {% if '|' in item.fields.purchase_location or ',' in item.fields.purchase_location %}
      {# Consolidated mode - multiple purchase locations #}
      {% set separator = '|' if '|' in item.fields.purchase_location else ',' %}
      {% set location_list = item.fields.purchase_location.split(separator) %}
      {% for location_id in location_list %}
        {% if location_id and location_id.strip() and location_id.strip() in purchase_locations.mapping %}
          {% set purchase_location = purchase_locations.mapping[location_id.strip()] %}
          {% if last %}
            {% set tooltip=purchase_location.fields.name %}
          {% else %}
            {% set text=purchase_location.fields.name %}
          {% endif %}
          {{ badge(check=purchase_location, solo=solo, last=last, color='light border', icon='building-line', text=text, tooltip=tooltip, collapsible='Location:') }}
        {% endif %}
      {% endfor %}
    {% else %}
      {# Single purchase location #}
      {% if item.fields.purchase_location.strip() and item.fields.purchase_location.strip() in purchase_locations.mapping %}
        {% set purchase_location = purchase_locations.mapping[item.fields.purchase_location.strip()] %}
        {% if last %}
          {% set tooltip=purchase_location.fields.name %}
        {% else %}
          {% set text=purchase_location.fields.name %}
        {% endif %}
        {{ badge(check=purchase_location, solo=solo, last=last, color='light border', icon='building-line', text=text, tooltip=tooltip, collapsible='Location:') }}
      {% endif %}
    {% endif %}
  {% endif %}
{% endmacro %}

{% macro purchase_price(price, solo=false, last=false) %}
  {% if last %}
    {% set tooltip=price %}
  {% else %}
    {% set text=price %}
  {% endif %}
  {{ badge(check=price, solo=solo, last=last, color='light border', icon='wallet-3-line', text=text, tooltip=tooltip, collapsible='Price:') }}
{% endmacro %}

{% macro set(set, solo=false, last=false, url=None, id=None) %}
  {% if id %}
      {% set url=url_for('set.details', id=id) %}
  {% endif %}
  {{ badge(check=set, url=url, solo=solo, last=last, color='secondary', icon='hashtag', collapsible='Set:', text=set, alt='Set') }}
{% endmacro %}

{% macro storage(item, storages, solo=false, last=false) %}
  {% if storages and item.fields.storage %}
    {% if '|' in item.fields.storage or ',' in item.fields.storage %}
      {# Consolidated mode - multiple storage locations #}
      {% set separator = '|' if '|' in item.fields.storage else ',' %}
      {% set storage_list = item.fields.storage.split(separator) %}
      {% for storage_id in storage_list %}
        {% if storage_id and storage_id.strip() and storage_id.strip() in storages.mapping %}
          {% set storage = storages.mapping[storage_id.strip()] %}
          {% if last %}
            {% set tooltip=storage.fields.name %}
          {% else %}
            {% set text=storage.fields.name %}
          {% endif %}
          {{ badge(url=storage.url(), solo=solo, last=last, color='light text-warning-emphasis bg-warning-subtle border border-warning-subtle', icon='archive-2-line', text=text, alt='Storage', tooltip=tooltip) }}
        {% endif %}
      {% endfor %}
    {% else %}
      {# Single storage location #}
      {% if item.fields.storage.strip() and item.fields.storage.strip() in storages.mapping %}
        {% set storage = storages.mapping[item.fields.storage.strip()] %}
        {% if last %}
          {% set tooltip=storage.fields.name %}
        {% else %}
          {% set text=storage.fields.name %}
        {% endif %}
        {{ badge(url=storage.url(), solo=solo, last=last, color='light text-warning-emphasis bg-warning-subtle border border-warning-subtle', icon='archive-2-line', text=text, alt='Storage', tooltip=tooltip) }}
      {% endif %}
    {% endif %}
  {% endif %}
{% endmacro %}

{% macro tag(item, tag, solo=false, last=false) %}
  {% if last %}
    {% set tooltip=tag.fields.name %}
  {% else %}
    {% set text=tag.fields.name %}
  {% endif %}
  {{ badge(check=item.fields[tag.as_column()], solo=solo, last=last, color='light text-primary-emphasis bg-primary-subtle border border-primary-subtle', icon='price-tag-2-line', text=text, alt='Tag', tooltip=tooltip) }}
{% endmacro %}

{% macro theme(theme, solo=false, last=false) %}
  {% if last %}
    {% set tooltip=theme %}
  {% else %}
    {% set text=theme %}
  {% endif %}
  {{ badge(check=theme, solo=solo, last=last, color='primary', icon='price-tag-3-line', text=text, alt='Theme', tooltip=tooltip) }}
{% endmacro %}

{% macro total_damaged(damaged, solo=false, last=false) %}
  {% if not config['HIDE_TABLE_DAMAGED_PARTS'] %}
    {{ badge(check=damaged, solo=solo, last=last, color='danger', icon='error-warning-line', collapsible='Damaged:', text=damaged, alt='Damaged') }}
  {% endif %}
{% endmacro %}

{% macro total_quantity(quantity, solo=false, last=false) %}
  {{ badge(check=quantity, solo=solo, last=last, color='success', icon='functions', collapsible='Quantity:', text=quantity, alt='Quantity') }}
{% endmacro %}

{% macro total_minifigures(minifigures, solo=false, last=false) %}
  {{ badge(check=minifigures, solo=solo, last=last, color='info', icon='group-line', collapsible='Minifigures:', text=minifigures, alt='Minifigures') }}
{% endmacro %}

{% macro total_missing(missing, solo=false, last=false) %}
  {% if not config['HIDE_TABLE_MISSING_PARTS'] %}
    {{ badge(check=missing, solo=solo, last=last, color='light text-danger-emphasis bg-danger-subtle border border-danger-subtle', icon='question-line', collapsible='Missing:', text=missing, alt='Missing') }}
  {% endif %}
{% endmacro %}

{% macro total_sets(sets, solo=false, last=false) %}
  {{ badge(check=sets, solo=solo, last=last, color='secondary', icon='hashtag', collapsible='Sets:', text=sets, alt='Sets') }}
{% endmacro %}

{% macro total_spare(spare, solo=false, last=false) %}
  {{ badge(check=spare, solo=solo, last=last, color='warning', icon='loop-left-line', collapsible='Spare:', text=spare, alt='Spare') }}
{% endmacro %}

{% macro rebrickable(item, solo=false, last=false) %}
  {{ badge(url=item.url_for_rebrickable(), solo=solo, last=last, blank=true, color='light border', icon='external-link-line', collapsible='Rebrickable', alt='Rebrickable') }}
{% endmacro %}

{% macro year(year, solo=false, last=false) %}
  {{ badge(check=year, solo=solo, last=last, color='secondary', icon='calendar-line', collapsible='Year:', text=year, alt='Year') }}
{% endmacro %}

{% macro render_ordered_badges(item, brickset_tags, brickset_owners, brickset_storages, brickset_purchase_locations, solo=false, last=false, context='grid') %}
  {# Get badge order from config based on context (grid or detail) #}
  {% if context == 'detail' %}
    {% set badge_order = config.get('BADGE_ORDER_DETAIL', ['theme', 'tag', 'year', 'parts', 'instance_count', 'total_minifigures', 'total_missing', 'total_damaged', 'owner', 'storage', 'purchase_date', 'purchase_location', 'purchase_price', 'instructions', 'rebrickable', 'bricklink']) %}
  {% else %}
    {% set badge_order = config.get('BADGE_ORDER_GRID', ['theme', 'year', 'parts', 'total_minifigures', 'owner']) %}
  {% endif %}

  {# Render each badge in the configured order #}
  {% for badge_key in badge_order %}
    {% if badge_key == 'theme' %}
      {# Only sets have themes #}
      {% if item.theme is defined %}
        {{ theme(item.theme.name, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'tag' %}
      {% for tag_item in brickset_tags %}
        {{ tag(item, tag_item, solo=solo, last=last) }}
      {% endfor %}
    {% elif badge_key == 'year' %}
      {# Only sets have years #}
      {% if not last and item.fields.year is defined %}
        {{ year(item.fields.year, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'parts' %}
      {# Only sets and minifigures have number_of_parts #}
      {% if item.fields.number_of_parts is defined %}
        {{ parts(item.fields.number_of_parts, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'instance_count' %}
      {# Only consolidated sets have instance_count #}
      {% if item.fields.instance_count is defined and item.fields.instance_count > 1 %}
        <span class="badge bg-primary"><i class="ri-stack-line"></i> {{ item.fields.instance_count }} copies</span>
      {% endif %}
    {% elif badge_key == 'total_minifigures' %}
      {# Only sets have total_minifigures #}
      {% if item.fields.total_minifigures is defined %}
        {{ total_minifigures(item.fields.total_minifigures, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'total_missing' %}
      {# Sets have total_missing, individual items have missing #}
      {% if item.fields.total_missing is defined %}
        {{ total_missing(item.fields.total_missing, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'total_damaged' %}
      {# Sets have total_damaged, individual items have damaged #}
      {% if item.fields.total_damaged is defined %}
        {{ total_damaged(item.fields.total_damaged, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'owner' %}
      {% for owner_item in brickset_owners %}
        {{ owner(item, owner_item, solo=solo, last=last) }}
      {% endfor %}
    {% elif badge_key == 'storage' %}
      {{ storage(item, brickset_storages, solo=solo, last=last) }}
    {% elif badge_key == 'purchase_date' %}
      {# Sets and part lots have purchase_date methods, individual parts/minifigures have raw fields #}
      {% if not last %}
        {% if item.purchase_date is defined and item.purchase_date is callable %}
          {{ purchase_date(item.purchase_date(), solo=solo, last=last, date_max_formatted=item.purchase_date_max_formatted()) }}
        {% elif item.purchase_date_formatted is defined and item.purchase_date_formatted is callable %}
          {{ purchase_date(item.purchase_date_formatted(), solo=solo, last=last) }}
        {% elif item.fields.purchase_date is defined and item.fields.purchase_date %}
          {{ purchase_date(item.fields.purchase_date, solo=solo, last=last) }}
        {% endif %}
      {% endif %}
    {% elif badge_key == 'purchase_location' %}
      {% if not last %}
        {{ purchase_location(item, brickset_purchase_locations, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'purchase_price' %}
      {# Sets and part lots have purchase_price methods, individual parts/minifigures have raw fields #}
      {% if not last %}
        {% if item.purchase_price is defined and item.purchase_price is callable %}
          {{ purchase_price(item.purchase_price(), solo=solo, last=last) }}
        {% elif item.fields.purchase_price is defined and item.fields.purchase_price %}
          {{ purchase_price(item.fields.purchase_price, solo=solo, last=last) }}
        {% endif %}
      {% endif %}
    {% elif badge_key == 'instructions' %}
      {# Only sets have instructions #}
      {% if not last and not solo and item.instructions is defined %}
        {{ instructions(item, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'rebrickable' %}
      {# Only items with url_for_rebrickable method (sets, parts, minifigures) #}
      {% if not last and item.url_for_rebrickable is defined %}
        {{ rebrickable(item, solo=solo, last=last) }}
      {% endif %}
    {% elif badge_key == 'bricklink' %}
      {# Only items with url_for_bricklink method (sets, parts) #}
      {% if not last and item.url_for_bricklink is defined %}
        {{ bricklink(item, solo=solo, last=last) }}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endmacro %}
