{# New-schema config form — multi-bot #}
{% from "_macros.html" import select_options %}

<form method="post" action="/config">
<input type="hidden" name="_schema" value="new">
<input type="hidden" name="_user_count" value="{{ config.users | length }}">
<input type="hidden" name="_bot_count" value="{{ config.bots | length }}">

<div class="config-card">
  <h3 class="config-section-label small-caps">General</h3>

  <div class="form-group">
    <label class="form-label small-caps" for="log_level">Log level</label>
    <select id="log_level" name="log_level" class="form-input">
      {{ select_options(log_level_options, config.log_level) }}
    </select>
  </div>
</div>

<div class="config-card">
  <h3 class="config-section-label small-caps">Agent</h3>

  <div class="form-group">
    <label class="form-label small-caps" for="model">Model</label>
    <input id="model" name="model" type="text" class="form-input"
           value="{{ config.agent.model }}" required>
    <span class="hint">e.g. <code>anthropic/claude-sonnet-4-5-20250514</code></span>
  </div>
</div>

<div class="config-card">
  <h3 class="config-section-label small-caps">Users</h3>

  {% for user in config.users %}
  {% set idx = loop.index0 %}
  <div class="form-group">
    <label class="form-label small-caps" for="user_{{ idx }}_name">Name</label>
    <input id="user_{{ idx }}_name" name="user_{{ idx }}_name" type="text"
           class="form-input" value="{{ user.name }}" required>
  </div>

  <div class="form-group">
    <label class="form-label small-caps" for="user_{{ idx }}_telegram_id">Telegram user ID</label>
    <input id="user_{{ idx }}_telegram_id" name="user_{{ idx }}_telegram_id" type="text"
           class="form-input" value="{{ user.telegram_id if user.telegram_id is not none else '' }}">
  </div>

  {% if not loop.last %}
  <hr style="border: none; border-top: 1px solid var(--border); margin: 24px 0;">
  {% endif %}
  {% endfor %}
</div>

<div class="config-card">
  <h3 class="config-section-label small-caps">Bots</h3>

  {% for bot in config.bots %}
  {% set idx = loop.index0 %}

  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_name">Bot name</label>
    <input id="bot_{{ idx }}_name" name="bot_{{ idx }}_name" type="text"
           class="form-input" value="{{ bot.name }}" required>
  </div>

  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_model">Model override</label>
    <input id="bot_{{ idx }}_model" name="bot_{{ idx }}_model" type="text"
           class="form-input" value="{{ bot.model or '' }}"
           placeholder="(uses global model)">
  </div>

  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_owner">Auth owner</label>
    <input id="bot_{{ idx }}_owner" name="bot_{{ idx }}_owner" type="text"
           class="form-input" value="{{ bot.auth.owner }}" required>
  </div>

  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_allowed">Allowed users</label>
    <input id="bot_{{ idx }}_allowed" name="bot_{{ idx }}_allowed" type="text"
           class="form-input" value="{{ bot.auth.allowed_users | join(', ') }}">
  </div>

  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_bootstrap">Bootstrap</label>
    <select id="bot_{{ idx }}_bootstrap" name="bot_{{ idx }}_bootstrap" class="form-input">
      {{ select_options(bootstrap_options, bot.auth.bootstrap) }}
    </select>
  </div>

  {% for ch in bot.channels %}
  {% set ch_idx = loop.index0 %}
  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_ch_{{ ch_idx }}_type">Channel type</label>
    <input id="bot_{{ idx }}_ch_{{ ch_idx }}_type"
           name="bot_{{ idx }}_ch_{{ ch_idx }}_type" type="text"
           class="form-input" value="{{ ch.type }}" readonly>
  </div>

  <div class="form-group">
    <label class="form-label small-caps" for="bot_{{ idx }}_ch_{{ ch_idx }}_token">Token</label>
    <input id="bot_{{ idx }}_ch_{{ ch_idx }}_token"
           name="bot_{{ idx }}_ch_{{ ch_idx }}_token" type="password"
           class="form-input" value="{{ masked_token if ch.token else '' }}">
  </div>
  {% endfor %}

  {% if not loop.last %}
  <hr style="border: none; border-top: 1px solid var(--border); margin: 24px 0;">
  {% endif %}
  {% endfor %}
</div>

<div class="form-actions">
  <button class="btn btn-primary" type="submit">Save Changes</button>
</div>
</form>
