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

<Page::Header @title={{this.title}}>
  <:breadcrumbs>
    <Page::Breadcrumbs @breadcrumbs={{this.breadcrumbs}} />
  </:breadcrumbs>
</Page::Header>

{{#if this.isDistributing}}
  <Keymgmt::Distribute @backend={{@model.backend}} @key={{@model.id}} @onClose={{fn (mut this.isDistributing) false}} />
{{else}}
  {{#if (eq this.mode "show")}}
    <div class="tabs-container box is-sideless is-fullwidth is-paddingless is-marginless" data-test-keymgmt-key-toolbar>
      <nav class="tabs" aria-label="navigation for managing keys">
        <ul>
          <li class={{if (not-eq @tab "versions") "active"}}>
            <LinkTo
              @route="vault.cluster.secrets.backend.show"
              @model={{@model.id}}
              @query={{hash tab=""}}
              data-test-tab="Details"
            >
              Details
            </LinkTo>
          </li>
          <li class={{if (eq @tab "versions") "active"}}>
            <LinkTo
              @route="vault.cluster.secrets.backend.show"
              @model={{@model.id}}
              @query={{hash tab="versions"}}
              data-test-tab="Versions"
            >
              Versions
            </LinkTo>
          </li>
        </ul>
      </nav>
    </div>
    <Toolbar>
      <ToolbarActions>
        {{#unless @model.distribution}}
          <Hds::Button
            @text="Distribute key"
            @color="secondary"
            class="toolbar-button"
            {{on "click" (fn (mut this.isDistributing) true)}}
            data-test-keymgmt-key-distribute
          />
        {{/unless}}
        {{#if @model.canDelete}}
          <Hds::Button
            @text="Destroy key"
            @color="secondary"
            class="toolbar-button"
            disabled={{not @model.deletionAllowed}}
            {{on "click" (fn (mut this.isDeleteModalOpen) true)}}
            data-test-keymgmt-key-destroy
          />
        {{/if}}
        {{#if @model.provider}}
          <ConfirmAction
            class="toolbar-button"
            @buttonText="Remove key"
            @buttonColor="secondary"
            @onConfirmAction={{perform this.removeKey}}
            @confirmTitle="Remove this key?"
            @confirmMessage="This will remove all versions of the key from the KMS provider. The key will stay in Vault."
            @isRunning={{this.removeKey.isRunning}}
            data-test-keymgmt-key-remove
          />
        {{/if}}
        {{#if (or @model.canDelete @model.provider)}}
          <div class="toolbar-separator"></div>
        {{/if}}
        <ConfirmAction
          @buttonText="Rotate key"
          class="toolbar-button"
          @buttonColor="secondary"
          @modalColor="warning"
          @onConfirmAction={{perform this.rotateKey}}
          @confirmTitle="Rotate this key?"
          @confirmMessage="After rotation, all key actions will default to using the newest version of the key."
          @color="warning"
          @isRunning={{this.rotateKey.isRunning}}
          data-test-keymgmt-key-rotate
        />
        {{#if @model.canEdit}}
          <ToolbarSecretLink
            @secret={{@model.id}}
            @mode="edit"
            @replace={{true}}
            @queryParams={{hash itemType="key"}}
            @data-test-edit-link={{true}}
          >
            Edit key
          </ToolbarSecretLink>
        {{/if}}
      </ToolbarActions>
    </Toolbar>
  {{/if}}

  {{#if this.isMutable}}
    <form {{on "submit" (perform this.saveKey)}}>
      <div class="box is-sideless is-fullwidth is-marginless">
        {{#let (if (eq @mode "create") "createFields" "updateFields") as |fieldsKey|}}
          {{#each (get @model fieldsKey) as |attr|}}
            <FormField data-test-field={{true}} @attr={{attr}} @model={{@model}} />
          {{/each}}
          <div class="field is-grouped box is-fullwidth is-bottomless">
            <Hds::ButtonSet>
              <Hds::Button
                @text={{if this.isCreating "Create key" "Update"}}
                @icon={{if this.saveTask.isRunning "loading"}}
                type="submit"
                disabled={{this.saveTask.isRunning}}
                data-test-keymgmt-key-submit
              />
              <Hds::Button
                @text="Cancel"
                @color="secondary"
                @route={{if this.isCreating @root.path "vault.cluster.secrets.backend.show"}}
                @model={{if this.isCreating @root.model @model.id}}
                @query={{unless this.isCreating (hash itemType="key") (hash itemType="")}}
                disabled={{this.savekey.isRunning}}
                data-test-keymgmt-key-cancel
              />
            </Hds::ButtonSet>
          </div>
        {{/let}}
      </div>
    </form>
  {{else if (eq @tab "versions")}}
    {{#each @model.versions as |version|}}
      <div class="list-item-row" data-test-keymgmt-key-version>
        <div class="columns is-mobile">
          <div class="column is-3 has-text-weight-bold">
            <Icon @name="history" class="has-text-grey-light" />
            <span>Version {{version.id}}</span>
          </div>
          <div class="column is-3 has-text-grey">
            {{date-from-now version.creation_time addSuffix=true}}
          </div>
          <div class="column is-6 is-flex-center">
            {{#if (eq @model.minEnabledVersion version.id)}}
              <Icon @name="check-circle-fill" class="has-text-success" />
              <span data-test-keymgmt-key-current-min>Current mininum enabled version</span>
            {{/if}}
          </div>
        </div>
      </div>
    {{/each}}
  {{else}}
    <div class="has-top-margin-xl has-bottom-margin-s">
      <h2 class="title is-5 has-border-bottom-light has-bottom-padding-s">Key details</h2>
      {{#each @model.showFields as |attr|}}
        <InfoTableRow
          @alwaysRender={{true}}
          @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
          @value={{get @model attr.name}}
          @defaultShown={{attr.options.defaultShown}}
          @formatDate={{if (eq attr.type "date") "MMM d yyyy, h:mm:ss aaa"}}
        />
      {{/each}}
    </div>
    <div class="has-top-margin-xl has-bottom-margin-s">
      <h2 class="title is-5 {{if @model.distribution 'has-border-bottom-light' 'is-borderless'}}">
        Distribution details
      </h2>
      {{#if @model.provider.permissionsError}}
        <Hds::ApplicationState class="top-padding-32 bottom-padding-32 is-marginless" as |A|>
          <A.Header
            @title="You are not authorized"
            @titleTag="h2"
            @errorCode="403"
            @icon="minus-circle"
            data-test-empty-state-title
          />
          <A.Body
            @text={{concat
              "You must be granted permissions to see whether this key is distributed. Ask your administrator if you think you should have access to LIST /"
              @model.backend
              "/key/"
              @model.name
              "/kms."
            }}
            data-test-empty-state-message
          />
        </Hds::ApplicationState>
      {{else if (is-empty-value @model.provider)}}
        <Hds::ApplicationState
          data-test-keymgmt-dist-empty-state
          class="top-padding-32 bottom-padding-32 is-marginless"
          as |A|
        >
          <A.Header @title="Key not distributed" @titleTag="h2" data-test-empty-state-title />
          <A.Body
            @text="When this key is distributed to a destination, those details will appear here."
            data-test-empty-state-message
          />
          {{#if @model.canListProviders}}
            <A.Footer data-test-empty-state-actions as |F|>
              <F.Button
                @text="Distribute key"
                @icon="chevron-right"
                @iconPosition="trailing"
                @color="tertiary"
                {{on "click" (fn (mut this.isDistributing) true)}}
              />
            </A.Footer>
          {{/if}}
        </Hds::ApplicationState>
      {{else}}
        <InfoTableRow @label="Distributed" @value={{@model.provider}}>
          <LinkTo @route="vault.cluster.secrets.backend.show" @model={{@model.provider}} @query={{hash itemType="provider"}}>
            <Icon @name="check-circle-fill" class="has-text-success" />{{@model.provider}}
          </LinkTo>
        </InfoTableRow>
        {{#if @model.distribution}}
          {{#each @model.distFields as |attr|}}
            <InfoTableRow
              @alwaysRender={{true}}
              @label={{capitalize (or attr.label (humanize (dasherize attr.name)))}}
              @value={{if
                (eq attr.name "protection")
                (uppercase (get @model.distribution attr.name))
                (get @model.distribution attr.name)
              }}
              @defaultShown={{attr.defaultShown}}
              @helperText={{attr.subText}}
              @formatDate={{if (eq attr.type "date") "MMM d yyyy, h:mm:ss aaa"}}
            />
          {{/each}}
        {{else}}
          <Hds::ApplicationState class="top-padding-32 bottom-padding-32 is-marginless" as |A|>
            <A.Header
              @title="You are not authorized"
              @titleTag="h2"
              @errorCode="403"
              @icon="minus-circle"
              data-test-empty-state-title
            />
            <A.Body
              @text={{concat
                "You must be granted permissions to view distribution details for this key. Ask your administrator if you think you should have access to GET /"
                @model.backend
                "/key/"
                @model.name
                "/kms."
              }}
              data-test-empty-state-message
            />
          </Hds::ApplicationState>
        {{/if}}
      {{/if}}
    </div>
  {{/if}}
{{/if}}

<ConfirmationModal
  @title="Destroy key?"
  @onClose={{fn (mut this.isDeleteModalOpen) false}}
  @isActive={{this.isDeleteModalOpen}}
  @confirmText={{@model.name}}
  @toConfirmMsg="deleting the key"
  @onConfirm={{fn this.deleteKey @model.id}}
>
  <p>
    Destroying the
    <strong>{{@model.name}}</strong>
    key means that the underlying data will be lost and the key will become unusable for cryptographic operations. It is
    unrecoverable.
  </p>
  <MessageError @model={{this.model}} @errorMessage={{this.error}} />
</ConfirmationModal>