{% extends 'admin/base.html.twig' %}

{% block title %}Indexierung (Ingest Jobs){% endblock %}

{% block body %}

    {% set latestJob = jobs is not empty ? jobs|first : null %}

    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <div>
            <h1 class="h3 mb-1">
                <i class="bi bi-terminal"></i> Indexierung (Ingest Jobs)
            </h1>
            <div class="small text-muted">
                Übersicht über Reindex-, Dokument- und Aktivierungsjobs des Systems.
            </div>
        </div>

        {% if is_granted('ROLE_SUPER_ADMIN') %}
            <form method="post"
                  action="{{ path('admin_global_reindex') }}"
                  onsubmit="return confirm('Global Reindex starten? Dies kann je nach Datenmenge mehrere Minuten dauern.');"
                  class="mb-0">

                <input type="hidden"
                       name="_token"
                       value="{{ csrf_token('global_reindex') }}">

                <button type="submit"
                        class="btn btn-sm btn-outline-info">
                    Global Reindex starten
                </button>
            </form>
        {% endif %}
    </div>

    <div class="card bg-dark border-secondary text-light mb-4 shadow-sm">
        <div class="card-body row g-4">
            <div class="col-lg-7">
                <h5 class="text-info mb-3">Was sieht man hier?</h5>
                <ul class="small mb-0">
                    <li><strong>DOCUMENT</strong> verarbeitet ein einzelnes Dokument neu.</li>
                    <li><strong>DOCUMENT_VERSION_ACTIVATE</strong> zieht eine aktivierte Version deterministisch neu in den Index.</li>
                    <li><strong>DOCUMENT_DELETE</strong> entfernt Dokumentinhalt wieder sauber aus den Index-Artefakten.</li>
                    <li><strong>GLOBAL_REINDEX</strong> baut den Wissensindex vollständig neu auf und ist der stärkste Reparaturpfad.</li>
                </ul>
            </div>

            <div class="col-lg-5">
                <h5 class="text-info mb-3">Worauf achten?</h5>
                <ul class="small mb-0">
                    <li><strong>RUNNING</strong> und <strong>QUEUED</strong> bedeuten: keine unnötigen parallelen Rebuilds starten.</li>
                    <li><strong>FAILED</strong> oder <strong>ABORTED</strong> direkt prüfen.</li>
                    <li>Bei inkonsistentem Indexzustand ist meist ein <strong>Global Reindex</strong> der richtige Reparaturschritt.</li>
                </ul>
            </div>
        </div>
    </div>

    {% if latestJob %}
        <div class="card bg-black border-secondary text-light mb-4 shadow-sm">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start flex-wrap gap-3">
                    <div>
                        <div class="small text-muted mb-1">Letzter Job</div>
                        <div class="fw-semibold">
                            <a href="{{ path('admin_job_show', {id: latestJob.id}) }}"
                               class="text-light text-decoration-none">
                                {{ latestJob.id }}
                            </a>
                        </div>
                    </div>

                    <div>
                        <div class="small text-muted mb-1">Typ</div>
                        <span class="badge bg-info text-dark">{{ latestJob.type }}</span>
                    </div>

                    <div>
                        <div class="small text-muted mb-1">Status</div>
                        {% if latestJob.status == 'COMPLETED' %}
                            <span class="badge bg-success">COMPLETED</span>
                        {% elseif latestJob.status == 'QUEUED' %}
                            <span class="badge bg-secondary">QUEUED</span>
                        {% elseif latestJob.status == 'RUNNING' %}
                            <span class="badge bg-warning text-dark">RUNNING</span>
                        {% elseif latestJob.status == 'FAILED' %}
                            <span class="badge bg-danger">FAILED</span>
                        {% elseif latestJob.status == 'ABORTED' %}
                            <span class="badge bg-dark border border-danger text-danger">ABORTED</span>
                        {% else %}
                            <span class="badge bg-dark border border-secondary">{{ latestJob.status }}</span>
                        {% endif %}
                    </div>

                    <div>
                        <div class="small text-muted mb-1">Gestartet</div>
                        <div class="small">
                            {{ latestJob.startedAt ? latestJob.startedAt|date('d.m.Y H:i:s') : '-' }}
                        </div>
                    </div>

                    <div>
                        <div class="small text-muted mb-1">Beendet</div>
                        <div class="small">
                            {{ latestJob.finishedAt ? latestJob.finishedAt|date('d.m.Y H:i:s') : 'läuft noch / offen' }}
                        </div>
                    </div>
                </div>

                {% if latestJob.errorMessage %}
                    <div class="alert alert-danger small mt-3 mb-0">
                        <strong>Fehler:</strong>
                        {{ latestJob.errorMessage|slice(0, 250) }}{% if latestJob.errorMessage|length > 250 %}…{% endif %}
                    </div>
                {% endif %}
            </div>
        </div>
    {% endif %}

    {% if jobs is empty %}

        <div class="alert alert-secondary shadow-sm">
            Keine Ingest Jobs vorhanden.
        </div>

    {% else %}

        <div class="card bg-black border-secondary shadow-sm">
            <div class="card-body p-0">

                <div class="d-flex justify-content-between align-items-center px-3 py-3 border-bottom border-secondary flex-wrap gap-2">
                    <div>
                        <strong class="text-info">Vorhandene Jobs</strong>
                        <span class="small text-muted ms-2">{{ jobs|length }} Einträge</span>
                    </div>

                    <div class="small text-muted">
                        Neueste Jobs stehen oben.
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-dark table-striped table-hover align-middle mb-0">
                        <thead class="table-secondary text-dark">
                        <tr>
                            <th style="width: 18%">Job</th>
                            <th style="width: 14%">Typ</th>
                            <th style="width: 12%">Status</th>
                            <th style="width: 18%">Bezug</th>
                            <th style="width: 12%">Gestartet</th>
                            <th style="width: 12%">Beendet</th>
                            <th style="width: 14%">Benutzer</th>
                        </tr>
                        </thead>
                        <tbody>

                        {% for job in jobs %}
                            <tr>
                                <td class="small">
                                    <div class="fw-semibold">
                                        <a href="{{ path('admin_job_show', {id: job.id}) }}"
                                           class="text-light text-decoration-none">
                                            {{ job.id }}
                                        </a>
                                    </div>

                                    {% if job.errorMessage %}
                                        <div class="text-danger small mt-1"
                                             title="{{ job.errorMessage }}">
                                            {{ job.errorMessage|slice(0, 120) }}{% if job.errorMessage|length > 120 %}…{% endif %}
                                        </div>
                                    {% endif %}
                                </td>

                                <td>
                                    <span class="badge bg-info text-dark">
                                        {{ job.type }}
                                    </span>
                                </td>

                                <td>
                                    {% if job.status == 'COMPLETED' %}
                                        <span class="badge bg-success">COMPLETED</span>
                                    {% elseif job.status == 'QUEUED' %}
                                        <span class="badge bg-secondary">QUEUED</span>
                                    {% elseif job.status == 'RUNNING' %}
                                        <span class="badge bg-warning text-dark">RUNNING</span>
                                    {% elseif job.status == 'FAILED' %}
                                        <span class="badge bg-danger">FAILED</span>
                                    {% elseif job.status == 'ABORTED' %}
                                        <span class="badge bg-dark border border-danger text-danger">ABORTED</span>
                                    {% else %}
                                        <span class="badge bg-dark border border-secondary">
                                            {{ job.status }}
                                        </span>
                                    {% endif %}
                                </td>

                                <td class="small">
                                    {% if job.documentId %}
                                        <div>
                                            <span class="text-muted">Dokument:</span>
                                            <a href="{{ path('admin_document_show', {id: job.documentId}) }}"
                                               class="text-light text-decoration-none">
                                                {{ job.documentId }}
                                            </a>
                                        </div>
                                    {% endif %}

                                    {% if job.documentVersionId %}
                                        <div class="mt-1">
                                            <span class="text-muted">Version:</span>
                                            {{ job.documentVersionId }}
                                        </div>
                                    {% endif %}

                                    {% if not job.documentId and not job.documentVersionId %}
                                        -
                                    {% endif %}
                                </td>

                                <td class="small">
                                    {{ job.startedAt ? job.startedAt|date('d.m.Y H:i:s') : '-' }}
                                </td>

                                <td class="small">
                                    {{ job.finishedAt ? job.finishedAt|date('d.m.Y H:i:s') : 'offen' }}
                                </td>

                                <td class="small">
                                    {{ job.startedBy ? job.startedBy.email : '-' }}
                                </td>
                            </tr>
                        {% else %}
                            <tr>
                                <td colspan="7" class="text-center text-secondary py-4">
                                    Keine Jobs gefunden.
                                </td>
                            </tr>
                        {% endfor %}

                        </tbody>
                    </table>
                </div>

            </div>
        </div>

    {% endif %}

    <div class="mt-4 small text-secondary">
        Hinweis: Während laufender Jobs (Status <strong>RUNNING</strong>) oder wartender Jobs (<strong>QUEUED</strong>)
        sollten keine unnötigen parallelen Reindex-Prozesse gestartet werden.
    </div>

{% endblock %}