{{!
  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>
    {{#if @capabilities.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 @capabilities.canDeleteMetadata}}
      <KvDeleteModal @mode="delete-metadata" @onDelete={{this.onDelete}} @text="Permanently delete" />
    {{/if}}
    {{#if @capabilities.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>
  {{#each-in this.customMetadata as |key value|}}
    <InfoTableRow @alwaysRender={{false}} @label={{key}} @value={{value}} />
  {{else}}
    {{#if (and (not @capabilities.canReadMetadata) (not @capabilities.canReadData))}}
      <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."
      />
    {{else}}
      {{! 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}}
      {{#if this.canRequestData}}
        <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="No custom metadata"
          @bottomBorder={{true}}
          @message="This data is version-agnostic and is usually used to describe the secret being stored."
        >
          {{#if @capabilities.canUpdateMetadata}}
            <Hds::Link::Standalone
              @icon="plus"
              @text="Add metadata"
              @route="secret.metadata.edit"
              @models={{array @backend @path}}
              data-test-add-custom-metadata
            />
          {{/if}}
        </EmptyState>
      {{/if}}
    {{/if}}
  {{/each-in}}
</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.updated_time}} />
      </InfoTableRow>
      <InfoTableRow @alwaysRender={{true}} @label="Maximum versions" @value={{@metadata.max_versions}} />
      <InfoTableRow @alwaysRender={{true}} @label="Check-and-Set required" @value={{@metadata.cas_required}} />
      <InfoTableRow
        @alwaysRender={{true}}
        @label="Delete version after"
        @value={{if
          (eq @metadata.delete_version_after "0s")
          "Never delete"
          (format-duration @metadata.delete_version_after)
        }}
      />
    </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>