{% extends "admin/base_site.html" %}
{% load i18n %}

{% block breadcrumbs %}
<div class="breadcrumbs">
  <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
  &rsaquo; <a href="{% url 'admin:system' %}">{% trans 'System' %}</a>
  &rsaquo; {% trans "Upgrade Report" %}
</div>
{% endblock %}

{% block extrastyle %}
{{ block.super }}
<style>
  .upgrade-report .upgrade-report-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
  }

  .upgrade-report .upgrade-report-header h2 {
    margin: 0;
  }

  .upgrade-report .upgrade-report-trigger {
    margin: 0;
  }

  .upgrade-report .upgrade-report-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    gap: 0.5rem;
  }

  .upgrade-report .upgrade-action-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1.25rem;
    gap: 0.375rem;
    min-height: 2.5rem;
    flex: 1 1 8rem;
  }

  .upgrade-report .upgrade-report-check {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0.25rem 0 0;
    flex-wrap: wrap;
  }

  .upgrade-report .upgrade-report-check .button {
    width: auto;
  }

  .upgrade-report .upgrade-report-check .help {
    margin: 0;
  }

  .upgrade-report .revision-refresh-button {
    flex: 0 0 auto;
    padding: 0.35rem;
    width: 2.25rem;
    height: 2.25rem;
    min-height: 2.25rem;
    border-radius: 999px;
  }

  .upgrade-report .revision-refresh-icon {
    width: 1.1rem;
    height: 1.1rem;
  }
</style>
{% endblock %}

