{% extends "admin/_layout.html" %}
{% from "_macros.html" import icon %}
{% set section = "schedules" %}

{% block title %}Scheduling - {{ brand.name }}{% endblock %}
{% block heading %}Scheduling{% endblock %}

{% block admin_content %}
<p class="admin-lede">
  A <strong>schedule</strong> runs a piece of work because time has passed
  rather than because somebody asked just now — a daily summary, a check every
  Monday, a reminder in an hour. Each one has its own chat and replies into it,
  or files a report. People make their own under <strong>Scheduled</strong>;
  what you decide here is whether the feature exists and what it may spend.
</p>

<div class="alert">
  {{ icon("shield", "icon--sm") }}
  <span>
    This is the one thing here that spends model time with nobody watching. A
    schedule pointed at a chat that can run commands would run them unattended,
    so a scheduled task is never an agent chat — but everything else a model can
    reach, it can reach on a timer. Give <strong>Schedule work</strong> under
    Groups &amp; permissions to the people who should have it; it is off for
    everybody by default.
  </span>
</div>

{% if saved %}
<div class="alert alert--success">{{ icon("check", "icon--sm") }} <span>Saved.</span></div>
{% endif %}

<form method="post" action="/admin/schedules" class="form-grid">

  <section class="card">
    <h2 class="card__title">Switch</h2>
    <div class="field">
      <label class="checkbox">
        <input type="checkbox" name="enabled" value="true" {{ 'checked' if values.enabled }}>
        <span>Let scheduled work run</span>
      </label>
      <p class="field__hint">
        Off by default. Turning this off stops everything firing and deletes
        nothing — schedules keep their place and resume when it is turned back
        on.
        {% if total %}
          There {{ "is" if total == 1 else "are" }} {{ total }}
          schedule{{ "" if total == 1 else "s" }} on this instance,
          {{ active }} of them not paused.
        {% endif %}
      </p>
    </div>
  </section>

  <section class="card">
    <h2 class="card__title">How often it looks</h2>
    <div class="field">
      <label class="field__label" for="tick">Check for due work every</label>
      <input class="input" type="number" id="tick" name="tick_seconds" min="5" max="300"
             value="{{ values.tick_seconds }}">
      <p class="field__hint">
        Seconds. This is how late a run can be, not how often anything happens:
        the finest a schedule can be set to is one minute, so anything under
        that buys nothing. One indexed query per tick.
      </p>
    </div>

    <div class="field">
      <label class="field__label" for="min-interval">Nothing may repeat faster than</label>
      <input class="input" type="number" id="min-interval" name="min_interval_seconds"
             min="60" max="86400" value="{{ values.min_interval_seconds }}">
      <p class="field__hint">
        Seconds. A floor on how often one schedule may come round. Raise it if
        people are setting things to run more often than the work takes.
      </p>
    </div>
  </section>

  <section class="card">
    <h2 class="card__title">What it may spend</h2>
    <div class="field">
      <label class="field__label" for="per-user">Schedules per person</label>
      <input class="input" type="number" id="per-user" name="max_per_user" min="1" max="200"
             value="{{ values.max_per_user }}">
      <p class="field__hint">
        Refused at the point of creation, with the reason. Existing schedules
        over a lowered limit keep running; only new ones are refused.
      </p>
    </div>

    <div class="field">
      <label class="field__label" for="concurrent">Runs at once</label>
      <input class="input" type="number" id="concurrent" name="max_concurrent" min="1" max="20"
             value="{{ values.max_concurrent }}">
      <p class="field__hint">
        Fifty schedules due at nine o'clock must not open fifty replies against
        one endpoint. The rest wait their turn rather than being dropped.
      </p>
    </div>

    <div class="field">
      <label class="field__label" for="queued">Turns that may pile up in one chat</label>
      <input class="input" type="number" id="queued" name="max_queued" min="1" max="50"
             value="{{ values.max_queued }}">
      <p class="field__hint">
        A schedule that comes round faster than its chat can answer would build
        a backlog for ever. Past this, a run is skipped and says so on the
        schedule rather than joining the queue.
      </p>
    </div>
  </section>

  <div class="btn-row">
    <button class="btn btn--primary" type="submit">Save</button>
  </div>
</form>
{% endblock %}
