{% import 'macro/accordion.html' as accordion %}
{% import 'macro/badge.html' as badge %}
{% import 'macro/card.html' as card %}
{% import 'macro/form.html' as form %}

<div {% if not solo %}id="set-{{ item.fields.consolidated_id or item.fields.id }}"{% endif %} class="card mb-3 flex-fill {% if solo %}card-solo{% endif %}{% if current_viewing %} border-secondary{% endif %}"{% if current_viewing %} style="border-width: 3px !important;"{% endif %}
  data-set-id="{{ item.fields.consolidated_id or item.fields.id }}" data-rebrickable-id="{{ item.fields.set }}"
  {% if item.fields.instance_count is defined %}data-instance-count="{{ item.fields.instance_count }}" data-instance-ids="{{ item.fields.instance_ids }}"{% endif %}
  {% if not solo and not tiny %}
    data-index="{{ index }}" data-number="{{ item.fields.set }}" data-set="{{ item.fields.set.split('-')[0] | int }}{{ '%05d' | format(item.fields.set.split('-')[1] | int) }}" data-name="{{ item.fields.name | lower }}" data-parts="{{ item.fields.number_of_parts }}"
    data-year="{{ item.fields.year }}" data-theme="{{ item.theme.name | lower }}" data-theme-id="{{ item.theme.id }}"
    {% if not config['HIDE_SET_INSTRUCTIONS'] %}
      data-has-missing-instructions="{{ (item.instructions | length == 0) | int }}"
    {% endif %}
    data-has-minifigures="{{ (item.fields.total_minifigures > 0) | int }}" data-minifigures="{{ item.fields.total_minifigures }}"
    {% if not config['HIDE_TABLE_MISSING_PARTS'] %}
      data-has-missing="{{ (item.fields.total_missing > 0) | int }}" data-missing="{{ item.fields.total_missing }}"
    {% endif %}
    {% if not config['HIDE_TABLE_DAMAGED_PARTS'] %}
      data-has-damaged="{{ (item.fields.total_damaged > 0) | int }}" data-damaged="{{ item.fields.total_damaged }}"
    {% endif %}
    {% if item.fields.purchase_date is not none %}data-purchase-date="{{ item.fields.purchase_date }}"{% endif %}
    {% if item.fields.purchase_price is not none %}data-purchase-price="{{ item.fields.purchase_price }}"{% endif %}
    data-has-purchase-location="{{ item.fields.purchase_location is not none | int }}"
    {% if item.fields.purchase_location is not none %}
      data-purchase-location="{{ item.fields.purchase_location }}"
      {% if item.fields.purchase_location in brickset_purchase_locations.mapping %}data-search-purchase-location="{{ brickset_purchase_locations.mapping[item.fields.purchase_location].fields.name | lower }}"{% endif %}
    {% endif %}
    data-has-storage="{{ item.fields.storage is not none | int }}"
    {% if item.fields.storage is not none %}
      data-storage="{{ item.fields.storage }}"
      {% if item.fields.storage in brickset_storages.mapping %}data-search-storage="{{ brickset_storages.mapping[item.fields.storage].fields.name | lower }}"{% endif %}
    {% endif %}
    {% for status in brickset_statuses %}
      {% with checked=item.fields[status.as_column()] %}
        {% if checked %}
          data-{{ status.as_dataset() }}="{{ checked }}"
        {% endif %}
      {% endwith %}
    {% endfor %}
    {% for owner in brickset_owners %}
      {% with checked=item.fields[owner.as_column()] %}
        {% if checked %}
          data-{{ owner.as_dataset() }}="{{ checked }}" data-search-owner-{{ loop.index }}="{{ owner.fields.name | lower }}"
        {% endif %}
      {% endwith %}
    {% endfor %}
    {% for tag in brickset_tags %}
      {% with checked=item.fields[tag.as_column()] %}
        {% if checked %}
          data-{{ tag.as_dataset() }}="{{ checked }}" data-search-tag-{{ loop.index }}="{{ tag.fields.name | lower }}"
        {% endif %}
      {% endwith %}
    {% endfor %}
  {% endif %}
