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

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

{% block admin_content %}
<p class="admin-lede">
  Instance-wide settings. These are stored in the database and take effect
  immediately — no restart, and they survive one.
</p>

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

{#
  The instance name used to be here. It lives on Customization now, with the
  logo, the wording and the themes -- one field, one page. Left as a link rather
  than duplicated: two controls writing one value is how each becomes the answer
  to "why did my change not stick?".
#}
<p class="admin-lede">
  The name, the logo, the wording and the themes are on
  <a href="/admin/customization">Customization</a>.
</p>

<form method="post" action="/admin/general">

  <section class="card">
    <h2 class="card__title">Default system prompt</h2>
    <p class="card__lede">
      Applied to every chat that does not have a prompt of its own. A model's
      prompt overrides this, and a chat's prompt overrides both — most specific
      wins outright rather than the three being stacked together.
    </p>
    <div class="field">
      <label class="field__label visually-hidden" for="system-prompt">System prompt</label>
      <textarea class="textarea" id="system-prompt" name="system_prompt" rows="5"
                placeholder="You are a helpful assistant.">{{ values.system_prompt }}</textarea>
      <p class="field__hint">Leave empty to send no system prompt at all.</p>
    </div>
  </section>

  <section class="card">
    <h2 class="card__title">Compaction</h2>
    <p class="card__lede">
      A long conversation eventually fills the model's context. When it gets
      close, the earlier turns are summarised and the summary is sent in their
      place. The messages themselves are kept and stay readable in the
      transcript — they simply stop being sent.
    </p>
    <div class="field">
      <label class="field__label" for="compact-threshold">Compact at</label>
      <input class="input" id="compact-threshold" name="compact_threshold" type="number"
             min="0" max="99" value="{{ values.compact_threshold }}">
      <p class="field__hint">
        Percent of the model's context length. <code>0</code> turns automatic
        compaction off; the button in each chat still works. Nothing happens for
        a model whose context length is unset under
        <a href="/admin/models">Models</a> — that is "unknown", not "small", and
        this will not act on a number nobody supplied. The wording of the
        summary is under <a href="/admin/prompts">Prompts</a>.
      </p>
    </div>
  </section>

  <section class="card">
    <h2 class="card__title">Tool calls in an ordinary chat</h2>
    <p class="card__lede">
      A model ends its own turn the moment it stops asking for tools — that is
      it saying it has what it needs, and nothing here overrides it. This is a
      ceiling for the case where it never says so.
    </p>
    <div class="field">
      <label class="field__label" for="max-chat-rounds">Most rounds of tool calls</label>
      <input class="input" id="max-chat-rounds" name="max_chat_rounds" type="number"
             min="0" max="100" value="{{ values.max_chat_rounds }}">
      <p class="field__hint">
        Several tools can be called in one round, so this is not a count of
        tools. Leave room for at least two: <code>knowledge_get</code> and
        <code>notes_get</code> read a document by an id a <em>search</em>
        returned, so a ceiling of one leaves the library searchable and not
        readable. <code>0</code> means no ceiling, which is how an agent chat
        already works — those are bounded under
        <a href="/admin/agents">Agents</a> by time and tokens instead.
      </p>
    </div>
  </section>

  <section class="card">
    <h2 class="card__title">
      Registration
      {% if values.allow_signup %}
      <span class="badge badge--success">open</span>
      {% else %}
      <span class="badge badge--danger">closed</span>
      {% endif %}
    </h2>

    <div class="field">
      <label class="checkbox">
        <input type="checkbox" name="allow_signup" value="true"
               {{ 'checked' if values.allow_signup }}>
        <span>Anyone who can reach this instance may create an account</span>
      </label>
      <p class="field__hint">
        Turn this off once your users exist. Sign-in is unaffected — existing
        accounts keep working, and the "Create one" link disappears from the
        sign-in page.
      </p>
    </div>

    {% if values.allow_signup and user_count > 0 %}
    <div class="alert alert--warning">
      {{ icon("warning", "alert__icon") }}
      <div>
        <strong>Registration is open.</strong>
        <div class="text-sm" style="margin-top: var(--sp-1)">
          This instance has {{ user_count }} account{{ '' if user_count == 1 else 's' }}.
          Anyone who can reach it can add another, and every account can use your
          configured models and API keys.
        </div>
      </div>
    </div>
    {% endif %}

    <p class="field__hint">
      <code>LEMBAS_ALLOW_SIGNUP</code> in the environment sets only the starting
      value. Once saved here, this setting wins.
    </p>
  </section>

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