{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<section class="section">
  <header class="acl-explainer">
    <p>
      ACL Tokens are associated with one or more policies or roles to grant specific capabilities. Users can use these to sign into, and operate, Nomad with the permissions laid out in their policies.
    </p>
    <div>
      {{#if (can "write token")}}
        <Hds::Button
          @text="Create Token"
          @icon="plus"
          @route="access-control.tokens.new"
          {{keyboard-shortcut
            pattern=(array "n" "t")
            action=(action this.goToNewToken)
            label="Create Token"
          }}
          data-test-create-token
        />
      {{else}}
        <Hds::Button
          @text="Create Token"
          @icon="plus"
          disabled
          data-test-disabled-create-token
        />
      {{/if}}
    </div>
  </header>
  {{#if this.model.tokens.length}}
    <Hds::Table @caption="A list of tokens for this cluster" class="acl-table"
      @model={{this.model.tokens}}
      @columns={{array
        (hash key="name" label="Name" isSortable=true)
        (hash key="type" label="Type" isSortable=true)
        (hash key="createTime" label="Created" isSortable=true)
        (hash key="expirationTime" label="Expires" isSortable=true)
        (hash key="roles" label="Roles")
        (hash key="policies" label="Policies")
        
        (hash key="delete" label="Delete")
      }}
      @sortBy="name"
    >
      <:body as |B|>
        <B.Tr
          {{keyboard-shortcut 
            enumerated=true
            action=(action "openToken" B.data)
          }}
          data-test-token-row
        >
          <B.Td data-test-token-name={{B.data.name}}>
            {{#if (eq B.data.id this.selfToken.id)}}
              <strong>{{B.data.name}}</strong>
            {{else}}
              <LinkTo @route="access-control.tokens.token" @model={{B.data.id}}>
                {{B.data.name}}
              </LinkTo>
            {{/if}}
          </B.Td>
          <B.Td data-test-token-type={{B.data.type}}>{{B.data.type}}</B.Td>
          <B.Td>{{moment-from-now B.data.createTime interval=1000}}</B.Td>
          <B.Td>
            {{#if B.data.expirationTime}}
              <Tooltip @text={{B.data.expirationTime}}>
                <span data-test-token-expiration-time class="{{if B.data.isExpired "has-text-danger"}}">{{moment-from-now B.data.expirationTime interval=1000}}</span>
              </Tooltip>
            {{else}}
              <span data-test-token-expiration-time>Never</span>
            {{/if}}
          </B.Td>

            <B.Td data-test-token-roles>
              <div class="tag-group">
                {{!--
                  We don't treat roles (roleNames) the same as policies, because Roles' names are currently
                  returning blank on the /tokens endpoint: https://github.com/hashicorp/nomad/issues/18451
                  TODO: when that's fixed, we can use an #each #let pattern like we do for policyNames.
                 --}}
                {{#each B.data.roles as |role|}}
                  {{#if role.name}}
                    <Hds::Tag @color="primary" @text="{{role.name}}" @route="access-control.roles.role" @model="{{role.id}}" />
                  {{/if}}
                {{else}}
                  {{#if (eq B.data.type "management")}}
                    Management Access
                  {{else}}
                    No Roles
                  {{/if}}
                {{/each}}
              </div>
            </B.Td>

            <B.Td data-test-token-policies>
              <div class="tag-group">
                {{#each B.data.policyNames as |policyName|}}
                  {{#let (find-by "name" policyName this.model.policies) as |policy|}}
                    {{#if policy}}
                      <Hds::Tag @color="primary" @text="{{policy.name}}" @route="access-control.policies.policy" @model="{{policy.name}}" />
                    {{else}}
                      <Hds::Tag
                        {{hds-tooltip "This policy has been deleted"}}
                        @text="{{policyName}}"
                      />
                    {{/if}}
                  {{/let}}
                {{else}}
                  {{#if (eq B.data.type "management")}}
                    Management Access
                  {{else}}
                    No Policies
                  {{/if}}
                {{/each}}
              </div>
            </B.Td>

            {{#if (can "destroy token")}}
              <B.Td data-test-delete-token>
                {{#if (eq B.data.id this.selfToken.id)}}
                  <Tooltip @text="Can't delete your own token" @isFullText={{true}}>
                    <Hds::Button @text="Delete" disabled @size="small" @color="critical" />
                  </Tooltip>
                {{else}}
                  <Hds::Button @text="Delete" @size="small" @color="critical"
                    {{on "click" (perform this.deleteToken B.data)}}
                  />
                {{/if}}
              </B.Td>
            {{/if}}

        </B.Tr>
      </:body>
    </Hds::Table>
  {{else}}
    <div class="empty-message">
      <h3 data-test-empty-policies-list-headline class="empty-message-headline">
        No Tokens
      </h3>
      <p class="empty-message-body">
        Get started by <LinkTo @route="access-control.policies.new">creating a new policy</LinkTo>
      </p>
    </div>
  {{/if}}
</section>
