{% extends "base.html" %}
{% block title %}Agenten{% endblock %}

{% block content %}
<div class="page-header">
  <h1>Agenten-Verwaltung</h1>
  <p>System-Prompts bearbeiten und verwalten</p>
</div>

{% if edit_agent %}
<div class="card">
  <div class="card-header bg-warning">
    <h5 class="mb-0">Prompt bearbeiten: {{ edit_agent }}</h5>
  </div>
  <div class="card-body">
    <form method="POST" action="{{ url_for('agents') }}">
      <input type="hidden" name="agent_name" value="{{ edit_agent }}">
      <div class="mb-3">
        <label for="prompt_content" class="form-label">System-Prompt</label>
        <textarea class="form-control font-monospace" id="prompt_content" name="prompt_content"
          rows="22" style="font-size:.82rem;line-height:1.5;">{{ edit_prompt }}</textarea>
      </div>
      <div class="d-flex gap-2">
        <button type="submit" class="btn btn-primary">Speichern</button>
        <a href="{{ url_for('agents') }}" class="btn btn-secondary">Abbrechen</a>
      </div>
    </form>
  </div>
</div>

{% else %}
<div class="card">
  <div class="card-header bg-dark d-flex justify-content-between align-items-center">
    <h5 class="mb-0">Alle Agenten</h5>
    <span class="badge bg-secondary">{{ agents_list|length }}</span>
  </div>
  <div class="card-body p-0">
    {% if agents_list %}
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead>
          <tr>
            <th>Agent</th>
            <th>Prompt-Vorschau</th>
            <th style="width:120px;">Aktionen</th>
          </tr>
        </thead>
        <tbody>
          {% for agent in agents_list %}
          <tr>
            <td>
              <strong>{{ agent.name }}</strong>
            </td>
            <td>
              <small style="color:var(--text-muted);">
                {{ agent.prompt[:160] }}{% if agent.prompt|length > 160 %}…{% endif %}
              </small>
            </td>
            <td>
              <a href="{{ url_for('agents', edit=agent.name) }}" class="btn btn-sm btn-outline-primary">Bearbeiten</a>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% else %}
    <div class="alert alert-warning m-3">
      Keine Agenten gefunden. Stelle sicher, dass <code>agents/</code> Unterverzeichnisse mit <code>systemprompt.md</code> enthält.
    </div>
    {% endif %}
  </div>
</div>
{% endif %}
{% endblock %}
