{% set admin_page = "cms" %}
{% extends "admin/base_admin.html" %}

{% block title %}{{ template.name }} Data — Admin{% endblock %}
{% block topbar_title %}{{ template.name }}{% endblock %}

{% block admin_content %}
  <div style="margin-bottom:16px;">
    <a href="{{ url_for('cms.template_list') }}" style="font-size:13px;color:#64748B;text-decoration:none;">
      ← Templates
    </a>
  </div>

  <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:20px;">
    <div>
      <h2 class="text-xl" style="margin:0;">{{ template.name }}</h2>
      <p class="text-sm text-slate" style="margin:4px 0 0;">
        URL: <code class="mono">{{ template.url_pattern }}</code> ·
        {{ data_rows | length }} rows ·
        {{ data_rows | selectattr('article_id') | list | length }} generated
      </p>
    </div>
    <div style="display:flex;gap:8px;">
      <a href="{{ url_for('cms.template_edit', template_id=template.id) }}" class="btn-outline btn-sm">Edit Template</a>
      <form method="post" action="{{ url_for('cms.template_bulk_generate', template_id=template.id) }}" class="m-0">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <button type="submit" class="btn btn-sm">Generate Pending</button>
      </form>
      <form method="post" action="{{ url_for('cms.template_regenerate_all', template_id=template.id) }}" class="m-0"
            onsubmit="return confirm('Regenerate all {{ data_rows | length }} articles?')">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <button type="submit" class="btn-outline btn-sm">Regenerate All</button>
      </form>
    </div>
  </div>

  <!-- Add data row -->
  <div class="card" style="padding:16px;margin-bottom:16px;">
    <p class="text-sm" style="font-weight:600;margin:0 0 8px;">Add data row</p>
    <form method="post" action="{{ url_for('cms.template_data_add', template_id=template.id) }}"
          style="display:flex;gap:10px;align-items:flex-end;flex-wrap:wrap;">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
      <div style="flex:1;min-width:300px;">
        <label class="text-sm" style="display:block;margin-bottom:4px;color:#475569;">JSON data</label>
        <input type="text" name="data_json" placeholder='{"country_name": "Brazil", "country_slug": "brazil"}' required
               style="width:100%;box-sizing:border-box;border:1px solid #CBD5E1;border-radius:6px;padding:6px 10px;font-size:13px;font-family:monospace;">
      </div>
      <button type="submit" class="btn btn-sm">Add</button>
    </form>
  </div>

  <!-- Data rows table -->
  <div class="card" style="padding:0;overflow:hidden;">
    {% if data_rows %}
    <table class="table" style="font-size:12px;">
      <thead>
        <tr>
          <th>#</th>
          <th>Data</th>
          <th>Article</th>
          <th>Added</th>
        </tr>
      </thead>
      <tbody>
        {% for row in data_rows %}
        <tr>
          <td class="mono text-sm" style="color:#94A3B8;">{{ row.id }}</td>
          <td>
            <code style="font-size:11px;color:#475569;max-width:400px;display:block;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"
                  title="{{ row.data_json }}">{{ row.data_json }}</code>
          </td>
          <td>
            {% if row.article_id %}
            <span class="badge-success">generated #{{ row.article_id }}</span>
            {% else %}
            <span style="font-size:11px;color:#F59E0B;">pending</span>
            {% endif %}
          </td>
          <td class="mono text-sm">{{ row.created_at[:10] }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div style="padding:40px;text-align:center;">
      <p class="text-slate text-sm">No data rows yet.</p>
      <p class="text-slate text-sm" style="margin-top:6px;">
        Run <code class="mono">uv run python scripts/seed_cms_coffee.py</code> to populate from DuckDB.
      </p>
    </div>
    {% endif %}
  </div>
{% endblock %}
