{{define "title"}}{{.Title}}{{end}}

{{define "content"}}
{{template "host_chrome" .}}
{{$page := .Page}}
{{$host := $page.Host}}
{{$f := $page.Form}}
<div class="max-w-[1280px] mx-auto px-8 pb-24 pt-6">

  <h1 class="text-[22px] font-medium tracking-[-0.005em] mt-1">
    {{if $page.IsNew}}New source group{{else}}Edit source group <span class="mono text-ink-mid">·</span> <span class="mono">{{$f.Name}}</span>{{end}}
  </h1>
  <p class="text-pretty text-[13px] text-ink-mute max-w-[720px] mt-2 leading-[1.6]">
    What this group covers and how long its snapshots are worth keeping.
    Snapshots produced for this group carry the group's name as a tag —
    rename with care: existing snapshots keep the old tag and won't get retained
    by a renamed group's policy.
  </p>

  {{if $page.Error}}
    <div class="mt-5 panel rounded-[6px] px-4 py-3 text-[13px]"
         style="border-color: color-mix(in oklch, var(--bad), transparent 60%); background: color-mix(in oklch, var(--bad), transparent 92%); color: var(--ink);">
      {{$page.Error}}
    </div>
  {{end}}

  <form method="post" action="{{$page.SaveAction}}" class="grid grid-cols-12 gap-8 mt-7">

    <div class="col-span-7 panel rounded-[7px] p-7">

      <h3 class="text-[11.5px] font-semibold uppercase tracking-[0.08em] text-ink-mute mb-3.5">Identity</h3>
      <div class="mb-5">
        <label class="field-label" for="name">Name</label>
        <input type="text" id="name" name="name" class="field mono" value="{{$f.Name}}" autofocus
               required pattern="[a-z0-9][a-z0-9_-]*" />
        <div class="field-help">Used as the snapshot tag. Lowercase, no spaces; matches what <span class="mono text-ink-mid">restic forget --tag</span> sees.</div>
      </div>

      <h3 class="text-[11.5px] font-semibold uppercase tracking-[0.08em] text-ink-mute mb-3.5 mt-5 pt-4 border-t border-line-soft">Paths</h3>
      <div class="mb-4">
        <label class="field-label" for="includes">Includes <span class="text-ink-fade">· one path per line</span></label>
        <textarea id="includes" name="includes" class="field mono" rows="4" style="resize: vertical;">{{$f.Includes}}</textarea>
        <div class="field-help">What <span class="mono text-ink-mid">restic backup</span> walks. Agent runs as root with <span class="mono text-ink-mid">CAP_DAC_READ_SEARCH</span>, so any readable path is fair game.</div>
      </div>
      <div class="mb-5">
        <label class="field-label" for="excludes">Excludes <span class="text-ink-fade">· optional, one pattern per line</span></label>
        <textarea id="excludes" name="excludes" class="field mono" rows="3" style="resize: vertical;">{{$f.Excludes}}</textarea>
        <div class="field-help">Passed straight through as <span class="mono text-ink-mid">--exclude</span> args.</div>
      </div>

      <h3 class="text-[11.5px] font-semibold uppercase tracking-[0.08em] text-ink-mute mb-3.5 mt-5 pt-4 border-t border-line-soft">
        Retention
        <span class="text-ink-fade font-medium normal-case tracking-[0.01em] ml-2">applied nightly · all blank = keep everything</span>
      </h3>

      {{if and (not $page.IsNew) $f.ConflictDimension}}
        <div class="mb-3.5 flex gap-3 items-start rounded-[6px] px-3.5 py-3"
             style="border: 1px solid color-mix(in oklch, var(--warn), transparent 60%); background: color-mix(in oklch, var(--warn), transparent 92%);">
          <div class="text-[16px] leading-none text-warn pt-[1px]">⚠</div>
          <div class="text-[12.5px] text-ink-mid leading-[1.55]">
            <strong class="text-ink">keep-{{$f.ConflictDimension}} is set, but no schedule pointing at this group fires often enough to populate that bucket.</strong>
            Either drop <span class="mono text-ink">keep-{{$f.ConflictDimension}}</span> or add a finer-grained schedule.
          </div>
        </div>
      {{end}}

      <div class="grid grid-cols-3 gap-3">
        <div class="keep-cell"><label>Keep last</label><input type="number" min="0" name="keep_last" value="{{$f.KeepLast}}" placeholder="—" /></div>
        <div class="keep-cell"><label>Hourly</label><input type="number" min="0" name="keep_hourly" value="{{$f.KeepHourly}}" placeholder="—" /></div>
        <div class="keep-cell"><label>Daily</label><input type="number" min="0" name="keep_daily" value="{{$f.KeepDaily}}" placeholder="—" /></div>
        <div class="keep-cell"><label>Weekly</label><input type="number" min="0" name="keep_weekly" value="{{$f.KeepWeekly}}" placeholder="—" /></div>
        <div class="keep-cell"><label>Monthly</label><input type="number" min="0" name="keep_monthly" value="{{$f.KeepMonthly}}" placeholder="—" /></div>
        <div class="keep-cell"><label>Yearly</label><input type="number" min="0" name="keep_yearly" value="{{$f.KeepYearly}}" placeholder="—" /></div>
      </div>
      <div class="text-[11.5px] text-ink-fade mt-3 leading-[1.55]">
        Blank fields stay unset (no constraint on that bucket). Forget runs nightly on the cadence configured on the
        <a href="/hosts/{{$host.ID}}/repo" class="text-accent underline" style="text-underline-offset: 2px;">Repo tab</a>.
      </div>

      <h3 class="text-[11.5px] font-semibold uppercase tracking-[0.08em] text-ink-mute mb-3.5 mt-7 pt-4 border-t border-line-soft">
        Retry on offline
        <span class="text-ink-fade font-medium normal-case tracking-[0.01em] ml-2">cron-fired runs only</span>
      </h3>
      <div class="grid grid-cols-2 gap-3.5">
        <div>
          <label class="field-label" for="retry_max">Max attempts</label>
          <input type="number" min="0" id="retry_max" name="retry_max" class="field mono" value="{{$f.RetryMax}}" />
        </div>
        <div>
          <label class="field-label" for="retry_backoff_seconds">Initial backoff (sec)</label>
          <input type="number" min="0" id="retry_backoff_seconds" name="retry_backoff_seconds" class="field mono" value="{{$f.RetryBackoffSeconds}}" />
        </div>
      </div>
      <div class="field-help mt-2">
        Each retry doubles the wait. <strong>Manual run-now ignores this</strong> — it just fails immediately if the agent is offline.
      </div>

      <h3 class="text-[11.5px] font-semibold uppercase tracking-[0.08em] text-ink-mute mb-3.5 mt-7 pt-4 border-t border-line-soft">
        Hooks
        <span class="text-ink-fade font-medium normal-case tracking-[0.01em] ml-2">backup jobs only</span>
      </h3>
      <div class="text-[12px] text-warn leading-[1.55] mb-3"
           style="background: color-mix(in oklch, var(--warn), transparent 92%); border: 1px solid color-mix(in oklch, var(--warn), transparent 75%); padding: 8px 10px; border-radius: 5px;">
        Hooks run as the agent service user — root on Linux, LocalSystem on Windows. Treat them like any other root cron entry.
      </div>
      <div class="mb-3">
        <label class="field-label" for="pre_hook">Pre-backup hook</label>
        <textarea id="pre_hook" name="pre_hook" class="field mono" rows="3" style="resize: vertical;"
                  placeholder="# e.g. systemctl stop myapp">{{$f.PreHook}}</textarea>
        <div class="field-help mt-1">Non-zero exit aborts the backup. Stored AEAD-encrypted.</div>
      </div>
      <div class="mb-3">
        <label class="field-label" for="post_hook">Post-backup hook</label>
        <textarea id="post_hook" name="post_hook" class="field mono" rows="3" style="resize: vertical;"
                  placeholder="# RM_JOB_STATUS={succeeded|failed} is in env">{{$f.PostHook}}</textarea>
        <div class="field-help mt-1">Always runs. <span class="mono">RM_JOB_STATUS</span> is set to the backup's outcome. Stored AEAD-encrypted.</div>
      </div>

      <div class="mt-8 pt-4 border-t border-line-soft flex gap-2">
        <button type="submit" class="btn btn-primary btn-lg">{{if $page.IsNew}}Create group{{else}}Save changes{{end}}</button>
        <a href="/hosts/{{$host.ID}}/sources" class="btn btn-lg">Cancel</a>
      </div>
    </div>

    <aside class="col-span-5">
      <div class="text-[11px] text-ink-fade uppercase tracking-[0.1em] mb-3.5">How this fits</div>
      <ol class="list-none p-0 m-0 text-[13px]">
        <li class="relative pl-9 pb-4">
          <span class="absolute left-0 top-0 w-[22px] h-[22px] border border-line rounded-full text-[11px] leading-[20px] text-center text-ink-mute mono">1</span>
          <div class="font-medium">Save here</div>
          <div class="text-[12px] text-ink-mute mt-1 leading-[1.55]">Bumps the host's schedule version; the agent picks up the new paths/retention on its next push (within seconds when online).</div>
        </li>
        <li class="relative pl-9 pb-4">
          <span class="absolute left-0 top-0 w-[22px] h-[22px] border border-line rounded-full text-[11px] leading-[20px] text-center text-ink-mute mono">2</span>
          <div class="font-medium">Schedules pointing here change behaviour</div>
          <div class="text-[12px] text-ink-mute mt-1 leading-[1.55]">Any schedule that includes this group in its picker now backs up the new paths next time it fires.</div>
        </li>
        <li class="relative pl-9 pb-4">
          <span class="absolute left-0 top-0 w-[22px] h-[22px] border border-line rounded-full text-[11px] leading-[20px] text-center text-ink-mute mono">3</span>
          <div class="font-medium">Retention applies on the next nightly forget</div>
          <div class="text-[12px] text-ink-mute mt-1 leading-[1.55]">Existing tagged snapshots get re-evaluated against the new keep-* rules. Untagged or differently-tagged snapshots are untouched.</div>
        </li>
        <li class="relative pl-9">
          <span class="absolute left-0 top-0 w-[22px] h-[22px] border border-line rounded-full text-[11px] leading-[20px] text-center text-ink-mute mono">4</span>
          <div class="font-medium">Run-now from the Sources list</div>
          <div class="text-[12px] text-ink-mute mt-1 leading-[1.55]">Want to test? Save, go back to <a href="/hosts/{{$host.ID}}/sources" class="text-accent underline">Sources</a>, hit Run-now on this row.</div>
        </li>
      </ol>
    </aside>

  </form>
</div>
{{end}}
