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

<KvPageHeader @breadcrumbs={{@breadcrumbs}} @secretPath={{@path}}>
  <:syncDetails>
    {{#if this.syncStatus}}
      <Hds::Alert data-test-sync-alert @type="inline" class="has-top-margin-s has-bottom-margin-m" @color="neutral" as |A|>
        <A.Title>
          This secret has been synced from Vault to
          {{pluralize this.syncStatus.length "destination"}}. Updates to this secret will automatically sync to its
          {{if (eq this.syncStatus.length 1) "destination" "destinations"}}.
        </A.Title>

        {{#each this.syncStatus as |status|}}
          <A.Description data-test-sync-alert={{status.name}}>
            <SyncStatusBadge @status={{status.sync_status}} />
            <Hds::Link::Inline
              @route="syncDestination"
              @color="secondary"
              @isRouteExternal={{true}}
              @models={{array status.type status.name}}
            >
              {{status.name}}
            </Hds::Link::Inline>
            - last updated
            {{date-format status.updated_at "MMMM do yyyy, h:mm:ss a"}}
          </A.Description>
        {{/each}}

        <A.Button
          @icon={{if this.fetchSyncStatus.isRunning "loading"}}
          @text="Refresh"
          @color="secondary"
          {{on "click" (perform this.fetchSyncStatus)}}
        />
        <A.LinkStandalone
          @isHrefExternal={{true}}
          @icon="docs-link"
          @text="About sync statuses"
          @href={{doc-link "/vault/docs/sync#sync-statuses"}}
        />
      </Hds::Alert>
    {{/if}}
  </:syncDetails>

  <: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" @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>

  <:toolbarFilters>
    {{#unless this.emptyState}}
      <Toggle @name="json" @checked={{this.showJsonView}} @onChange={{fn (mut this.showJsonView)}}>
        <span class="has-text-grey">JSON</span>
      </Toggle>
    {{/unless}}
  </:toolbarFilters>
  <:toolbarActions>
    {{#if this.showUndelete}}
      <Hds::Button
        @text="Undelete"
        @color="secondary"
        class="toolbar-button"
        data-test-kv-delete="undelete"
        {{on "click" this.undelete}}
      />
    {{/if}}
    {{#if this.showDelete}}
      <KvDeleteModal
        @mode="delete"
        @secret={{@secret}}
        @metadata={{@metadata}}
        @capabilities={{@capabilities}}
        @onDelete={{this.handleDestruction}}
        @version={{this.version}}
      />
    {{/if}}
    {{#if this.showDestroy}}
      <KvDeleteModal
        @mode="destroy"
        @secret={{@secret}}
        @capabilities={{@capabilities}}
        @onDelete={{this.handleDestruction}}
        @version={{this.version}}
      />
    {{/if}}
    {{#if (or @capabilities.canReadData @capabilities.canReadMetadata @capabilities.canUpdateData)}}
      <div class="toolbar-separator"></div>
    {{/if}}
    {{#if (and @capabilities.canReadData (eq this.secretState "created"))}}
      <CopySecretDropdown
        @clipboardText={{stringify @secret.data}}
        @onWrap={{perform this.wrapSecret}}
        @isWrapping={{this.wrapSecret.isRunning}}
        @wrappedData={{this.wrappedData}}
        @onClose={{this.clearWrappedData}}
      />
    {{/if}}
    {{#if @capabilities.canReadMetadata}}
      <KvVersionDropdown @displayVersion={{this.version}} @metadata={{@metadata}} @onClose={{this.closeVersionMenu}} />
    {{/if}}
    {{! @isPatchAllowed is true if the version is enterprise AND a user has "patch" secret + "read" subkeys capabilities }}
    {{#if @isPatchAllowed}}
      <ToolbarLink data-test-patch-latest-version @route="secret.patch" @models={{array @backend @path}} @type="add">
        Patch latest version
      </ToolbarLink>
    {{/if}}
    {{#if @capabilities.canUpdateData}}
      <ToolbarLink data-test-create-new-version @route="secret.details.edit" @models={{array @backend @path}} @type="add">
        Create new version
      </ToolbarLink>
    {{/if}}
  </:toolbarActions>
</KvPageHeader>

{{#if (or this.isSecretDeleted (not this.emptyState))}}
  <div class="info-table-row-header">
    <div class="info-table-row thead {{if this.showJsonView 'is-shadowless'}} ">
      {{#unless this.hideHeaders}}
        <div class="th column is-one-quarter">
          Key
        </div>
        <div class="th column">
          Value
        </div>
      {{/unless}}
      <div class="th column justify-right">
        {{#if (or this.isSecretDeleted @secret.created_time)}}
          <KvTooltipTimestamp
            @text="Version {{@secret.version}} {{this.secretState}}"
            @timestamp={{(if this.isSecretDeleted @secret.deletion_time @secret.created_time)}}
          />
        {{/if}}
      </div>
    </div>
  </div>
{{/if}}

{{#if this.emptyState}}
  <EmptyState @title={{this.emptyState.title}} @message={{this.emptyState.message}}>
    {{#if this.emptyState.link}}
      <Hds::Link::Standalone
        @icon="docs-link"
        @iconPosition="trailing"
        @text="KV v2 API docs"
        @href={{doc-link this.emptyState.link}}
        @isHrefExternal={{true}}
      />
    {{/if}}
  </EmptyState>
{{else}}
  <hr class="is-marginless has-background-gray-200" />
  {{#if this.showJsonView}}
    <Hds::CodeBlock
      data-test-code-block="secret-data"
      @value={{stringified-secret-data @secret.secretData}}
      @language="json"
      @hasCopyButton={{true}}
      @maxHeight="300px"
      as |CB|
    >
      <CB.Title @tag="h3">
        Version data
      </CB.Title>
    </Hds::CodeBlock>
  {{else}}
    {{#each-in @secret.secretData as |key value|}}
      <InfoTableRow @label={{key}} @value={{value}} @alwaysRender={{true}}>
        <MaskedInput @name={{key}} @value={{value}} @displayOnly={{true}} @allowCopy={{true}} />
      </InfoTableRow>
    {{else}}
      <InfoTableRow @label="" @value="" @alwaysRender={{true}} />
    {{/each-in}}
  {{/if}}
{{/if}}