{# ---------- PAC-INFO MACROS ---------- #}
{# These macros render semantic HTML with light class hooks. #}

{%- macro info_card(pac_info) -%}
<section class="lf-info-card">
    <div class="lf-info-title">{{ pac_info.display_name }}</div>
  <div class="lf-info-grid">
    {% if pac_info.image_url %}
        <img src="{{ pac_info.image_url }}" class="prod_img lf-info-item" alt="">
    {% endif %}
  
    {% if pac_info.main_category %}
        {% for c in pac_info.pac_id.categories %}
            {% for k, v in c.segments_as_dict().items() %}
                {% if k != "key" %}
                <div class="lf-info-item">
                    {% if k %}
                    <div class="lf-info-key">{{ k }}</div>
                    {% else %}
                    <div class="lf-info-key"> <i>{{"No Key" }}</i></div>
                    {% endif %}
                    <div class="lf-info-val">{{ v }}</div>
                </div>
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% endif %}

    </div>
    {% if pac_info.safety_pictograms %}
        {% for p in pac_info.safety_pictograms.values() %}
        <figure class="lf-figure lf-figure--image">
            <img src="{{ p.value|e }}" alt="{{ p.label|e }}" class="lf-attr-image">
            <figcaption class="lf-figure__caption sr-only">{{ p.label }}</figcaption>
        </figure>
        {% endfor %}
    {% endif %}

  </section>
{%- endmacro -%}


{%- macro key_value_table(pairs) -%}
  {# pairs: dict-like object #}
  <table class="lf-table lf-table--kv">
    <tbody>
      {%- for k, v in pairs.items() -%}
        {%- if k != "key" -%}
          <tr>
            <th class="lf-th lf-th--key">{{ k }}</th>
            <td class="lf-td lf-td--val">{{ v }}</td>
          </tr>
        {%- endif -%}
      {%- endfor -%}
    </tbody>
  </table>
{%- endmacro -%}


{%- macro category_block(category) -%}
  <div class="lf-section lf-section--category">
    <h5 class="lf-section__title">{{ category.__class__.__name__ }}</h5>
    {{ key_value_table(category.segments_as_dict()) }}
  </div>
{%- endmacro -%}


{%- macro services_table(user_handover_group) -%}
<div class="lf-section lf-section--services">
  <div class="lf-origin-hint">from {{ user_handover_group.origin }}</div>
  <div class="lf-services">
    {%- for s in user_handover_group.services -%}
      <a href="{{ s.url }}" target="_blank" rel="noopener" class="lf-service">
        <span class="lf-service__icon"> 
            {% include "external-link.svg" %}
        </span>
        <span class="lf-service__name">{{ s.service_name }}</span>
      </a>
    {%- endfor -%}
  </div>
</div>
{%- endmacro -%}


{%- macro reference_value(value) -%}
<span class="lf-reference">{{ value }}</span>
{%- endmacro -%}


{%- macro attribute_row(a) -%}
  <tr>
    <th class="lf-th lf-th--key">{{ a.label }}</th>
    <td class="lf-td lf-td--val">
        {% set value = a.value %}
        {%- if is_image(value) -%}
            <figure class="lf-figure lf-figure--image">
                <img src="{{ value|e }}" alt="{{ label|e }}" class="lf-attr-image">
                <figcaption class="lf-figure__caption sr-only">{{ label }}</figcaption>
            </figure>
        {%- elif is_url(value) -%}
            <a href="{{ value|e }}" target="_blank" rel="noopener" class="lf-link">{{ value }}</a>
        {%- elif is_reference(value) -%}
            {{ reference_value(value)}}
        {%- else -%}
            <span class="lf-text">{{ value }}</span>
        {%- endif -%}
    </td>
  </tr>
{%- endmacro -%}


{%- macro attribute_group_block(ag) -%}
  <div class="lf-section lf-section--attributes">
    <h5 class="lf-section__title">
      {{ ag.label }} <span class="lf-origin-hint">(from {{ ag.origin }})</span>
    </h5>
    <table class="lf-table lf-table--kv">
      <tbody>
        {%- for a in ag.attributes.values() -%}
          {{ attribute_row(a) }}
        {%- endfor -%}
      </tbody>
    </table>
</div>
{%- endmacro -%}


{%- macro data_table_inline(dt) -%}
  {# Renders your DataTable structure; expects filters: is_data_table(dt) #}
  {%- if dt.data | length == 1 -%}
    <table class="lf-table lf-table--kv">
      <tbody>
        {%- for i in range(dt.col_names | length) -%}
          <tr>
            <th class="lf-th lf-th--key">{{ dt.col_names[i] }}</th>
            <td class="lf-td lf-td--val">{{ dt.data[0][i] }}</td>
          </tr>
        {%- endfor -%}
      </tbody>
    </table>
  {%- else -%}
    <table class="lf-table lf-table--grid">
      <thead>
        <tr>
          {%- for rn in dt.col_names -%}
            <th class="lf-th">{{ rn }}</th>
          {%- endfor -%}
        </tr>
      </thead>
      <tbody>
        {%- for row in dt.data -%}
          <tr>
            {%- for e in row -%}
              <td class="lf-td">{{ e }}</td>
            {%- endfor -%}
          </tr>
        {%- endfor -%}
      </tbody>
    </table>
  {%- endif -%}
{%- endmacro -%}


{%- macro attached_data_block(attached_data) -%}
  <div class="lf-section lf-section--attached">
    {%- for name, trex in attached_data.items() -%}
      <div class="lf-subsection">
        <h5 class="lf-subsection__title">{{ name }}</h5>
        <table class="lf-table lf-table--kv">
          <tbody>
            {%- for k, v in trex.items() -%}
              <tr>
                <th class="lf-th lf-th--key">{{ k }}</th>
                <td class="lf-td lf-td--val">
                  {%- if is_data_table(v) -%}
                    {{ data_table_inline(v) }}
                  {%- else -%}
                    {{ v }}
                  {%- endif -%}
                </td>
              </tr>
            {%- endfor -%}
          </tbody>
        </table>
    </div>
    {%- endfor -%}
    </div>
{%- endmacro -%}
