{{!
  Copyright IBM Corp. 2016, 2025
  SPDX-License-Identifier: BUSL-1.1
}}

<KvPageHeader @breadcrumbs={{@breadcrumbs}} @secretPath={{@path}}>
  <:tabLinks>
    <li>
      <LinkTo @route="secret.index" @models={{array @backend @path}} data-test-secrets-tab="Overview">Overview</LinkTo>
    </li>
    <li>
      <LinkTo @route="secret.details" @models={{array @backend @path}} data-test-secrets-tab="Secret">Secret</LinkTo>
    </li>
    <li>
      <LinkTo @route="secret.metadata.index" @models={{array @backend @path}} data-test-secrets-tab="Metadata">
        Metadata
      </LinkTo>
    </li>
    <li>
      <LinkTo @route="secret.paths" @models={{array @backend @path}} data-test-secrets-tab="Paths">Paths</LinkTo>
    </li>
    <li>
      <LinkTo
        @route="secret.metadata.versions"
        @models={{array @backend @path}}
        data-test-secrets-tab="Version History"
        current-when={{true}}
      >
        Version History
      </LinkTo>
    </li>
  </:tabLinks>

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

{{#if @capabilities.canReadMetadata}}
  <div class="sub-text has-text-weight-semibold is-flex-end has-short-padding">
    <KvTooltipTimestamp @text="Secret last updated" @timestamp={{@metadata.updated_time}} />
  </div>
  {{#each (sorted-versions @metadata.versions) as |versionData|}}
    <LinkedBlock
      class="list-item-row"
      @params={{array "vault.cluster.secrets.backend.kv.secret.details" @backend @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 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 (is-deleted versionData.deletion_time)}}
              <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.current_version)}}
              <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">
            <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
              <dd.ToggleIcon
                @icon="more-horizontal"
                @text="Manage version"
                @hasChevron={{false}}
                data-test-popup-menu-trigger
              />
              <dd.Interactive
                @route="secret.details"
                @models={{array @backend @path}}
                @query={{hash version=versionData.version}}
              >View version {{versionData.version}}</dd.Interactive>
              {{#if
                (and
                  @capabilities.canCreateVersionData (not versionData.destroyed) (not (is-deleted versionData.deletion_time))
                )
              }}
                <dd.Interactive
                  @route="secret.details.edit"
                  @models={{array @backend @path}}
                  @query={{hash version=versionData.version}}
                  data-test-create-new-version-from={{versionData.version}}
                >
                  Create new version from
                  {{versionData.version}}
                </dd.Interactive>
              {{/if}}
            </Hds::Dropdown>
          </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}}