<div class="container paper-page admin-page">
  <div class="paper-crumbs">Admin &nbsp;/&nbsp; <span class="here">Repositories</span></div>
  <h1 class="paper-page-title">Repositories</h1>

  <nav class="admin-nav">
    <a href="/admin/"><i class="fas fa-tachometer-alt"></i> Overview</a>
    <a href="/admin/repositories" class="active"><i class="fas fa-code-branch"></i> Repositories</a>
    <a href="/admin/users"><i class="fas fa-users"></i> Users</a>
    <a href="/admin/conferences"><i class="fas fa-chalkboard-teacher"></i> Conferences</a>
    <a href="/admin/queues"><i class="fas fa-tasks"></i> Queues</a>
    <a href="/admin/errors"><i class="fas fa-bug"></i> Errors</a>
  </nav>

  <div class="admin-summary">
    <span class="summary-total">{{total >= 0 ? (total | number) : '…'}}</span>
    <span class="summary-meta">{{totalSize | humanFileSize}} on disk</span>
    <span class="summary-pill ok" ng-class="{active: query.ready}" ng-click="query.ready = !query.ready; query.page = 1" title="Toggle ready filter">Ready <span class="count">{{statusCountFor('ready') | number}}</span></span>
    <span class="summary-pill warn" ng-class="{active: query.preparing}" ng-click="query.preparing = !query.preparing; query.page = 1" title="Toggle preparing filter">Preparing <span class="count">{{statusCountFor('preparing') + statusCountFor('download') | number}}</span></span>
    <span class="summary-pill error" ng-class="{active: query.error}" ng-click="query.error = !query.error; query.page = 1" title="Toggle errored filter">Errored <span class="count">{{statusCountFor('error') | number}}</span></span>
    <span class="summary-pill" ng-class="{active: query.expired}" ng-click="query.expired = !query.expired; query.page = 1" title="Toggle expired filter">Expired <span class="count">{{statusCountFor('expired') + statusCountFor('expiring') | number}}</span></span>
    <span class="summary-pill" ng-class="{active: query.removed}" ng-click="query.removed = !query.removed; query.page = 1" title="Toggle removed filter">Removed <span class="count">{{statusCountFor('removed') + statusCountFor('removing') | number}}</span></span>
  </div>

  <div class="alert alert-danger" ng-if="fetchError" style="margin: 8px 0;">
    <i class="fas fa-exclamation-triangle"></i> {{fetchError}}
  </div>

  <form class="w-100 admin-filter-toolbar" aria-label="Repositories" accept-charset="UTF-8">
    <!-- Row 1: search + scoped inputs + headline actions -->
    <div class="admin-filter-row">
      <div class="search-wrap">
        <input
          type="search"
          class="form-control"
          aria-label="Search repositories"
          placeholder="Search repoId, source repo, error message…"
          autocomplete="off"
          ng-model="query.search"
        />
        <span class="admin-search-hint" ng-if="!query.search">/</span>
      </div>
      <span class="admin-filter-inline"><label>Owner</label><input type="text" class="form-control form-control-sm" placeholder="username" ng-model="query.owner" /></span>
      <span class="admin-filter-inline"><label>Conference</label><input type="text" class="form-control form-control-sm" placeholder="ID" ng-model="query.conference" /></span>
      <span class="admin-filter-spacer"></span>
      <button class="btn btn-sm" type="button" ng-click="exportCsv()" title="Export current view to CSV"><i class="fas fa-file-csv"></i> Export</button>
      <span class="admin-filter-inline" aria-label="Pagination">
        <button class="btn btn-sm" type="button" ng-click="query.page = Math.max(1, query.page - 1)" ng-disabled="query.page <= 1"><i class="fas fa-chevron-left"></i></button>
        <span style="font-family: var(--font-mono); font-size: 12px; color: var(--ink-muted);">{{query.page}}/{{totalPage || 1}}</span>
        <button class="btn btn-sm" type="button" ng-click="query.page = Math.min(totalPage, query.page + 1)" ng-disabled="query.page >= totalPage"><i class="fas fa-chevron-right"></i></button>
      </span>
    </div>

    <!-- Row 2: appears only when there are active filter chips -->
    <div class="admin-filter-row" ng-if="chips.length">
      <div class="admin-active-chips">
        <span class="admin-active-chip" ng-repeat="chip in chips track by chip.key">
          <span class="key">{{chip.label}}</span>
          <span>{{chip.value}}</span>
          <button type="button" ng-click="clearFilter(chip.key)" aria-label="Remove filter"><i class="fas fa-times"></i></button>
        </span>
      </div>
    </div>
  </form>

  <div class="bulk-bar" ng-if="selectedCount() > 0">
    <span><strong>{{selectedCount()}}</strong> selected</span>
    <button class="btn btn-sm" type="button" ng-click="bulkRefresh()"><i class="fas fa-sync"></i> Force refresh</button>
    <button class="btn btn-sm text-danger" type="button" ng-click="bulkRemoveCache()"><i class="fas fa-broom"></i> Remove cache</button>
    <button class="btn btn-sm" type="button" ng-click="clearSelection()">Clear</button>
  </div>

  <div class="paper-table paper-table-repos has-bulk w-100" role="table" aria-label="Repositories">
    <div class="paper-table-head" role="row">
      <div role="columnheader" style="width: 28px;">
        <input type="checkbox" ng-click="selectAllOnPage()" ng-checked="allSelected" aria-label="Select all on page" />
      </div>
      <div role="columnheader"><span class="sortable" ng-class="{active: query.sort == 'source.repositoryName'}" ng-click="sortBy('source.repositoryName')">Repository <i class="fas" ng-class="sortIcon('source.repositoryName')"></i></span></div>
      <div role="columnheader"><span class="sortable" ng-class="{active: query.sort == 'status'}" ng-click="sortBy('status')">Status <i class="fas" ng-class="sortIcon('status')"></i></span></div>
      <div role="columnheader" class="num"><span class="sortable" ng-class="{active: query.sort == 'pageView'}" ng-click="sortBy('pageView')">Views <i class="fas" ng-class="sortIcon('pageView')"></i></span></div>
      <div role="columnheader"><span class="sortable" ng-class="{active: query.sort == 'anonymizeDate'}" ng-click="sortBy('anonymizeDate')">Anonymized <i class="fas" ng-class="sortIcon('anonymizeDate')"></i></span></div>
      <div role="columnheader" aria-label="Actions"></div>
    </div>
    <div
      class="paper-table-row"
      role="row"
      ng-class="{'repo-inactive': repo.status == 'expired' || repo.status == 'removed', 'repo-error': repo.status == 'error', 'row-selected': selected[repo.repoId]}"
      ng-repeat="repo in repositories | filter:repoFiler | orderBy:orderBy as filteredRepositories"
    >
      <div role="cell" style="width: 28px;">
        <input type="checkbox" ng-model="selected[repo.repoId]" aria-label="Select repository" />
      </div>
      <div class="cell-anon" role="cell">
        <span class="type-badge type-repo">Repo</span>
        <div class="anon-text">
          <a class="repo-name" target="_blank" ng-href="/r/{{repo.repoId}}" ng-bind="repo.repoId"></a>
          <div class="anon-sub">
            <a href="https://github.com/{{repo.source.repositoryName}}/" ng-bind="repo.source.repositoryName"></a><span ng-if="repo.options.update">&nbsp;&middot;&nbsp;<a href="https://github.com/{{repo.source.fullName}}/tree/{{repo.source.branch}}" ng-bind="repo.source.branch"></a></span><span ng-if="!repo.options.update">&nbsp;&middot;&nbsp;@<a href="https://github.com/{{repo.source.fullName}}/tree/{{repo.source.commit}}" ng-bind="repo.source.commit.substring(0, 8)"></a></span><span ng-if="::repo.conference">&nbsp;&middot;&nbsp;<i class="fas fa-chalkboard-teacher"></i> {{repo.conference}}</span><span>&nbsp;&middot;&nbsp;{{::repo.size.storage | humanFileSize}}</span><span>&nbsp;&middot;&nbsp;{{::repo.options.terms.length | number}} terms</span>
          </div>
        </div>
      </div>
      <div class="cell-status" role="cell">
        <span class="status-line">
          <span class="status-dot" ng-class="{'status-removed': repo.status == 'removed' || repo.status == 'expired', 'status-ready': repo.status == 'ready', 'status-error': repo.status == 'error', 'status-preparing': repo.status == 'preparing'}"></span>
          <span ng-bind="repo.status | title"></span>
        </span>
        <span class="status-sub" ng-if="repo.statusMessage" title="{{repo.statusMessage}}" ng-bind="repo.statusMessage | statusMsg"></span>
      </div>
      <div class="cell-views num" role="cell" ng-bind="::repo.pageView || 0 | number"></div>
      <div class="cell-expires" role="cell" ng-bind="repo.anonymizeDate | humanTime"></div>
      <div class="cell-actions" role="cell">
        <div class="dropdown">
          <button class="btn btn-icon-dots" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" aria-label="Actions">
            <i class="fas fa-ellipsis-h" aria-hidden="true"></i>
          </button>
          <div class="dropdown-menu dropdown-menu-right">
            <a class="dropdown-item" href="/anonymize/{{repo.repoId}}"><i class="far fa-edit"></i> Edit</a>
            <a class="dropdown-item" href="/r/{{repo.repoId}}/"><i class="fa fa-eye"></i> View repo</a>
            <a class="dropdown-item" href="/w/{{repo.repoId}}/" target="_self" ng-if="repo.options.page && repo.status == 'ready'"><i class="fas fa-globe"></i> View page</a>
            <a class="dropdown-item" href="#" ng-click="fetchGithubInfo(repo)"><i class="fab fa-github"></i> Live GitHub info</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#" ng-show="repo.status == 'ready' || repo.status == 'error'" ng-click="updateRepository(repo)"><i class="fas fa-sync"></i> Force update</a>
            <a class="dropdown-item" href="#" ng-show="repo.status == 'removed'" ng-click="updateRepository(repo)"><i class="fas fa-check-circle"></i> Enable</a>
            <a class="dropdown-item" href="#" ng-show="repo.statusMessage" ng-click="showStatusMessage(repo)"><i class="fas fa-exclamation-triangle"></i> View status message</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#" ng-click="removeCache(repo)"><i class="fas fa-broom"></i> Remove cache</a>
            <a class="dropdown-item text-danger" href="#" ng-show="repo.status == 'ready'" ng-click="removeRepository(repo)"><i class="fas fa-trash-alt"></i> Remove</a>
          </div>
        </div>
      </div>
    </div>
    <div class="paper-table-empty" ng-if="filteredRepositories.length == 0">
      <i class="fas fa-inbox"></i>
      <span>No repositories match the current filters.</span>
    </div>
  </div>

  <div class="admin-toolbar" style="justify-content: space-between; border-bottom: none;">
    <span style="font-size: 12px; color: var(--ink-muted);">{{total | number}} results</span>
    <div class="pagination-compact" ng-if="totalPage > 1">
      <button class="btn btn-sm" ng-click="query.page = Math.max(1, query.page - 1)" ng-disabled="query.page <= 1">
        <i class="fas fa-chevron-left"></i> Previous
      </button>
      <input type="number" class="form-control form-control-sm" ng-model="query.page" min="1" max="{{totalPage}}" style="width: 56px;" aria-label="Page" />
      <span>of {{totalPage}}</span>
      <button class="btn btn-sm" ng-click="query.page = Math.min(totalPage, query.page + 1)" ng-disabled="query.page >= totalPage">
        Next <i class="fas fa-chevron-right"></i>
      </button>
    </div>
    <span class="admin-filter-inline">
      <label>Per page</label>
      <select class="form-control form-control-sm" ng-model="query.limit">
        <option value="10">10</option>
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="250">250</option>
      </select>
    </span>
  </div>
</div>
