{{template "base" .}}
{{define "content"}}
<div style="max-width:1200px">

  <div style="margin-bottom:16px">
    <div class="page-title">Machines</div>
    <div class="page-sub">CrowdSec agent registrations</div>
  </div>

  {{if not .CLIAvailable}}
  <div class="cli-unavail-banner" style="margin-bottom:16px">
    <div>
      <strong style="display:block;margin-bottom:4px">cscli unavailable</strong>
      Machine management requires the cscli binary. Mount it at the CSCLI_PATH configured in your environment.
    </div>
  </div>
  {{end}}

  <div class="panel">
    <div class="panel-header">
      <span class="panel-title">Machines ({{len .Machines}})</span>
    </div>
    {{if .Machines}}
    <div style="overflow-x:auto">
      <table class="data-table">
        <thead>
          <tr>
            <th>Machine ID</th>
            <th>IP</th>
            <th>Last Heartbeat</th>
            <th>Version</th>
            <th>Auth</th>
            <th>Status</th>
            {{if $.CLIAvailable}}<th>Actions</th>{{end}}
          </tr>
        </thead>
        <tbody>
          {{range .Machines}}
          <tr>
            <td style="font-family:'JetBrains Mono',monospace;font-size:12px;font-weight:600" title="{{.MachineID}}">{{truncate .MachineID 32}}</td>
            <td style="font-family:'JetBrains Mono',monospace;font-size:12px">{{.IPAddress}}</td>
            <td style="font-size:12px;color:var(--muted)">{{truncate .LastHeartbeat 16}}</td>
            <td style="font-family:'JetBrains Mono',monospace;font-size:11px;color:var(--muted)">{{.Version}}</td>
            <td style="font-size:12px;color:var(--muted)">{{.AuthType}}</td>
            <td>
              {{if .IsValidated}}
                <span class="badge badge-green">validated</span>
              {{else}}
                <span class="badge badge-amber">pending</span>
              {{end}}
            </td>
            {{if $.CLIAvailable}}
            <td style="display:flex;gap:6px;flex-wrap:wrap">
              {{if not .IsValidated}}
              <form method="POST" action="/machines/validate" style="display:inline">
                <input type="hidden" name="_csrf" value="{{$.CSRFToken}}">
                <input type="hidden" name="id" value="{{.MachineID}}">
                <button type="submit" class="btn-safe-sm" onclick="return confirm('Validate machine &quot;{{.MachineID}}&quot;?')">Validate</button>
              </form>
              {{end}}
              <form method="POST" action="/machines/delete" style="display:inline">
                <input type="hidden" name="_csrf" value="{{$.CSRFToken}}">
                <input type="hidden" name="id" value="{{.MachineID}}">
                <button type="submit" class="btn-danger-sm" onclick="return confirm('Delete machine &quot;{{.MachineID}}&quot;?')">Delete</button>
              </form>
            </td>
            {{end}}
          </tr>
          {{end}}
        </tbody>
      </table>
    </div>
    {{else}}
    <div class="empty-state">
      <div class="empty-text">No machines registered</div>
      <div class="empty-sub">Register an agent: cscli machines add &lt;name&gt; -a</div>
    </div>
    {{end}}
  </div>
</div>
{{end}}
