{{!
  Copyright (c) HashiCorp, Inc.
  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>
    {{#if @canReadMetadata}}
      <li>
        <LinkTo
          @route="secret.metadata.versions"
          @models={{array @backend @path}}
          data-test-secrets-tab="Version History"
        >Version History</LinkTo>
      </li>
    {{/if}}
  </:tabLinks>

  <:toolbarActions>
    {{#if @canDeleteMetadata}}
      <KvDeleteModal @mode="delete-metadata" @onDelete={{this.onDelete}} @text="Permanently delete" />
    {{/if}}
    {{#if @canUpdateMetadata}}
      <ToolbarLink @route="secret.metadata.edit" @models={{array @backend @path}} 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 the user had read permissions and there is no custom_metadata this is an empty object, without read capabilities it's falsy }}
  {{#if this.customMetadata}}
    {{#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 @canUpdateMetadata}}
          <Hds::Link::Standalone
            @icon="plus"
            @text="Add metadata"
            @route="secret.metadata.edit"
            @models={{array @backend @path}}
            data-test-add-custom-metadata
          />
        {{/if}}
      </EmptyState>
    {{/each-in}}
  {{else if @canReadData}}
    {{! Offer opportunity to manually request /data/ for custom_metadata }}
    {{#if this.error.isControlGroup}}
      <ControlGroupInlineError @error={{this.error}} class="has-top-margin-s has-bottom-margin-s" />
    {{else if this.error}}
      <MessageError @errorMessage={{this.error}} />
    {{/if}}
    <EmptyState
      @title="Request custom metadata?"
      @bottomBorder={{true}}
      @message="You do not have access to the metadata endpoint but you can retrieve custom metadata from the secret data endpoint."
    >
      <div class="is-block">
        <Hds::Alert @type="compact" @color="critical" class="has-top-margin-xs" as |A|>
          <A.Description>
            Sensitive secret data will be retrieved.
          </A.Description>
        </Hds::Alert>
        <Hds::Button
          class="has-top-margin-xs"
          @text="Request data"
          @icon="reload"
          @iconPosition="trailing"
          @isFullWidth={{true}}
          data-test-request-data
          {{on "click" this.requestData}}
        />
      </div>
    </EmptyState>
  {{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 @metadata}}
    <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>