{# kit_index_inventory_artifacts_v3
 Copyright (C) 2026 Alex Stoyanov

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with this program.
 If not, see <https://www.gnu.org/licenses/>.
#}
<!doctype html>
<html class="light" lang="en">
<head>
  <meta charset="utf-8">
  <style>
    {% include 'partials/forge_tailwind.j2' %}
    {% include 'partials/material_symbols_local.j2' %}

    @page {
      size: {{ page_size_css }};
      margin: 0;
    }

    @media print {
      body {
        background-color: white !important;
        color: black !important;
      }
      .print-sheet {
        box-shadow: none !important;
        margin: 0 !important;
      }
    }

    .print-sheet {
      box-sizing: border-box;
      width: {{ page_width_mm }}mm;
      height: {{ page_height_mm }}mm;
      margin: 0 auto;
      overflow: hidden;
      page-break-after: always;
      background: white;
    }

    .print-sheet:last-child {
      page-break-after: auto;
    }
  </style>
</head>
<body class="bg-background-light font-display antialiased">
{% macro inventory_table(rows) %}
  <div class="overflow-hidden border border-gray-200 rounded">
    <table class="w-full text-left text-sm">
      <thead class="bg-gray-50 text-gray-500 font-medium border-b border-gray-200">
        <tr>
          <th class="px-3 py-2">Component ID</th>
          <th class="px-3 py-2">Designated Custodian</th>
          <th class="px-3 py-2">Storage Location</th>
          <th class="px-3 py-2 text-center">Status</th>
        </tr>
      </thead>
      <tbody class="divide-y divide-gray-200">
        {% set ns_table = namespace(has_rows=false) %}
        {% for row in rows %}
        {% if row %}
        {% set ns_table.has_rows = true %}
        <tr class="bg-white">
          <td class="px-3 py-3 font-mono font-medium text-gray-900">
            {{ row.component_id }}
            <div class="text-[10px] text-gray-500 font-normal mt-1">{{ row.detail }}</div>
          </td>
          <td class="px-3 py-3">
            <div class="h-6 border-b border-gray-300 border-dashed w-full"></div>
          </td>
          <td class="px-3 py-3">
            <div class="h-6 border-b border-gray-300 border-dashed w-full"></div>
          </td>
          <td class="px-3 py-3 text-center">
            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-50 text-gray-700 border border-blue-100">
              {{ row.status if row.status is defined else 'Generated' }}
            </span>
          </td>
        </tr>
        {% endif %}
        {% endfor %}
        {% if not ns_table.has_rows %}
        <tr class="bg-white">
          <td colspan="4" class="px-3 py-3 text-sm text-gray-500">No inventory entries.</td>
        </tr>
        {% endif %}
      </tbody>
    </table>
  </div>
{% endmacro %}

{% set kit_qr_pages = pages | rejectattr('instructions_full_page') | list %}
{% set first_page_row_limit = 4 %}
{% set continuation_page_row_limit = 14 %}
{% set ns = namespace(total_chunks=0) %}
{% for page in kit_qr_pages %}
{% set ns.total_chunks = ns.total_chunks + (page.qr_items | length) %}
{% endfor %}

{% if inventory_rows is defined and inventory_rows %}
{% set row_source = inventory_rows %}
{% else %}
{% set ns_rows = namespace(items=[]) %}
{% for index_page in kit_qr_pages %}
{% set first_qr = index_page.qr_items[0].index if index_page.qr_items else none %}
{% set last_qr = index_page.qr_items[-1].index if index_page.qr_items else none %}
{% if first_qr is not none and last_qr is not none %}
{% set detail = 'KIT %02d' % first_qr %}
{% if last_qr != first_qr %}
{% set detail = detail ~ (' to KIT %02d' % last_qr) %}
{% endif %}
{% else %}
{% set detail = 'No QR chunks' %}
{% endif %}
{% set ns_rows.items = ns_rows.items + [{
  'component_id': 'KIT-PAGE-' ~ ('%02d' % index_page.page_num),
  'detail': detail,
  'status': 'Generated'
}] %}
{% endfor %}
{% set row_source = ns_rows.items %}
{% endif %}

{% set rows_per_page = first_page_row_limit %}
{% set row_chunks = row_source | batch(rows_per_page, none) | list %}
{% if not row_chunks %}
{% set row_chunks = [[]] %}
{% endif %}
{% set total_pages = row_chunks | length %}

{% for page_rows in row_chunks %}
  <section class="print-sheet flex flex-col p-[15mm] border border-gray-100">
    {% set forge_page_label = 'Page ' ~ loop.index ~ ' / ' ~ total_pages %}
    {% set forge_header_title = copy.title %}
    {% set forge_header_subtitle = copy.subtitle %}
    {% set forge_header_kicker = 'The Forge // Secure Offline Storage' %}
    {% set forge_header_classification = 'Authorized Personnel Only' %}
    {% set forge_header_guidance = copy.header_guidance %}
    {% set forge_header_ref = doc_id %}
    {% set forge_header_icon = 'inventory_2' %}
    {% include 'partials/forge_shell_header.j2' %}

    <div class="grid grid-cols-2 gap-4 text-sm mb-6">
      <div>
        <p class="text-gray-500 uppercase text-xs font-bold tracking-wider mb-1">QR Pages</p>
        <p class="font-mono text-gray-900">{{ kit_qr_pages | length }}</p>
      </div>
      <div>
        <p class="text-gray-500 uppercase text-xs font-bold tracking-wider mb-1">QR Chunks</p>
        <p class="font-mono text-gray-900">{{ ns.total_chunks }}</p>
      </div>
    </div>

    <div class="border-l-4 border-primary bg-blue-50 p-4 mb-6">
      <div class="flex gap-3">
        <span class="material-symbols-outlined text-primary text-xl">warning</span>
        <div>
          <h3 class="font-bold text-gray-900 text-sm uppercase mb-1">{{ copy.warning_title }}</h3>
          <p class="text-sm text-gray-700 leading-relaxed">
            {{ copy.warning_body }}
          </p>
        </div>
      </div>
    </div>

    <div class="mb-6">
      <h2 class="text-lg font-bold text-gray-900 uppercase tracking-wider border-b border-gray-200 pb-2 mb-4 flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">inventory_2</span>
        {{ copy.hardware_inventory_label }}
      </h2>
      {{ inventory_table(page_rows) }}
    </div>

    <div class="mb-8">
      <h2 class="text-lg font-bold text-gray-900 uppercase tracking-wider border-b border-gray-200 pb-2 mb-4 flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">verified_user</span>
        {{ copy.chain_of_custody_label }}
      </h2>
      <div class="grid grid-cols-2 gap-x-6 gap-y-3">
        {% for line in instructions.lines %}
        <div class="flex items-start gap-3 p-3 border border-gray-100 rounded bg-gray-50/50">
          <div class="w-6 h-6 border-2 border-gray-400 rounded bg-white flex-shrink-0 mt-0.5"></div>
          <div class="flex-1">
            <p class="text-sm font-semibold text-gray-900">{{ line }}</p>
          </div>
        </div>
        {% endfor %}
        {% if instructions.scan_hint %}
        <div class="flex items-start gap-3 p-3 border border-gray-100 rounded bg-gray-50/50">
          <div class="w-6 h-6 border-2 border-gray-400 rounded bg-white flex-shrink-0 mt-0.5"></div>
          <div class="flex-1">
            <p class="text-sm font-semibold text-gray-900">{{ instructions.scan_hint }}</p>
          </div>
        </div>
        {% endif %}
      </div>
    </div>

    <section class="mt-auto pt-8 border-t border-gray-200 flex justify-between items-end mb-4">
      <div class="text-xs text-gray-400">
        <p>{{ forge_copy.protocol_label }} {{ forge_copy.version }}</p>
        <p>Printed from secure session ID: <span class="font-mono text-gray-600">{{ doc_id }}</span></p>
      </div>
      {% if loop.last %}
      <div class="flex flex-col items-end">
        <div class="text-right mb-2">
          <p class="text-[10px] uppercase tracking-widest text-gray-400 font-bold mb-1">
            Custodian Signature
          </p>
          <div class="w-48 h-10 border-b border-gray-300 border-dashed"></div>
        </div>
        <div class="flex items-center gap-1 text-gray-500">
          <span class="material-symbols-outlined text-sm">lock</span>
          <span class="text-[10px] font-mono">Verification Complete</span>
        </div>
      </div>
      {% endif %}
    </section>

    {% set forge_footer_left = forge_copy.generator_label %}
    {% set forge_footer_center = forge_page_label %}
    {% set forge_footer_right = copy.footer_guidance %}
    {% include 'partials/forge_shell_footer.j2' %}
  </section>
{% endfor %}
</body>
</html>