{% block content %}
<div id="content-main">
  <div class="module upgrade-report">
    <div class="upgrade-report-header">
      <h2>{% trans "Upgrade Report" %}</h2>
      <form method="post" action="{% url 'admin:system-upgrade-run-check' %}" class="upgrade-report-trigger">
        {% csrf_token %}
        {% if report.settings.is_stable_mode %}
        {% blocktrans trimmed asvar unstable_confirm_message %}
          This node is configured for stable updates. Continue with an unstable upgrade?
        {% endblocktrans %}
        {% endif %}
        <div class="upgrade-report-actions">
          <button
            type="submit"
            name="channel"
            value="unstable"
            class="button default upgrade-action-button"
            {% if report.settings.is_stable_mode %}
            onclick="return confirm('{{ unstable_confirm_message|escapejs }}');"
            {% endif %}
          >
            {% trans "Upgrade Immediately (Unstable)" %}
          </button>
          <button
            type="submit"
            name="channel"
            value="stable"
            class="button upgrade-action-button"
          >
            {% trans "Upgrade on Next Release (Stable)" %}
          </button>
        </div>
      </form>
    </div>
    {% with report=auto_upgrade_report %}
    <div class="upgrade-report-section">
      <h3>{% trans "Settings" %}</h3>
      <dl>
        <dt>{% trans "Auto-upgrade status" %}</dt>
        <dd>
          {% if report.settings.enabled %}
            {% if report.settings.is_unstable_mode %}
              {% trans "Enabled (tracking the unstable channel)" %}
            {% elif report.settings.is_stable_mode %}
              {% trans "Enabled (tracking the stable release channel)" %}
            {% else %}
              {% blocktrans %}Enabled (mode "{{ report.settings.mode }}"){% endblocktrans %}
            {% endif %}
          {% else %}
            {% if report.settings.lock_exists %}
              {% trans "Disabled (mode file present but unreadable)" %}
            {% else %}
              {% trans "Disabled" %}
            {% endif %}
          {% endif %}
        </dd>
        <dt>{% trans "Channel" %}</dt>
        <dd>
          <code>{{ report.settings.channel }}</code>
          {% if report.settings.raw_mode and report.settings.raw_mode|lower != report.settings.channel %}
            <p class="help">{% blocktrans %}Legacy mode value: {{ report.settings.raw_mode }}{% endblocktrans %}</p>
          {% endif %}
        </dd>
        <dt>{% trans "Local revision" %}</dt>
        <dd>
          {% if report.settings.local_revision %}
            <code>{{ report.settings.local_revision }}</code>
          {% else %}
            <em>{% trans "Unavailable" %}</em>
          {% endif %}
        </dd>
        <dt>{% trans "Origin revision" %}</dt>
        <dd>
          {% if report.settings.origin_revision %}
            <code>{{ report.settings.origin_revision }}</code>
          {% else %}
            <em>{% trans "Unavailable" %}</em>
          {% endif %}
          {% if report.settings.origin_revision_error %}
            <p class="errornote">{{ report.settings.origin_revision_error }}</p>
          {% endif %}
        </dd>
        {% translate "Check Last Revision" as check_revision_label %}
        <dt>{% trans "Revision checks" %}</dt>
        <dd>
          <form method="post" action="{% url 'admin:system-upgrade-check-revision' %}" class="upgrade-report-check">
            {% csrf_token %}
            <button
              type="submit"
              class="button upgrade-action-button revision-refresh-button"
              aria-label="{{ check_revision_label }}"
              title="{{ check_revision_label }}"
            >
              <svg
                viewBox="0 0 20 20"
                aria-hidden="true"
                focusable="false"
                class="revision-refresh-icon"
              >
                <path d="M3.3 10a6.7 6.7 0 0111.4-4.7l1.2-1.2v4.4h-4.4l1.6-1.6A4.7 4.7 0 005 10a4.7 4.7 0 004.7 4.7c1.2 0 2.3-.46 3.1-1.3l1.4 1.4A6.7 6.7 0 013.3 10z" />
              </svg>
            </button>
            {% if report.settings.revision_checked_label %}
              <p class="help">{% blocktrans %}Last checked {{ report.settings.revision_checked_label }}.{% endblocktrans %}</p>
            {% else %}
              <p class="help">{% trans "Revision details are retrieved on demand." %}</p>
            {% endif %}
          </form>
        </dd>
        <dt>{% trans "Task" %}</dt>
        <dd>
          <code>{{ report.settings.task_name }}</code>
          {% if report.schedule.task_admin_url %}
            [ <a href="{{ report.schedule.task_admin_url }}">{% trans "View" %}</a> ]
          {% endif %}
          {% if report.settings.task_path %}
            — <code>{{ report.settings.task_path }}</code>
          {% endif %}
        </dd>
        <dt>{% trans "Log file" %}</dt>
        <dd><code>{{ report.settings.log_path }}</code></dd>
        <dt>{% trans "Suite uptime" %}</dt>
        <dd>
          {% if report.settings.suite_uptime %}
            {{ report.settings.suite_uptime }}
          {% else %}
            <em>{% trans "Unavailable" %}</em>
          {% endif %}
        </dd>
        <dt>{% trans "Blocked revisions" %}</dt>
        <dd>
          {% if report.settings.skip_revisions %}
          <ul>
            {% for revision in report.settings.skip_revisions %}
            <li><code>{{ revision }}</code></li>
            {% endfor %}
          </ul>
          {% else %}
          <em>{% trans "No revisions are currently blocked." %}</em>
          {% endif %}
        </dd>
      </dl>
      {% if report.settings.read_error %}
        <p class="errornote">{% trans "The auto-upgrade mode could not be read; verify the lock file permissions." %}</p>
      {% endif %}
    </div>
    <div class="upgrade-report-section">
      <h3>{% trans "Schedule" %}</h3>
      {% if report.schedule.available %}
        {% if report.schedule.configured %}
        <table class="table table-striped">
          <tbody>
            <tr>
              <th scope="row">{% trans "Enabled" %}</th>
              <td>
                {% if report.schedule.enabled %}
                  {% trans "Yes" %}
                {% else %}
                  {% trans "No" %}
                {% endif %}
              </td>
            </tr>
            <tr>
              <th scope="row">{% trans "One-off" %}</th>
              <td>
                {% if report.schedule.one_off %}
                  {% trans "Yes" %}
                {% else %}
                  {% trans "No" %}
                {% endif %}
              </td>
            </tr>
            <tr>
              <th scope="row">{% trans "Queue" %}</th>
              <td>{{ report.schedule.queue|default:"" }}</td>
            </tr>
            <tr>
              <th scope="row">{% trans "Schedule" %}</th>
              <td>
                {{ report.schedule.schedule|default:"" }}
                {% if report.schedule.config_admin_url %}
                  [ <a href="{{ report.schedule.config_admin_url }}">{% trans "View" %}</a> ]
                {% endif %}
              </td>
            </tr>
            <tr>
              <th scope="row">{% trans "Next run" %}</th>
              <td>{{ report.schedule.next_run|default:"" }}</td>
            </tr>
            <tr>
              <th scope="row">{% trans "Last run" %}</th>
              <td>{{ report.schedule.last_run_at|default:"" }}</td>
            </tr>
            <tr>
              <th scope="row">{% trans "Start time" %}</th>
              <td>{{ report.schedule.start_time|default:"" }}</td>
            </tr>
            <tr>
              <th scope="row">{% trans "Expires" %}</th>
              <td>{{ report.schedule.expires|default:"" }}</td>
            </tr>
            <tr>
              <th scope="row">{% trans "Run count" %}</th>
              <td>{{ report.schedule.total_run_count }}</td>
            </tr>
            <tr>
              <th scope="row">{% trans "Failure count" %}</th>
              <td>{{ report.schedule.failure_count }}</td>
            </tr>
            {% if report.schedule.description %}
            <tr>
              <th scope="row">{% trans "Description" %}</th>
              <td>{{ report.schedule.description }}</td>
            </tr>
            {% endif %}
          </tbody>
        </table>
        {% else %}
          <p class="help">{% trans "The auto-upgrade periodic task has not been created yet." %}</p>
        {% endif %}
      {% else %}
        {% if report.schedule.error %}
          <p class="help">{{ report.schedule.error }}</p>
        {% else %}
          <p class="help">{% trans "Scheduling information is unavailable." %}</p>
        {% endif %}
      {% endif %}
    </div>
    <div class="upgrade-report-section">
      <h3>{% trans "Recent activity" %}</h3>
      {% if report.log_entries %}
      <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col">{% trans "Timestamp" %}</th>
            <th scope="col">{% trans "Message" %}</th>
          </tr>
        </thead>
        <tbody>
          {% for entry in report.log_entries %}
          <tr>
            <td>{{ entry.timestamp|default:"" }}</td>
            <td><code>{{ entry.message }}</code></td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% else %}
        <p class="help">{% trans "No auto-upgrade activity has been recorded yet." %}</p>
      {% endif %}
      {% if report.log_error %}
        <p class="errornote">{{ report.log_error }}</p>
      {% endif %}
    </div>
    {% endwith %}
  </div>
</div>
{% endblock %}
