{% extends "base.html" %}
{% block title %}{{ repo_id }} — Configuration — Airut{% endblock %}
{% block head_extra %}
<link rel="stylesheet" href="{{ static_url('styles/config.css') }}">
{% endblock %}

{% block body %}
<div class="page">
  {% if error %}
  <div class="cfg-banner error">{{ error }}</div>
  {% endif %}

  {% if buffer %}
  {# Save bar #}
  <div class="cfg-save-bar">
    <button id="review-save-btn" class="cfg-btn primary" {{ '' if dirty_count else 'disabled' }}
            hx-get="/api/config/diff"
            hx-target="#diff-modal-body"
            hx-swap="innerHTML"
            hx-headers='{"X-Requested-With":"XMLHttpRequest"}'>
      Review &amp; Save
    </button>
    <button id="discard-btn" class="cfg-btn danger" {{ '' if dirty_count else 'disabled' }}
            hx-post="/api/config/discard"
            hx-headers='{"X-Requested-With":"XMLHttpRequest"}'
            hx-confirm="Discard all unsaved changes?">
      Discard
    </button>
    <span id="dirty-count" class="cfg-dirty {{ '' if dirty_count else 'hidden' }}">
      {%- if dirty_count -%}
        {{ dirty_count }} unsaved change{{ 's' if dirty_count != 1 else '' }}
      {%- endif -%}
    </span>
  </div>

  {# Stale banner #}
  {% if stale %}
  <div class="cfg-banner warning">
    Config changed externally since you started editing. Unsaved changes may be lost.
    <a href="/config/repos/{{ repo_id }}">Reload</a>
  </div>
  {% endif %}

  {# Repo Settings #}
  <h2 class="cfg-section-title">Repository Settings</h2>

  {# Git & Identity card #}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Git &amp; Identity</span>
    </div>
    {% for f in schema %}
    {% if f.name == 'git_repo_url' %}
    {% include "components/config/field.html" %}
    {% endif %}
    {% endfor %}
  </div>

  {# Model & Effort card #}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Model &amp; Execution</span>
    </div>
    {% for f in schema %}
    {% if f.name in ['model', 'effort', 'claude_version'] %}
    {% include "components/config/field.html" %}
    {% endif %}
    {% endfor %}
  </div>

  {# Network card #}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Network</span>
    </div>
    {% for f in schema %}
    {% if f.name == 'network_sandbox_enabled' %}
    {% include "components/config/field.html" %}
    {% endif %}
    {% endfor %}
  </div>

  {# Container card #}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Container</span>
    </div>
    {% for f in schema %}
    {% if f.name == 'container_path' %}
    {% include "components/config/field.html" %}
    {% endif %}
    {% endfor %}
  </div>

  {# Resource Limits card #}
  {% for f in schema %}
  {% if f.type_tag == 'nested' and f.name == 'resource_limits' %}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Resource Limits</span>
    </div>
    {% include "components/config/nested.html" %}
  </div>
  {% endif %}
  {% endfor %}

  {# Channels section #}
  <h2 class="cfg-section-title">Channels</h2>

  {% include "components/config/channels.html" %}

  {# Credentials section #}
  <h2 class="cfg-section-title">Credentials</h2>

  {# Plain secrets (dict[str, str]) #}
  {% for f in schema %}
  {% if f.name == 'secrets' %}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Plain Secrets</span>
    </div>
    {% include "components/config/field.html" %}
  </div>
  {% endif %}
  {% endfor %}

  {# Masked secrets (keyed collection) #}
  {% for f in schema %}
  {% if f.name == 'masked_secrets' %}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Masked Secrets</span>
    </div>
    {% include "components/config/field.html" %}
  </div>
  {% endif %}
  {% endfor %}

  {# Signing credentials (keyed collection) #}
  {% for f in schema %}
  {% if f.name == 'signing_credentials' %}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Signing Credentials</span>
    </div>
    {% include "components/config/field.html" %}
  </div>
  {% endif %}
  {% endfor %}

  {# GitHub App credentials (keyed collection) #}
  {% for f in schema %}
  {% if f.name == 'github_app_credentials' %}
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">GitHub App Credentials</span>
    </div>
    {% include "components/config/field.html" %}
  </div>
  {% endif %}
  {% endfor %}

  {# Scheduled Tasks section #}
  {% for f in schema %}
  {% if f.name == 'schedules' %}
  <h2 class="cfg-section-title">Scheduled Tasks</h2>
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Schedules</span>
    </div>
    {% include "components/config/field.html" %}
  </div>
  {% endif %}
  {% endfor %}

  {# Remove repo #}
  <h2 class="cfg-section-title">Danger Zone</h2>
  <div class="card">
    <div class="cfg-card-header">
      <span class="cfg-card-title">Remove Repository</span>
    </div>
    <p class="cfg-remove-hint">
      Remove this repository from the configuration. This does not delete any
      data on disk — only removes the config entry.
    </p>
    <button class="cfg-btn danger" id="remove-repo-btn"
            hx-post="/api/config/remove"
            hx-vals='{"path":"repos","key":"{{ repo_id }}"}'
            hx-headers='{"X-Requested-With":"XMLHttpRequest"}'
            hx-confirm="Remove repository '{{ repo_id }}' from config?"
            hx-swap="none">
      Remove {{ repo_id }}
    </button>
  </div>

  {# Diff dialog #}
  <dialog id="diff-modal" class="cfg-dialog">
    <div class="cfg-dialog-header">Review Changes</div>
    <div id="diff-modal-body">Loading...</div>
    <div id="save-feedback"></div>
    <div class="cfg-dialog-footer">
      <button class="cfg-btn primary"
              hx-post="/api/config/save"
              hx-target="#save-feedback"
              hx-swap="innerHTML"
              hx-headers='{"X-Requested-With":"XMLHttpRequest"}'>
        Confirm Save
      </button>
      <button type="button" class="cfg-btn" id="diff-cancel-btn">Cancel</button>
    </div>
  </dialog>
  {% endif %}
</div>
{% endblock %}

{% block scripts %}
<script src="{{ static_url('vendor/htmx.min.js') }}"></script>
<script src="{{ static_url('js/config-editor.js') }}"></script>
{% endblock %}
