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

{% block title %}Retrieval Wissensbasis{% endblock %}

{% block body %}

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3"><i class="bi bi-robot"></i> Retrieval Wissensbasis</h1>

        <a href="{{ path('admin_dashboard') }}"
           class="btn btn-sm btn-outline-secondary">
            Zurück zum Dashboard
        </a>
    </div>

    {# ============================= #}
    {# Index Meta Section           #}
    {# ============================= #}

    <div class="card bg-dark border-secondary mb-5">
        <div class="card-body">

            <h5 class="text-info mb-3">Generierungsmatrix (index_meta.json)</h5>

            {% if meta.error is defined %}
                <div class="alert alert-danger">
                    <strong>Fehler:</strong><br>
                    {{ meta.error }}<br>
                    <small class="text-muted">{{ meta.path }}</small>
                </div>
            {% else %}

                <div class="table-responsive">
                    <table class="table table-dark table-striped align-middle mb-0">
                        <tbody>
                        {% for key, value in meta %}
                            <tr>
                                <td style="width:260px;" class="text-info">
                                    {{ key }}
                                </td>
                                <td>
                                    {% if value is iterable %}
                                        <pre class="mb-0 small text-light">
{{ value|json_encode(constant('JSON_PRETTY_PRINT')) }}
                                    </pre>
                                    {% else %}
                                        {{ value }}
                                    {% endif %}
                                </td>
                            </tr>
                        {% else %}
                            <tr>
                                <td colspan="2" class="text-secondary">
                                    Keine Meta-Daten vorhanden.
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>

            {% endif %}
        </div>
    </div>

    {# ============================= #}
    {# NDJSON Section               #}
    {# ============================= #}

    {% set currentPage = ndjson.page|default(1) %}
    {% set currentLimit = ndjson.limit|default(50) %}

    <div class="card bg-black border-secondary">
        <div class="card-body">

            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="text-info mb-0">
                    Wissensdaten Chunks | Übersicht (index.ndjson)
                </h5>

                <div class="btn-group">
                    <a href="{{ path('admin_system_agent', {
                        page: currentPage > 1 ? currentPage - 1 : 1,
                        limit: currentLimit
                    }) }}"
                       class="btn btn-sm btn-outline-info">
                        ← Zurück
                    </a>

                    <a href="{{ path('admin_system_agent', {
                        page: currentPage + 1,
                        limit: currentLimit
                    }) }}"
                       class="btn btn-sm btn-outline-info">
                        Weiter →
                    </a>
                </div>
            </div>

            {% if ndjson.error %}
                <div class="alert alert-danger">
                    <strong>Fehler:</strong><br>
                    {{ ndjson.error }}<br>
                    <small>{{ ndjson.path|default('-') }}</small>
                </div>
            {% endif %}

            <div class="mb-3 small text-secondary">
                Datei:
                {% if ndjson.path %}
                    <span class="badge bg-success">Vorhanden</span>
                {% else %}
                    <span class="badge bg-danger">Nicht gefunden</span>
                {% endif %}

                • Geladene Einträge: {{ debugCount|default(0) }}
                • Seite {{ currentPage }}
                • Limit {{ currentLimit }}
            </div>

            <div class="table-responsive">
                <table class="table table-dark table-striped table-hover align-middle">
                    <thead class="table-secondary text-dark">
                    <tr>
                        <th style="width:200px;">chunk_id</th>
                        <th style="width:180px;">document_id</th>
                        <th>Text (gekürzt)</th>
                    </tr>
                    </thead>
                    <tbody>

                    {% for item in ndjson.items|default([]) %}
                        <tr>
                            <td class="small text-info">{{ item.chunk_id ?? '-' }}</td>

                            <td class="small text-info">
                                {% if item.document_id %}
                                    {{ item.document_id }}<br>
                                    <a href="{{ path('admin_document_show', {id: item.document_id}) }}"
                                       class="text-light small">
                                        ansehen
                                    </a>
                                {% else %}
                                    -
                                {% endif %}
                            </td>

                            <td>

                                {% set text = item.text ?? '' %}
                                <div class="small">
                                    {{ text|slice(0, 240) }}
                                    {% if text|length > 240 %}…{% endif %}
                                </div>

                                <details class="mt-2 position-relative">
                                    <summary class="text-secondary small">
                                        JSON anzeigen
                                    </summary>

                                    <pre class="bg-dark text-info p-2 border border-secondary rounded mt-2 small position-absolute top-25 z-1 w-100">
{{ item|json_encode(constant('JSON_PRETTY_PRINT')) }}
                                </pre>
                                </details>

                            </td>
                        </tr>

                    {% else %}
                        <tr>
                            <td colspan="3" class="text-center text-secondary py-4">
                                Keine Einträge gefunden.
                            </td>
                        </tr>
                    {% endfor %}

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

        </div>
    </div>

    <div class="mt-4 small text-secondary">
        Hinweis: Änderungen am NDJSON-Index oder an der Indexstruktur können
        inkonsistente Retrieval-Ergebnisse verursachen, bis ein vollständiger
        Reindex durchgeführt wurde.
    </div>

{% endblock %}
