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

<Secrets::DestinationHeader
  @destination={{@destination}}
  @capabilities={{@capabilities}}
  @refreshList={{this.refreshRoute}}
/>

{{#if @associations.meta.filteredTotal}}
  <div class="has-bottom-margin-s">
    {{#each @associations as |association index|}}
      <ListItem as |Item|>
        <Item.content>
          <div>
            <Hds::TooltipButton @text="KV v2 engine mount path">
              <Hds::Badge @text="{{association.mount}}/" />
            </Hds::TooltipButton>
            <LinkToExternal
              data-test-association-name={{index}}
              class="has-text-black has-text-weight-semibold"
              @route="kvSecretOverview"
              @models={{array association.mount association.secret_name}}
            >
              {{association.secret_name}}
            </LinkToExternal>
            {{#if association.sub_key}}
              <Hds::Badge @text="secret key: {{association.sub_key}}/" />
            {{/if}}
            <div>
              <SyncStatusBadge @status={{association.sync_status}} data-test-association-status={{index}} />
              <code class="has-text-grey is-size-8" data-test-association-updated={{index}}>
                last updated on
                {{date-format association.updated_at "MMMM do yyyy, h:mm:ss a"}}
              </code>
            </div>
          </div>
        </Item.content>

        <Item.menu>
          <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" @width="210px" as |dd|>
            <dd.ToggleIcon
              @icon="more-horizontal"
              @text="Synced secret popup menu"
              @hasChevron={{false}}
              data-test-popup-menu-trigger
            />
            {{#if (eq @destination.options.granularity_level "secret-key")}}
              <dd.Description
                @text='Sync or unsync actions will apply to the secret "{{association.secret_name}}" and not this individual key.'
              />
              <dd.Separator />
            {{/if}}
            {{#if (has-capability @capabilities "update" pathKey="syncSetAssociation" params=@destination)}}
              <dd.Interactive data-test-association-action="sync" {{on "click" (fn this.update association "set")}}>
                Sync now
              </dd.Interactive>
            {{/if}}
            <dd.Interactive
              data-test-association-action="view"
              @route="kvSecretOverview"
              @isRouteExternal={{true}}
              @models={{array association.mount association.secret_name}}
            >
              View secret
            </dd.Interactive>
            {{#if (has-capability @capabilities "update" pathKey="syncRemoveAssociation" params=@destination)}}
              <dd.Interactive
                data-test-association-action="unsync"
                @color="critical"
                {{on "click" (fn (mut this.secretToUnsync) association)}}
              >
                Unsync
              </dd.Interactive>
            {{/if}}
          </Hds::Dropdown>
        </Item.menu>
      </ListItem>
    {{/each}}
    <Hds::Pagination::Numbered
      @currentPage={{@associations.meta.currentPage}}
      @currentPageSize={{@associations.meta.pageSize}}
      @route="secrets.destinations.destination.secrets"
      @model={{@destination}}
      @showSizeSelector={{false}}
      @totalItems={{@associations.meta.filteredTotal}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  </div>
{{else}}
  <EmptyState
    @title="No synced secrets yet"
    @message="Select secrets from existing KV version 2 engines and sync them to the destination."
  >
    <Hds::Link::Standalone
      @text="Sync secrets"
      @icon="chevron-right"
      @iconPosition="trailing"
      @route="secrets.destinations.destination.sync"
    />
  </EmptyState>
{{/if}}

{{#if this.secretToUnsync}}
  <ConfirmModal
    @color="critical"
    @confirmMessage='The secret "{{this.secretToUnsync.secret_name}}" will be unsynced from this destination.'
    @onClose={{fn (mut this.secretToUnsync) null}}
    @onConfirm={{fn this.update this.secretToUnsync "remove"}}
  />
{{/if}}