{% load i18n %}
{% load humanize %}
{% load aa_customizer_tags %}

{% superuser_branding as aac_sb %}

{# Inject superuser-only CSS. <style>/<link> in <body> is valid HTML5. #}
{% if aac_sb.superuser_dashboard_css_url %}
    <link rel="stylesheet" href="{{ aac_sb.superuser_dashboard_css_url }}">
{% endif %}
{% if aac_sb.superuser_dashboard_css %}
    <style>{{ aac_sb.superuser_dashboard_css|safe }}</style>
{% endif %}
{% if aac_sb.superuser_dashboard_head_html %}{{ aac_sb.superuser_dashboard_head_html|safe }}{% endif %}

{% get_current_language as LANGUAGE_CODE %}

{% comment %}
Some translations used in the HTML and JavaScript code below.
We define them here so that they can be used in the JavaScript code as well with
the escapejs filter without having to redefine them later.
{% endcomment %}
{% translate "second" as l10nSecondSingular %}
{% translate "seconds" as l10nSecondPlural %}
{% translate "minute" as l10nMinuteSingular %}
{% translate "minutes" as l10nMinutePlural %}
{% translate "hour" as l10nHourSingular %}
{% translate "hours" as l10nHourPlural %}
{% translate "N/A" as l10nNA %}
{% translate "ERROR" as l10nError %}
{% translate "running" as l10nRunning %}
{% translate "queued" as l10nQueued %}
{% translate "succeeded" as l10nSucceeded %}
{% translate "retried" as l10nRetried %}
{% translate "failed" as l10nFailed %}

{% if debug %}
    <div id="aa-dashboard-panel-debug" class="col-12 mb-3">
        <div class="card text-bg-warning">
            <div class="card-body">
                {% translate "Debug mode" as widget_title %}
                {% include "framework/dashboard/widget-title.html" with title=widget_title %}

                <div>
                    <p class="text-center">
                        {% translate "Debug mode is currently turned on!<br>Make sure to turn it off as soon as you are finished testing." %}
                    </p>
                </div>
            </div>
        </div>
    </div>
{% endif %}

{% if notifications %}
    <div id="aa-dashboard-panel-admin-application-notifications" class="col-12 mb-3">
        <div class="card">
            <div class="card-body">
                {% translate "Announcements" as widget_title %}
                {% include "framework/dashboard/widget-title.html" with title=widget_title %}

                <div>
                    <ul class="list-group">
                        {% for notif in notifications %}
                            <li class="list-group-item">
                                <span class="badge text-bg-success me-2">{% translate "Open" %}</span>
                                <a href="{{ notif.announcement_url }}" target="_blank">#{{ notif.announcement_number }} {{ notif.announcement_text }}</a>
                            </li>
                        {% empty %}
                            <div class="alert alert-primary" role="alert">
                                {% translate "No notifications at this time" %}
                            </div>
                        {% endfor %}
                    </ul>

                    <div class="text-end pt-3">
                        <a href="https://gitlab.com/allianceauth/allianceauth/issues" target="_blank" class="me-1 text-decoration-none">
                            <span class="badge text-bg-danger">
                                <i class="fab fa-gitlab" aria-hidden="true"></i>
                                {% translate 'Powered by GitLab' %}
                            </span>
                        </a>
                        <a href="https://discord.com/invite/fjnHAmk" target="_blank" class="text-decoration-none">
                            <span class="badge text-bg-info">
                                <i class="fab fa-discord" aria-hidden="true"></i>
                                {% translate 'Support Discord' %}
                            </span>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endif %}

