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

{% block title %}Article Templates — {{ config.APP_NAME }} Admin{% endblock %}
{% block topbar_title %}Article Templates{% endblock %}

{% block admin_content %}
  <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:20px;">
    <div>
      <h2 class="text-xl" style="margin:0;">Article Templates</h2>
      <p class="text-sm text-slate" style="margin:4px 0 0;">
        Jinja2 templates for programmatic article generation (pSEO).
      </p>
    </div>
    <div style="display:flex;gap:8px;">
      <a href="{{ url_for('cms.cms_index') }}" class="btn-outline btn-sm">Articles</a>
      <a href="{{ url_for('cms.template_new') }}" class="btn btn-sm">+ New Template</a>
    </div>
  </div>

  <div class="card" style="padding:0;overflow:hidden;">
    {% if templates %}
    <table class="table">
      <thead>
        <tr>
          <th>Template</th>
          <th>URL Pattern</th>
          <th>Data Rows</th>
          <th>Generated</th>
          <th>Created</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {% for tmpl in templates %}
        <tr>
          <td>
            <a href="{{ url_for('cms.template_data_view', template_id=tmpl.id) }}" style="font-weight:500;">
              {{ tmpl.name }}
            </a>
          </td>
          <td>
            <code class="mono text-sm" style="color:#64748B;">{{ tmpl.url_pattern }}</code>
          </td>
          <td class="mono text-sm">{{ tmpl.data_count }}</td>
          <td>
            <span class="text-sm">{{ tmpl.generated_count }}</span>
            {% if tmpl.generated_count < tmpl.data_count %}
            <span style="font-size:11px;color:#F59E0B;margin-left:4px;">
              {{ tmpl.data_count - tmpl.generated_count }} pending
            </span>
            {% endif %}
          </td>
          <td class="mono text-sm">{{ tmpl.created_at[:10] }}</td>
          <td>
            <div style="display:flex;gap:6px;">
              <a href="{{ url_for('cms.template_edit', template_id=tmpl.id) }}" class="btn-outline btn-sm">Edit</a>
              <form method="post" action="{{ url_for('cms.template_bulk_generate', template_id=tmpl.id) }}" class="m-0">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="btn btn-sm"
                        {% if tmpl.data_count == tmpl.generated_count %}disabled style="opacity:0.5;"{% endif %}>
                  Generate
                </button>
              </form>
            </div>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div style="padding:40px;text-align:center;">
      <p class="text-slate text-sm">No article templates yet.</p>
      <p class="text-slate text-sm" style="margin-top:8px;">
        Run <code class="mono">uv run python scripts/seed_cms_coffee.py</code> to seed coffee market templates.
      </p>
      <a href="{{ url_for('cms.template_new') }}" class="btn btn-sm" style="margin-top:12px;">Create template manually</a>
    </div>
    {% endif %}
  </div>
{% endblock %}
