{{!
  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>
    {{#if @secret.canReadMetadata}}
      <LinkTo @route="secret.metadata.versions" data-test-secrets-tab="Version History">Version History</LinkTo>
    {{/if}}
  </:tabLinks>

  <:toolbarActions>
    {{#if @secret.canDeleteMetadata}}
      <KvDeleteModal @mode="delete-metadata" @metadata={{@metadata}} @onDelete={{this.onDelete}}>
        Permanently delete
      </KvDeleteModal>
    {{/if}}
    {{#if @secret.canUpdateMetadata}}
      <ToolbarLink @route="secret.metadata.edit" data-test-edit-metadata>Edit metadata</ToolbarLink>
    {{/if}}
  </:toolbarActions>
</KvPageHeader>

<h2 class="title is-5 has-bottom-padding-s has-top-margin-l">
  Custom metadata
</h2>
<div class="box is-fullwidth is-sideless is-paddingless is-marginless" data-test-kv-custom-metadata-section>
  {{#if (or @metadata.canReadMetadata @secret.canReadData)}}
    {{#each-in this.customMetadata as |key value|}}
      <InfoTableRow @alwaysRender={{false}} @label={{key}} @value={{value}} />
    {{else}}
      <EmptyState
        @title="No custom metadata"
        @bottomBorder={{true}}
        @message="This data is version-agnostic and is usually used to describe the secret being stored."
      >
        {{#if @secret.canUpdateMetadata}}
          <LinkTo @route="secret.metadata.edit" @model={{@path}} data-test-add-custom-metadata>
            Add metadata
          </LinkTo>
        {{/if}}
      </EmptyState>
    {{/each-in}}
  {{else}}
    <EmptyState
      @title="You do not have access to read custom metadata"
      @message="In order to read custom metadata you either need read access to the secret data and/or read access to metadata."
    />
  {{/if}}
</div>
<section data-test-kv-metadata-section>
  <h2 class="title is-5 has-bottom-padding-s has-top-margin-l">
    Secret metadata
  </h2>
  {{#if @secret.canReadMetadata}}
    <div class="box is-fullwidth is-sideless is-paddingless is-marginless" data-test-kv-metadata-section>
      <InfoTableRow @alwaysRender={{true}} @label="Last updated">
        <KvTooltipTimestamp @timestamp={{@metadata.updatedTime}} />
      </InfoTableRow>
      <InfoTableRow @alwaysRender={{true}} @label="Maximum versions" @value={{@metadata.maxVersions}} />
      <InfoTableRow @alwaysRender={{true}} @label="Check-and-Set required" @value={{@metadata.casRequired}} />
      <InfoTableRow
        @alwaysRender={{true}}
        @label="Delete version after"
        @value={{if (eq @metadata.deleteVersionAfter "0s") "Never delete" (format-duration @metadata.deleteVersionAfter)}}
      />
    </div>
  {{else}}
    <EmptyState
      @title="You do not have access to secret metadata"
      @message="In order to edit secret metadata, the UI requires read permissions; otherwise, data may be deleted. Edits can still be made via the API and CLI."
    />
  {{/if}}
</section>