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

<Hds::PageHeader class="job-page-header" as |PH|>
  <PH.Title data-test-job-name>
    {{or this.title this.job.name}}
    {{#if @job.meta.structured.root.children.pack}}
      <span data-test-pack-tag class="tag is-hollow">
        <Hds::Icon @name="box" @color="faint" />
        <span>Pack</span>
      </span>
    {{/if}}
    {{yield}}
  </PH.Title>
  {{#if this.description}}
    <PH.Description data-test-job-description>
      {{this.description}}
    </PH.Description>
  {{/if}}
  {{#if this.links}}
    <PH.Generic>
      <div class="job-ui-links" data-test-job-links>
        {{#each this.links as |link|}}
          <Hds::Button @color="secondary" @isInline={{true}} @text={{link.Label}} @icon="external-link" @iconPosition="trailing" @href={{link.Url}} />
        {{/each}}
      </div>
    </PH.Generic>
  {{/if}}
  <PH.Actions>
    {{#if (not (eq this.job.status "dead"))}}
      {{#if (can "exec allocation" namespace=this.job.namespace)}}
        {{#if (and this.job.actions.length this.job.allocations.length)}}
          <ActionsDropdown @actions={{this.job.actions}} />
        {{/if}}
      {{/if}}
      <Exec::OpenButton @job={{this.job}} />
      <TwoStepButton
        data-test-stop
        @alignRight={{true}}
        @idleText="Stop Job"
        @cancelText="Cancel"
        @confirmText="Yes, Stop Job"
        @confirmationMessage="Are you sure you want to stop this job?"
        @awaitingConfirmation={{this.stopJob.isRunning}}
        @onConfirm={{perform this.stopJob}}
        {{keyboard-shortcut
          label="Stop"
          pattern=(array "s" "t" "o" "p")
          action=(perform this.stopJob true)
        }} />
    {{else}}
      <TwoStepButton
        data-test-purge
        @alignRight={{true}}
        @idleText="Purge Job"
        @cancelText="Cancel"
        @confirmText="Yes, Purge Job"
        @confirmationMessage="Are you sure? You cannot undo this action."
        @awaitingConfirmation={{this.purgeJob.isRunning}}
        @onConfirm={{perform this.purgeJob}}
        {{keyboard-shortcut
          label="Purge"
          pattern=(array "p" "u" "r" "g" "e")
          action=(perform this.purgeJob)
        }}
        />
      {{!--
        1. If job.stopped is true, that means the job was manually stopped and can be restared. So we should show the "start" button.
        2. If job.stopped is false, but if job.status is "dead", that means the job has failed and can't be restarted necessarily. We should should check to see that there's a stable verison of the job to fall back to.
          2a. If there is a stable version, we should show a "Revert to last stable version" button
          2b. If there is no stable version, we should show an "Edit and resubmit" button
       --}}

      {{#if this.job.stopped}}
        <TwoStepButton
          data-test-start
          @alignRight={{true}}
          @idleText="Start Job"
          @cancelText="Cancel"
          @confirmText="Yes, Start Job"
          @confirmationMessage="Are you sure you want to start this job?"
          @awaitingConfirmation={{this.startJob.isRunning}}
          @onConfirm={{perform this.startJob}}
          {{keyboard-shortcut
            label="Start"
            pattern=(array "s" "t" "a" "r" "t")
            action=(perform this.startJob true)
          }}
        />
      {{else}}
        {{#if this.job.hasStableNonCurrentVersion}}
          <TwoStepButton
            data-test-revert
            @alignRight={{true}}
            @idleText="Revert to last stable version (v{{this.job.latestStableVersion.number}})"
            @cancelText="Cancel"
            @confirmText="Yes, Revert to last stable version"
            @confirmationMessage="Are you sure you want to revert to the last stable version?"
            @awaitingConfirmation={{this.revertTo.isRunning}}
            @onConfirm={{perform this.revertTo this.job.latestStableVersion}}
          />
        {{else if
          (and
            (not this.job.hasVersionStability)
            this.job.latestVersion
          )
        }}
          <TwoStepButton
            data-test-revert
            @alignRight={{true}}
            @idleText="Revert to last version (v{{this.job.latestVersion.number}})"
            @cancelText="Cancel"
            @confirmText="Yes, Revert to last version"
            @confirmationMessage="Are you sure you want to revert to the last version?"
            @awaitingConfirmation={{this.revertTo.isRunning}}
            @onConfirm={{perform this.revertTo this.job.latestVersion}}
          />
          <Hds::Button
            data-test-edit-and-resubmit
            @color="primary" @isInline={{true}} @text="Edit and Resubmit job" @route={{"jobs.job.definition" this.job.id}} @query={{hash isEditing=true}} />
        {{else}}
          <Hds::Button
            data-test-edit-and-resubmit
            {{hds-tooltip "This job has failed and has no stable previous version to fall back to. You can edit and resubmit the job to try again." options=(hash placement="bottom")}}
            @color="primary" @isInline={{true}} @text="Edit and Resubmit job" @route={{"jobs.job.definition" this.job.id}} @query={{hash isEditing=true}} />
        {{/if}}
      {{/if}}
    {{/if}}
  </PH.Actions>
</Hds::PageHeader>