>
  {{ card.header(item, item.fields.name, solo=solo, identifier=item.fields.set) }}
  {{ card.image(item, solo=solo, last=last, caption=item.fields.name, alt=item.fields.set) }}
  <div class="card-body border-bottom-0 {% if not solo %}p-1{% endif %}"{% if current_viewing %} style="border-color: var(--bs-border-color) !important; border-width: 1px !important;"{% endif %}>
    {# Render badges in configured order based on context (grid view vs detail view) #}
    {{ badge.render_ordered_badges(item, brickset_tags, brickset_owners, brickset_storages, brickset_purchase_locations, solo=solo, last=last, context='detail' if solo else 'grid') }}
  </div>
  {# Show notes based on context and config #}
  {% if item.fields.description and item.fields.description | trim %}
    {% if (solo and config['SHOW_NOTES_DETAIL']) or (not solo and config['SHOW_NOTES_GRID']) %}
      <div class="card-body border-bottom-0 {% if not solo %}p-1{% endif %} pt-0"{% if current_viewing %} style="border-color: var(--bs-border-color) !important; border-width: 1px !important;"{% endif %}>
        <div class="alert alert-info mb-0 {% if not solo %}p-1 small{% endif %}" role="alert">
          <i class="ri-sticky-note-line me-1"></i>{{ item.fields.description }}
        </div>
      </div>
    {% endif %}
  {% endif %}
  {% if not tiny and brickset_statuses | length %}
    <ul class="list-group list-group-flush card-check border-bottom-0"{% if current_viewing %} style="border-color: var(--bs-border-color) !important; border-width: 1px !important;"{% endif %}>
      {% for status in brickset_statuses %}
        <li class="d-flex list-group-item {% if not solo %}p-1{% endif %} text-nowrap">
          {% if item.fields.instance_count is defined and item.fields.instance_count > 1 %}
            {# Consolidated set - show mixed status indicator #}
            {% set status_count = item.fields[status.as_column() + '_count'] %}
            {% set total_count = item.fields.instance_count %}
            {% if status_count == 0 %}
              {# None checked #}
              <input class="form-check-input px-1" type="checkbox" disabled>
              <label class="form-check-label">
                {{ status.fields.name }}
                <small class="text-muted ms-1">(0/{{ total_count }})</small>
              </label>
            {% elif status_count == total_count %}
              {# All checked #}
              <input class="form-check-input px-1" type="checkbox" checked disabled>
              <label class="form-check-label">
                {{ status.fields.name }}
                <small class="text-muted ms-1">({{ total_count }}/{{ total_count }})</small>
              </label>
            {% else %}
              {# Partial - some checked #}
              <input class="form-check-input px-1 partial-status" type="checkbox" disabled>
              <label class="form-check-label">
                {{ status.fields.name }}
                <small class="text-muted ms-1">({{ status_count }}/{{ total_count }})</small>
              </label>
            {% endif %}
          {% else %}
            {# Individual set - use normal checkbox #}
            {{ form.checkbox(status.fields.name, item.fields.id, status.as_dataset(), status.url_for_set_state(item.fields.id), item.fields[status.as_column()], parent='set', delete=delete) }}
          {% endif %}
        </li>
      {% endfor %}
    </ul>
  {% endif %}
  {% if solo %}
    <div class="accordion accordion-flush border-top" id="set-details"{% if current_viewing %} style="border-color: var(--bs-border-color) !important; border-width: 1px !important;"{% endif %}>
      {% if not delete %}
        {% if not config['HIDE_SET_INSTRUCTIONS'] %}
          {{ accordion.header('Instructions', 'instructions', 'set-details', expanded=open_instructions, quantity=item.instructions | length, icon='file-line', class='p-0') }}
          <div class="list-group list-group-flush">
            {% if item.instructions | length %}
              {% for instruction in item.instructions %}
              <a class="list-group-item list-group-item-action" href="{{ instruction.url() }}" target="_blank"><i class="ri-arrow-right-long-line"></i> <i class="ri-{{ instruction.icon() }}"></i> {{ instruction.filename }}</a>
              {% endfor %}
            {% else %}
              <span class="list-group-item list-group-item-action text-center"><i class="ri-error-warning-line"></i> No instructions file found.</span>
              {% if g.login.is_authenticated() %}
                <a class="list-group-item list-group-item-action" href="{{ url_for('instructions.upload') }}"><i class="ri-upload-line"></i> Upload an instructions file</a>
              {% endif %}
            {% endif %}
            {% if g.login.is_authenticated() %}
              <a class="list-group-item list-group-item-action" href="{{ url_for('instructions.download', set=item.fields.set) }}"><i class="ri-download-line"></i> Download instructions</a>
            {% endif %}
          </div>
          {{ accordion.footer() }}
        {% endif %}
        {{ accordion.table(item.parts(), 'Parts', 'parts-inventory', 'set-details', 'part/table.html', icon='shapes-line', hamburger_menu=g.login.is_authenticated())}}
        {% for minifigure in item.minifigures() %}
          {{ accordion.table(minifigure.parts(), minifigure.fields.name, minifigure.fields.figure, 'set-details', 'part/table.html', quantity=minifigure.fields.quantity, icon='group-line', image=minifigure.url_for_image(), alt=minifigure.fields.figure, details=minifigure.url(), hamburger_menu=g.login.is_authenticated())}}
        {% endfor %}
        {% include 'set/management.html' %}
      {% endif %}
      {% if g.login.is_authenticated() %}
        {{ accordion.header('Danger zone', 'danger-zone', 'set-details', expanded=delete, danger=true, class='text-end') }}
          {% if delete %}
            <form action="{{ item.url_for_do_delete() }}" method="post">
              {% if error %}<div class="alert alert-danger text-start" role="alert"><strong>Error:</strong> {{ error }}.</div>{% endif %}
              <div class="alert alert-danger text-center" role="alert">You are about to delete a set. This action is irreversible.</div>
              <a class="btn btn-primary" href="{{ item.url() }}" role="button"><i class="ri-arrow-left-long-line"></i> Back to the set</a>
              <button type="submit" class="btn btn-danger"><i class="ri-close-line"></i> Delete the set</button>
            </form>
          {% else %}
            <a href="{{ item.url_for_delete() }}" class="btn btn-danger" role="button"><i class="ri-close-line"></i> Delete the set</a>
          {% endif %}
        {{ accordion.footer() }}
      {% endif %}
    </div>
    <div class="card-footer"></div>
  {% endif %}
</div>

{% if solo and g.login.is_authenticated() %}
<!-- Quick Add Individual Part Modal -->
<div class="modal fade" id="quickAddIndividualPartModal" tabindex="-1" aria-labelledby="quickAddModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="quickAddModalLabel">Add to Individual Parts Inventory</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <form id="quickAddForm">
          <!-- Part info (read-only) -->
          <div class="mb-3">
            <label class="form-label fw-bold">Part</label>
            <div class="d-flex align-items-center">
              <img id="quickAddPartImage" src="" alt="" style="max-width: 100px; max-height: 100px;" class="me-3">
              <div>
                <div id="quickAddPartName" class="fw-bold"></div>
                <div id="quickAddPartNumber" class="text-muted small"></div>
                <div id="quickAddPartColor" class="text-muted small"></div>
              </div>
            </div>
          </div>

          <input type="hidden" id="quickAddPart" name="part">
          <input type="hidden" id="quickAddColor" name="color">

          <!-- Quantity -->
          <div class="mb-3">
            <label for="quickAddQuantity" class="form-label">Quantity <span class="text-danger">*</span></label>
            <input type="number" class="form-control" id="quickAddQuantity" name="quantity" value="1" min="1" required>
          </div>

          <!-- Storage (optional) -->
          <div class="mb-3">
            <label for="quickAddStorage" class="form-label">Storage (optional)</label>
            <select class="form-select" id="quickAddStorage" name="storage">
              <option value="">-- No storage --</option>
              {% for storage in brickset_storages %}
                <option value="{{ storage.fields.id }}">{{ storage.fields.name }}</option>
              {% endfor %}
            </select>
          </div>

          <!-- Purchase Location (optional) -->
          <div class="mb-3">
            <label for="quickAddPurchaseLocation" class="form-label">Purchase Location (optional)</label>
            <select class="form-select" id="quickAddPurchaseLocation" name="purchase_location">
              <option value="">-- No purchase location --</option>
              {% for location in brickset_purchase_locations %}
                <option value="{{ location.fields.id }}">{{ location.fields.name }}</option>
              {% endfor %}
            </select>
          </div>

          <!-- Description/Notes (optional) -->
          <div class="mb-3">
            <label for="quickAddDescription" class="form-label">Notes (optional)</label>
            <textarea class="form-control" id="quickAddDescription" name="description" rows="2"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" id="quickAddSubmit">Add to Inventory</button>
      </div>
    </div>
  </div>
</div>
{% endif %}
