{{ define "repo/pulls/fragments/pullActions" }}
  {{ $lastIdx := sub (len .Pull.Submissions) 1 }}
  {{ $roundNumber := .RoundNumber }}
  {{ $stack := .Stack }}

  {{ $totalPulls := sub 0 1 }}
  {{ $below := sub 0 1 }}
  {{ $stackCount := "" }}
  {{ if (gt (len .Stack) 1) }}
    {{ $totalPulls = len $stack }}
    {{ $below = $stack.Below .Pull }}
    {{ $mergeable := len $below.Mergeable }}
    {{ $stackCount = printf "%d/%d" $mergeable $totalPulls }}
  {{ end }}

  {{ $isPushAllowed := .RepoInfo.Roles.IsPushAllowed }}
  {{ $isMerged := .Pull.State.IsMerged }}
  {{ $isClosed := .Pull.State.IsClosed }}
  {{ $isOpen := .Pull.State.IsOpen }}
  {{ $isConflicted := and .MergeCheck (or .MergeCheck.Error .MergeCheck.IsConflicted) }}
  {{ $isPullAuthor := and .LoggedInUser (eq .LoggedInUser.Did .Pull.OwnerDid) }}
  {{ $isLastRound := eq $roundNumber $lastIdx }}
  {{ $isSameRepoBranch := .Pull.IsBranchBased }}
  {{ $isUpToDate := .ResubmitCheck.No }}
  <div id="actions-{{$roundNumber}}" hx-target="this" class="flex flex-wrap gap-2 relative p-2">
    <button 
      hx-get="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/round/{{ $roundNumber }}/comment"
      class="btn-flat p-2 flex items-center gap-2 no-underline hover:no-underline group">
        {{ i "message-square-plus" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        Comment
    </button>
    {{ if .BranchDeleteStatus }}
      <button 
        hx-delete="/{{ resolve .BranchDeleteStatus.Repo.Did }}/{{ .BranchDeleteStatus.Repo.Slug }}/branches"
        hx-vals='{"branch": "{{ .BranchDeleteStatus.Branch }}" }'
        hx-swap="none"
        class="btn-flat p-2 flex items-center gap-2 no-underline hover:no-underline group text-red-500 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300">
          {{ i "git-branch" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
          {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
          Delete branch
      </button>
    {{ end }}
    {{ if and $isPushAllowed $isOpen $isLastRound }}
      <button 
        hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/merge"
        hx-swap="none"
        hx-confirm="Are you sure you want to merge pull #{{ .Pull.PullId }} into the `{{ .Pull.TargetBranch }}` branch?"
        class="btn-flat p-2 flex items-center gap-2 group"
        {{ if $isConflicted }}disabled{{ end }}
      >
        {{ i "git-merge" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        Merge{{if $stackCount}} {{$stackCount}}{{end}}
      </button>
    {{ end }}

    {{ if and $isPullAuthor $isOpen $isLastRound }}
      <button id="resubmitBtn"
          {{ if not .Pull.IsPatchBased }}
            hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/resubmit"
            hx-swap="none"
          {{ else }}
            hx-get="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/resubmit"
          {{ end }}

          hx-disabled-elt="#resubmitBtn"
          class="btn-flat p-2 flex items-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed group"

          {{ if $isUpToDate }}
          title="Update this branch to resubmit this pull request"
          disabled
          {{ else }}
          title="Resubmit this pull request"
          {{ end }}
          >
          {{ i "rotate-ccw" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
          {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
          Resubmit
      </button>
    {{ end }}

    {{ if and (or $isPullAuthor $isPushAllowed) $isOpen $isLastRound }}
    <button 
      hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/close"
      hx-swap="none"
      class="btn-flat p-2 flex items-center gap-2 group">
        {{ i "ban" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        Close
    </button>
    {{ end }}

    {{ if and (or $isPullAuthor $isPushAllowed) $isClosed $isLastRound }}
    <button 
      hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/reopen"
      hx-swap="none"
      class="btn-flat p-2 flex items-center gap-2 group">
        {{ i "refresh-ccw-dot" "w-4 h-4 inline group-[.htmx-request]:hidden" }}
        {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
        Reopen
    </button>
    {{ end }}
  </div>
{{ end }}
