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

{% block title %}System-Dashboard{% endblock %}

{% block body %}
    <div class="container-fluid">

        {% set chunkStatus = vectorHealth.status|default('UNKNOWN') %}
        {% set chunkBadgeClass =
            chunkStatus starts with 'OK'
            ? 'bg-success'
            : (chunkStatus == 'INCONSISTENT_MISSING_VECTOR'
            ? 'bg-warning text-dark'
            : 'bg-danger')
        %}

        {% set tagStatus = tagVectorHealth.status|default('UNKNOWN') %}
        {% set tagBadgeClass =
            tagStatus starts with 'OK'
            ? 'bg-success'
            : (tagStatus == 'INCONSISTENT_MISSING_VECTOR'
            ? 'bg-warning text-dark'
            : 'bg-danger')
        %}

        {% set percent = chunkLimit > 0 ? (chunkCount / chunkLimit * 100)|round(3) : 0 %}
        {% set percentClass =
            percent >= 95
            ? 'bg-danger'
            : (percent >= 85 ? 'bg-warning text-dark' : 'bg-success')
        %}

        {% set chunkHealthy = chunkStatus in ['OK', 'OK_EMPTY'] %}
        {% set tagHealthy = tagStatus in ['OK', 'OK_EMPTY'] %}
        {% set anyHealthIssue = not chunkHealthy or not tagHealthy %}

        <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
            <h1 class="h3 mb-0">
                <i class="bi bi-hdd-rack"></i> Systemübersicht
            </h1>
            <span class="badge bg-secondary">RetrieX Admin</span>
        </div>

        {% if anyHealthIssue %}
            <div class="alert alert-warning shadow-sm mb-4">
                <strong>Achtung:</strong>
                Mindestens ein Index-Zustand ist nicht konsistent.
                Prüfe die Detailkarten unten und führe bei Bedarf einen Global Reindex aus.
            </div>
        {% endif %}

        <div class="row g-4 mb-4">

            <div class="col-lg-6 col-xl-3">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <div class="small text-light mb-2">
                            <i class="bi bi-files"></i> Chunk-Vektor
                        </div>

                        <h4 class="mb-2">
                            <span class="badge {{ chunkBadgeClass }}">
                                {{ chunkStatus }}
                            </span>
                        </h4>

                        <div class="small text-muted">
                            Keyword-/Chunk-Retrieval-Grundlage des Systems
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-6 col-xl-3">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <div class="small text-light mb-2">
                            <i class="bi bi-tags"></i> Tag-Vektor
                        </div>

                        <h4 class="mb-2">
                            <span class="badge {{ tagBadgeClass }}">
                                {{ tagStatus }}
                            </span>
                        </h4>

                        <div class="small text-muted">
                            Semantisches Tag-Routing für Dokumenträume und Entity-Erkennung
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-6 col-xl-3">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <div class="small text-light mb-2">
                            <i class="bi bi-robot"></i> Wissenskapazität
                        </div>

                        <h4 class="mb-2">
                            {{ chunkCount|number_format(0, ',', '.') }}
                            <span class="text-secondary small">
                                / {{ chunkLimit|number_format(0, ',', '.') }} Chunks
                            </span>
                        </h4>

                        <div class="progress bg-dark mb-2" style="height: 14px;">
                            <div class="progress-bar {{ percentClass }}"
                                 style="width: {{ percent }}%;">
                            </div>
                        </div>

                        <div class="small text-light">
                            {{ percent }} % ausgelastet
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-6 col-xl-3">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <div class="small text-light mb-2">
                            <i class="bi bi-shield-check"></i> System-Governance
                        </div>

                        <div class="small">
                            <strong>Benutzer</strong><br>
                            {{ app.user ? app.user.userIdentifier : '-' }}
                        </div>

                        <div class="small mt-3">
                            <strong>Rollen</strong><br>
                            {{ app.user ? app.user.roles|join(', ') : '-' }}
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <div class="row g-4">

            <div class="col-lg-4">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <h5 class="text-info mb-3">
                            <i class="bi bi-files"></i> Chunk-Vektor-Details
                        </h5>

                        <div class="small text-info">NDJSON-Chunks</div>
                        <div class="h5 mb-3">
                            {{ vectorHealth.ndjson_chunk_count|default(0)|number_format(0, ',', '.') }}
                        </div>

                        <div class="small text-info">Vektor-Index-Chunks</div>
                        <div class="h5 mb-3">
                            {{ vectorHealth.vector_chunk_count|default(0)|number_format(0, ',', '.') }}
                        </div>

                        <div class="d-flex flex-wrap gap-2 mt-3">
                            <span class="badge {{ vectorHealth.ndjson_exists|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                NDJSON {{ vectorHealth.ndjson_exists|default(false) ? 'vorhanden' : 'fehlt' }}
                            </span>
                            <span class="badge {{ vectorHealth.vector_exists|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                Index {{ vectorHealth.vector_exists|default(false) ? 'vorhanden' : 'fehlt' }}
                            </span>
                            <span class="badge {{ vectorHealth.meta_exists|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                Meta {{ vectorHealth.meta_exists|default(false) ? 'vorhanden' : 'fehlt' }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-4">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <h5 class="text-info mb-3">
                            <i class="bi bi-tags"></i> Tag-Vektor-Details
                        </h5>

                        <div class="small text-info">Exportierte Tags (NDJSON)</div>
                        <div class="h5 mb-3">
                            {{ tagVectorHealth.tags_ndjson_count|default(0)|number_format(0, ',', '.') }}
                        </div>

                        <div class="small text-info">Vektor-Index-Tags</div>
                        <div class="h5 mb-3">
                            {{ tagVectorHealth.vector_tag_count|default(0)|number_format(0, ',', '.') }}
                        </div>

                        <div class="small text-info">Tags mit aktiven Dokumenten</div>
                        <div class="h5 mb-3">
                            {{ tagVectorHealth.tags_with_active_document_ids|default(0)|number_format(0, ',', '.') }}
                        </div>

                        <div class="d-flex flex-wrap gap-2 mt-3">
                            <span class="badge {{ tagVectorHealth.tags_ndjson_exists|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                NDJSON {{ tagVectorHealth.tags_ndjson_exists|default(false) ? 'vorhanden' : 'fehlt' }}
                            </span>
                            <span class="badge {{ tagVectorHealth.vector_exists|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                Index {{ tagVectorHealth.vector_exists|default(false) ? 'vorhanden' : 'fehlt' }}
                            </span>
                            <span class="badge {{ tagVectorHealth.meta_exists|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                Meta {{ tagVectorHealth.meta_exists|default(false) ? 'vorhanden' : 'fehlt' }}
                            </span>
                            <span class="badge {{ tagVectorHealth.meta_valid|default(false) ? 'text-bg-success' : 'text-bg-danger' }}">
                                Meta {{ tagVectorHealth.meta_valid|default(false) ? 'gültig' : 'ungültig' }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-4">
                <div class="card bg-black border-secondary text-light h-100 shadow-sm">
                    <div class="card-body">
                        <h5 class="text-info mb-3">
                            <i class="bi bi-search"></i> Indexierung (Ingest Jobs)
                        </h5>

                        <div class="text-muted small mb-3">
                            Erstellt den kompletten Wissensindex neu und zieht dabei auch die
                            physischen Retrieval-Artefakte wieder gerade.
                        </div>

                        <form method="post"
                              action="{{ path('admin_global_reindex') }}"
                              onsubmit="return confirm('Global Reindex starten? Dies kann mehrere Minuten dauern.');">

                            <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>

                        {% if anyHealthIssue %}
                            <div class="alert alert-dark border border-warning text-light small mt-3 mb-0">
                                Empfohlen bei inkonsistentem Chunk- oder Tag-Zustand.
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            {% if is_granted('ROLE_SUPER_ADMIN') %}
                <div class="col-lg-4">
                    <div class="card bg-black border-danger text-light h-100 shadow-sm">
                        <div class="card-body">
                            <h5 class="text-danger mb-3">
                                <i class="bi bi-sign-stop-fill"></i> Kritische Systemoperationen
                            </h5>

                            <div class="small mb-3 text-secondary">
                                Entfernt alle Dokumente, Versionen, Indizes und Jobs.
                                <br><strong>Nicht rückgängig zu machen.</strong>
                            </div>

                            <form method="post"
                                  action="{{ path('admin_document_reset') }}"
                                  onsubmit="return confirm('System vollständig zurücksetzen?');">

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

                                <button type="submit"
                                        class="btn btn-outline-danger">
                                    Vollständigen System-Reset ausführen
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            {% endif %}

        </div>

    </div>
{% endblock %}