{% extends "base.html" %}

{% block title %}Admin Dashboard - {{ config.APP_NAME }}{% endblock %}

{% block content %}
<main class="container">
  <header style="display: flex; justify-content: space-between; align-items: center;">
    <div>
      <h1>Admin Dashboard</h1>
      {% if session.get('admin_impersonating') %}
      <mark>Currently impersonating a user</mark>
      <form method="post" action="{{ url_for('admin.stop_impersonating') }}" style="display: inline;">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <button type="submit" class="secondary outline" style="padding: 0.25rem 0.5rem;">Stop</button>
      </form>
      {% endif %}
    </div>
    <form method="post" action="{{ url_for('admin.logout') }}">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
      <button type="submit" class="secondary outline">Logout</button>
    </form>
  </header>
  
  <!-- Stats Grid -->
  <div class="grid">
    <article>
      <header><small>Total Users</small></header>
      <p style="font-size: 2rem; margin: 0;"><strong>{{ stats.users_total }}</strong></p>
      <small>+{{ stats.users_today }} today, +{{ stats.users_week }} this week</small>
    </article>
    
    <article>
      <header><small>Active Subscriptions</small></header>
      <p style="font-size: 2rem; margin: 0;"><strong>{{ stats.active_subscriptions }}</strong></p>
    </article>
    
    <article>
      <header><small>Task Queue</small></header>
      <p style="font-size: 2rem; margin: 0;"><strong>{{ stats.tasks_pending }}</strong> pending</p>
      {% if stats.tasks_failed > 0 %}
      <small style="color: var(--del-color);">{{ stats.tasks_failed }} failed</small>
      {% else %}
      <small style="color: var(--ins-color);">0 failed</small>
      {% endif %}
    </article>
  </div>
  
  <!-- Quick Links -->
  <div class="grid" style="margin-bottom: 2rem;">
    <a href="{{ url_for('admin.users') }}" role="button" class="secondary outline">All Users</a>
    <a href="{{ url_for('admin.tasks') }}" role="button" class="secondary outline">Task Queue</a>
    <a href="{{ url_for('dashboard.index') }}" role="button" class="secondary outline">View as User</a>
  </div>
  
  <div class="grid">
    <!-- Recent Users -->
    <section>
      <h2>Recent Users</h2>
      <article>
        {% if recent_users %}
        <table>
          <thead>
            <tr>
              <th>Email</th>
              <th>Plan</th>
              <th>Joined</th>
            </tr>
          </thead>
          <tbody>
            {% for u in recent_users %}
            <tr>
              <td>
                <a href="{{ url_for('admin.user_detail', user_id=u.id) }}">{{ u.email }}</a>
              </td>
              <td>{{ u.plan or 'free' }}</td>
              <td>{{ u.created_at[:10] }}</td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
        <a href="{{ url_for('admin.users') }}">View all →</a>
        {% else %}
        <p>No users yet.</p>
        {% endif %}
      </article>
    </section>
    
    <!-- Failed Tasks -->
    <section>
      <h2>Failed Tasks</h2>
      <article>
        {% if failed_tasks %}
        <table>
          <thead>
            <tr>
              <th>Task</th>
              <th>Error</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for task in failed_tasks[:5] %}
            <tr>
              <td>{{ task.task_name }}</td>
              <td><small>{{ task.error[:50] }}...</small></td>
              <td>
                <form method="post" action="{{ url_for('admin.task_retry', task_id=task.id) }}" style="margin: 0;">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                  <button type="submit" class="outline" style="padding: 0.25rem 0.5rem; margin: 0;">Retry</button>
                </form>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
        <a href="{{ url_for('admin.tasks') }}">View all →</a>
        {% else %}
        <p style="color: var(--ins-color);">✓ No failed tasks</p>
        {% endif %}
      </article>
    </section>
  </div>
</main>
{% endblock %}
