{% extends "base.html" %}
{% block body_class %}themed-bg{% endblock %}

{% block content %}
<div class="container">
  <h1 class="page-title">Subject Normalizer</h1>

  <div class="card" style="padding:16px; margin-bottom:16px;">
    <div class="meta-grid">
      <div>
        <div class="meta-label">Mode</div>
        <div class="subject-title" style="margin:0">
          {% if applied %}Applied (saved to DB){% else %}Preview (dry-run){% endif %}
        </div>
      </div>
      <div>
        <div class="meta-label">Entries changed{% if not applied %} (would change){% endif %}</div>
        <div class="subject-title" style="margin:0">{{ changed }}</div>
      </div>
      <div>
        <div class="meta-label">Warnings</div>
        <div class="subject-title" style="margin:0">{{ warnings_total }}</div>
      </div>
    </div>
    {% if limit %}
      <div class="small muted" style="margin-top:8px;">Preview limit: {{ limit }}</div>
    {% endif %}
  </div>

  <div class="card" style="padding:16px; margin-bottom:16px;">
    <div class="meta-label">Preview (first 100 changes)</div>
    <div class="small muted" style="margin:6px 0 10px;">
      Showing the first 100 entries that {% if applied %}were{% else %}would be{% endif %} changed.
    </div>
    <div style="overflow:auto;">
      <table class="table">
        <thead>
          <tr><th style="width:80px;">ID</th><th>Before</th><th>After</th></tr>
        </thead>
        <tbody>
          {% for row in preview %}
            <tr>
              <td class="small">{{ row.0 }}</td>
              <td><code>{{ row.1 }}</code></td>
              <td><code>{{ row.2 }}</code></td>
            </tr>
          {% empty %}
            <tr><td colspan="3" class="muted">No changes detected.</td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>

    <div class="result-toolbar" style="margin-top:12px;">
      <div class="rt-left">
        <a class="btn btn-secondary" href="{% url 'stats' %}">← Back to Statistics</a>
      </div>
      <div class="rt-right">
        {% if not applied and user.is_authenticated and user.is_staff %}
          <form method="post" action="{% url 'normalize_subjects' %}">
            {% csrf_token %}
            <button class="btn btn-danger"
              onclick="return confirm('Apply subject normalization to ALL entries? This will modify the subject field across the database. Make a backup first.')">
              Apply to all
            </button>
          </form>
        {% endif %}
      </div>
    </div>
  </div>
</div>
{% endblock %}