{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<KvPageHeader @breadcrumbs={{@breadcrumbs}} @pageTitle={{@path}}>
  <:tabLinks>
    <LinkTo @route="secret.details" data-test-secrets-tab="Secret">Secret</LinkTo>
    <LinkTo @route="secret.metadata.index" data-test-secrets-tab="Metadata">Metadata</LinkTo>
    <LinkTo @route="secret.paths" data-test-secrets-tab="Paths">Paths</LinkTo>
    <LinkTo @route="secret.metadata.versions" data-test-secrets-tab="Version History">Version History</LinkTo>
  </:tabLinks>

  <:toolbarActions>
    {{#if @metadata.canReadMetadata}}
      <ToolbarLink @route="secret.metadata.diff">Version diff</ToolbarLink>
    {{/if}}
  </:toolbarActions>
</KvPageHeader>

{{#if @metadata.canReadMetadata}}
  <div class="sub-text has-text-weight-semibold is-flex-end has-short-padding">
    <KvTooltipTimestamp @text="Secret last updated" @timestamp={{@metadata.updatedTime}} />
  </div>
  {{#each @metadata.sortedVersions as |versionData|}}
    <LinkedBlock
      class="list-item-row"
      @params={{array "vault.cluster.secrets.backend.kv.secret.details" @metadata.path}}
      @queryParams={{hash version=versionData.version}}
      data-test-version-linked-block={{versionData.version}}
    >
      <div class="level is-mobile">
        <div class="is-grid is-grid-3-columns is-three-fourths-width">
          {{! version number and icon }}
          <div class="align-self-center">
            <Icon @name="history" class="has-text-grey" data-test-version />
            <span class="has-text-weight-semibold has-text-black">
              Version
              {{versionData.version}}
            </span>
          </div>
          {{! icons }}
          <div class="align-self-center" data-test-icon-holder={{versionData.version}}>
            {{#if versionData.destroyed}}
              <div>
                <span class="has-text-danger is-size-8 is-block">
                  <Icon @name="x-square-fill" />Destroyed
                </span>
              </div>
            {{else if versionData.isSecretDeleted}}
              <div>
                <span class="has-text-grey is-size-8 is-block">
                  <Icon @name="x-square-fill" />
                  <KvTooltipTimestamp @text="Deleted" @timestamp={{versionData.deletion_time}} />
                </span>
              </div>
            {{else if (loose-equal versionData.version @metadata.currentVersion)}}
              <div>
                <span class="has-text-success is-size-8 is-block">
                  <Icon @name="check-circle-fill" />Current
                </span>
              </div>
            {{/if}}
          </div>
          {{! version created date }}
          <div class="is-size-8 has-text-weight-semibold has-text-grey align-self-center">
            <KvTooltipTimestamp @text="Created" @timestamp={{versionData.created_time}} />
          </div>
        </div>

        <div class="level-right">
          <div class="level-item">
            <PopupMenu @name="version-{{versionData.version}}">
              <nav class="menu">
                <ul class="menu-list">
                  <li>
                    <LinkTo @route="secret.details" @query={{hash version=versionData.version}}>
                      View version
                      {{versionData.version}}
                    </LinkTo>
                  </li>
                  {{#if @metadata.canCreateVersionData}}
                    <li>
                      <LinkTo
                        @route="secret.details.edit"
                        @query={{hash version=versionData.version}}
                        data-test-create-new-version-from={{versionData.version}}
                        @disabled={{or versionData.destroyed versionData.isSecretDeleted}}
                      >
                        Create new version from
                        {{versionData.version}}
                      </LinkTo>
                    </li>
                  {{/if}}
                </ul>
              </nav>
            </PopupMenu>
          </div>
        </div>
      </div>
    </LinkedBlock>
  {{/each}}
{{else}}
  <EmptyState
    @title="You do not have permission to read metadata"
    @message="Ask your administrator if you think you should have access."
  />
{{/if}}