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

<KvPageHeader @breadcrumbs={{@breadcrumbs}} @mountName={{@backend}}>
  <:tabLinks>
    <li>
      <LinkTo
        @route={{this.router.currentRoute.localName}}
        @models={{@currentRouteParams}}
        data-test-secrets-tab="Secrets"
        @current-when={{true}}
      >Secrets</LinkTo>
    </li>
    <li>
      <LinkTo @route="configuration" @model={{@backend}} data-test-secrets-tab="Configuration">Configuration</LinkTo>
    </li>
  </:tabLinks>

  <:toolbarFilters>
    {{#if (and (not-eq @secrets 403) (or @secrets @filterValue))}}
      <KvListFilter @mountPoint={{this.mountPoint}} @filterValue={{@filterValue}} />
    {{/if}}
  </:toolbarFilters>

  <:toolbarActions>
    <ToolbarLink
      data-test-toolbar-create-secret
      @route="create"
      @model={{@backend}}
      @query={{hash initialKey=@filterValue}}
      @type="add"
    >
      Create secret
    </ToolbarLink>
  </:toolbarActions>
</KvPageHeader>

{{#if (eq @secrets 403)}}
  <div class="box is-fullwidth is-shadowless has-tall-padding">
    <div class="selectable-card-container one-card">
      <OverviewCard
        @cardTitle="View secret"
        @subText="Type the path of the secret you want to view. Include a trailing slash to navigate to the list view."
      >
        <:content>
          <form {{on "submit" this.transitionToSecretDetail}} class="has-top-margin-m">
            <InputSearch
              @id="search-input-kv-secret"
              @initialValue={{@pathToSecret}}
              @onChange={{this.handleSecretPathInput}}
              @placeholder="secret/"
              data-test-view-secret
            />
            <Hds::Button
              @text={{this.buttonText}}
              @color="secondary"
              type="submit"
              disabled={{not this.secretPath}}
              data-test-submit
            />
          </form>
          {{#if @failedDirectoryQuery}}
            <AlertInline
              @type="danger"
              @message="You do not have the required permissions or the directory does not exist."
            />
          {{/if}}
        </:content>
      </OverviewCard>
    </div>
  </div>
{{else}}
  {{#if @secrets}}
    {{#each @secrets as |secretPath|}}
      {{#let (this.isDirectory secretPath) (this.fullSecretPath secretPath) as |isDir fullPath|}}
        <LinkedBlock
          data-test-list-item={{secretPath}}
          class="list-item-row"
          @params={{array (if isDir "list-directory" "secret.index") @backend fullPath}}
          @linkPrefix={{this.mountPoint}}
        >
          <div class="level is-mobile">
            <div class="level-left">
              <div>
                <Icon @name={{if isDir "folder" "file"}} class="has-text-grey-light" />
                <span class="has-text-weight-semibold is-underline" data-test-path>
                  {{secretPath}}
                </span>
              </div>
            </div>
            <div class="level-right is-flex is-paddingless is-marginless">
              <div class="level-item">
                <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
                  <dd.ToggleIcon
                    @icon="more-horizontal"
                    @text="Manage secret"
                    @hasChevron={{false}}
                    data-test-popup-menu-trigger
                  />
                  {{#if isDir}}
                    <dd.Interactive
                      @route="list-directory"
                      @models={{array @backend fullPath}}
                      data-test-list-menu-item="Content"
                    >
                      Content
                    </dd.Interactive>
                  {{else}}
                    <dd.Interactive
                      @route="secret.index"
                      @models={{array @backend fullPath}}
                      data-test-list-menu-item="Overview"
                    >
                      Overview
                    </dd.Interactive>
                    <dd.Interactive
                      @route="secret.details"
                      @models={{array @backend fullPath}}
                      data-test-list-menu-item="Secret data"
                    >
                      Secret data
                    </dd.Interactive>
                    {{#if @capabilities.canRead}}
                      <dd.Interactive
                        @route="secret.metadata.versions"
                        @models={{array @backend fullPath}}
                        data-test-list-menu-item="View version history"
                      >
                        View version history
                      </dd.Interactive>
                    {{/if}}
                    {{#if @capabilities.canDelete}}
                      <dd.Interactive
                        @color="critical"
                        {{on "click" (fn (mut this.metadataToDelete) secretPath)}}
                        data-test-list-menu-item="Permanently delete"
                      >
                        Permanently delete
                      </dd.Interactive>
                    {{/if}}
                  {{/if}}
                </Hds::Dropdown>
              </div>
            </div>
          </div>
        </LinkedBlock>
      {{/let}}
    {{/each}}
    {{#if this.metadataToDelete}}
      <ConfirmModal
        @color="critical"
        @onClose={{fn (mut this.metadataToDelete) null}}
        @onConfirm={{fn this.onDelete this.metadataToDelete}}
        @confirmMessage="This will permanently delete this secret and all its versions."
      />
    {{/if}}
    <Hds::Pagination::Numbered
      @currentPage={{@secrets.meta.currentPage}}
      @currentPageSize={{@secrets.meta.pageSize}}
      @route={{this.router.currentRoute.localName}}
      @showSizeSelector={{false}}
      @models={{@currentRouteParams}}
      @totalItems={{@secrets.meta.total}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  {{else}}
    {{#if @filterValue}}
      <EmptyState @title='There are no secrets matching "{{@filterValue}}".' />
    {{else}}
      <EmptyState
        data-test-secret-list-empty-state
        @title="No secrets yet"
        @message="When created, secrets will be listed here. Create a secret to get started."
      />
    {{/if}}
  {{/if}}
{{/if}}