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

<div class="box is-fullwidth is-bottomless is-sideless">
  <MessageError @model={{this.model}} />
  <div class="control-group-header {{if this.isSuccess 'is-success'}}">
    <p>
      <Icon @name={{if this.isSuccess "check-circle-fill" "lock-fill"}} />
      <strong data-test-banner-prefix>{{this.bannerPrefix}}</strong>
      <span data-test-banner-text>{{this.bannerText}}</span>
    </p>
  </div>
  {{#if
    (and
      this.model.approved
      (not this.currentUserHasAuthorized)
      (or (not this.model.requestEntity) this.currentUserIsRequesting)
    )
  }}
    <ControlGroupSuccess
      data-test-control-group-success
      @model={{this.model}}
      @controlGroupResponse={{this.controlGroupResponse}}
    />
  {{else}}
    <div class="control-group">
      <div data-test-requestor-text>
        {{#if this.model.requestEntity.canRead}}
          <LinkTo
            @route="vault.cluster.access.identity.show"
            @models={{array "entities" this.model.requestEntity.id "details"}}
          >
            {{this.requestorName}}
          </LinkTo>
        {{else}}
          {{this.requestorName}}
        {{/if}}
        {{#if this.currentUserIsRequesting}}are{{else}}is{{/if}}
        {{#if this.model.approved}}
          authorized to access
          <code>{{this.model.requestPath}}</code>
        {{else}}
          requesting access to
          <code>{{this.model.requestPath}}</code>
        {{/if}}
      </div>

      {{#if (or (not this.model.requestEntity) this.currentUserIsRequesting)}}
        <Hds::Alert
          @type="inline"
          @color={{if this.isSuccess "success" "warning"}}
          @icon={{false}}
          data-test-inline-alert
          as |A|
        >
          <A.Title>Accessor</A.Title>
          <A.Description>
            <Hds::Copy::Snippet
              @textToCopy={{this.model.id}}
              @color="secondary"
              @onError={{(fn
                (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger")
              )}}
              data-test-accessor-value
            />
          </A.Description>
        </Hds::Alert>
      {{/if}}

      <div class="authorizations" data-test-authorizations>
        {{#if (gt this.model.authorizations.length 0)}}
          <span class="has-text-success">
            <Icon @name="check-circle" />
          </span>
          Already approved by
          {{#each this.model.authorizations as |authorization index|}}
            {{~#if authorization.canRead~}}
              <LinkTo @route="vault.cluster.access.identity.show" @models={{array "entities" authorization.id "details"}}>
                {{authorization.name}}
              </LinkTo>
            {{~else~}}
              {{authorization.name}}
            {{~/if~}}{{#if (lt (inc index) this.model.authorizations.length)}},{{/if}}
          {{/each}}
        {{else}}
          <span class="has-text-grey">
            <Icon @name="check-circle" />
          </span>
          Awaiting authorization.
        {{/if}}
      </div>
    </div>
  {{/if}}

  {{#if this.controlGroupResponse.token}}
    <div class="has-bottom-margin-m has-top-margin-m" data-test-token>
      <p class="has-text-grey has-padding-xxs">
        We’ve saved your request token, but you may want to copy it just in case:
      </p>
      <Hds::Copy::Snippet
        data-test-token-value
        @textToCopy={{this.controlGroupResponse.token}}
        @color="secondary"
        @onError={{(fn (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger"))}}
      />
    </div>
  {{/if}}
  {{! template-lint-configure simple-unless "warn" }}
  {{#unless (and this.model.approved (or (not this.model.requestEntity) this.currentUserIsRequesting))}}
    <div class="field is-grouped box is-fullwidth is-bottomless">
      {{#if this.model.canAuthorize}}
        {{#if (or this.model.approved this.currentUserHasAuthorized)}}
          <Hds::Button
            @text="Back"
            @color="secondary"
            @route="vault.cluster.access.control-groups"
            @icon="chevron-left"
            @iconPosition="leading"
            data-test-back-button
          />
        {{else}}
          <Hds::Button
            @text="Authorize"
            @icon={{if this.authorize.isRunning "loading"}}
            {{on "click" (action (perform this.authorize))}}
            data-test-button="Authorize"
          />
        {{/if}}
      {{else}}
        <Hds::Button
          @text="Refresh"
          @icon={{if this.refresh.isRunning "loading"}}
          {{on "click" (action (perform this.refresh))}}
          data-test-button="Refresh"
        />
      {{/if}}
    </div>
  {{/unless}}
</div>