<div class="col-12 mb-3">
    <div class="card">
        <div class="card-body row">
            <div id="aa-dashboard-panel-software-version" class="col-xl-6 col-lg-12 col-md-12 col-sm-12 mb-3 mb-xl-0">
                {% translate "Software Version" as widget_title %}
                {% include "framework/dashboard/widget-title.html" with title=widget_title %}

                <div>
                    <ul class="list-group list-group-horizontal w-100" role="group" aria-label="{% translate 'Software Version' %}">
                        <li class="list-group-item w-100">
                            <div class="btn h-100 w-100 cursor-default text-center">
                                <h6 class="list-group-item-heading">{% translate "Current" %}</h6>
                                <p class="list-group-item-text">{{ current_version }}</p>
                            </div>
                        </li>

                        <li class="list-group-item text-bg-{% if latest_patch %}success{% elif latest_minor %}warning{% else %}danger{% endif %} w-100">
                            <a class="btn h-100 w-100 text-center" href="https://gitlab.com/allianceauth/allianceauth/-/releases/v{{ latest_patch_version }}">
                                <h6 class="list-group-item-heading">{% translate "Latest Stable" %}</h6>

                                <p class="list-group-item-text">
                                    <i class="fab fa-gitlab hidden-xs d-none d-sm-inline" aria-hidden="true"></i>
                                    {{ latest_patch_version }}
                                    {% if not latest_patch %}<br>{% translate "Update available" %}{% endif %}
                                </p>
                            </a>
                        </li>

                        {% if latest_beta %}
                            <li class="list-group-item text-bg-info w-100">
                                <a class="btn h-100 w-100 text-center" href="https://gitlab.com/allianceauth/allianceauth/-/releases/v{{ latest_beta_version }}">
                                    <h6 class="list-group-item-heading">{% translate "Latest Pre-Release" %}</h6>

                                    <p class="list-group-item-text">
                                        <i class="fab fa-gitlab hidden-xs d-none d-sm-inline" aria-hidden="true"></i>
                                        {{ latest_beta_version }}
                                        <br>{% translate "Pre-Release available" %}
                                    </p>
                                </a>
                            </li>
                        {% endif %}
                    </ul>
                </div>
            </div>

            <div id="aa-dashboard-panel-task-queue" class="col-xl-6 col-lg-12 col-md-12 col-sm-12">
                {% translate "Task Queue" as widget_title %}
                {% include "framework/dashboard/widget-title.html" with title=widget_title %}

                <div>
                    <p>
                        {% blocktranslate with total=tasks_total|intcomma latest=earliest_task|timesince|default:"?" %}
                            Status of <span id="total-task-count">{{ total }}</span> processed tasks • last <span id="celery-uptime">{{ latest }}</span>
                        {% endblocktranslate %}
                    </p>

                    <div
                        id="celery-tasks-progress-bar"
                        class="progress mb-2"
                        style="height: 12px;"
                        title="Pending data update…"
                    >
                        {% include "admin_status/celery_bar_partial.html" with label="succeeded" level="success" tasks_count=tasks_succeeded %}
                        {% include "admin_status/celery_bar_partial.html" with label="retried" level="info" tasks_count=tasks_retried %}
                        {% include "admin_status/celery_bar_partial.html" with label="failed" level="danger" tasks_count=tasks_failed %}
                    </div>

                    <p>
                        <span id="running-task-count">?</span> {{ l10nRunning }} |
                        <span id="queued-tasks-count">?</span> {{ l10nQueued }} |
                        <span id="succeeded-tasks-count">{{ tasks_succeeded|intcomma }}</span> {{ l10nSucceeded }} |
                        <span id="retried-tasks-count">{{ tasks_retried|intcomma }}</span> {{ l10nRetried }} |
                        <span id="failed-tasks-count">{{ tasks_failed|intcomma }}</span> {{ l10nFailed }}
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    const taskQueueSettings = {
        url: '{% url "authentication:task_counts" %}',
        l10n: {
            language: '{{ LANGUAGE_CODE }}',
            second_singular: '{{ l10nSecondSingular|escapejs }}',
            second_plural: '{{ l10nSecondPlural|escapejs }}',
            minute_singular: '{{ l10nMinuteSingular|escapejs }}',
            minute_plural: '{{ l10nMinutePlural|escapejs }}',
            hour_singular: '{{ l10nHourSingular|escapejs }}',
            hour_plural: '{{ l10nHourPlural|escapejs }}',
            na: '{{ l10nNA|escapejs }}',
            error: '{{ l10nError|escapejs }}',
            running: '{{ l10nRunning|escapejs }}',
            queued: '{{ l10nQueued|escapejs }}',
            succeeded: '{{ l10nSucceeded|escapejs }}',
            retried: '{{ l10nRetried|escapejs }}',
            failed: '{{ l10nFailed|escapejs }}'
        }
    };
</script>
{% include "bundles/auth-dashboard-task-queue-js.html" %}

{% if aac_sb.superuser_dashboard_body_html %}{{ aac_sb.superuser_dashboard_body_html|safe }}{% endif %}
