{{ define "repo/pulls/fragments/pullActions" }}
  {{ $lastIdx := .Pull.LatestVersionNumber }}
  {{ $roundNumber := .RoundNumber }}
  {{ $loading := .Loading }}

  {{ $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 }}
  {{ $isUpToDate := .ResubmitCheck.No }}
  <div id="actions-{{$roundNumber}}" hx-target="this" class="{{ if .LoggedInUser }}flex flex-wrap gap-2 relative p-2{{ else }}hidden{{ end }}"
    {{ if $loading }}
      hx-get="/{{ .Pull.RepoDid }}/pulls/{{ .Pull.PullId }}/{{ $roundNumber }}/_/actions"
      hx-trigger="load"
      hx-swap="outerHTML"
    {{ end }}
  >
    {{ if .LoggedInUser }}
    <button
      hx-get="/{{ .Pull.RepoDid }}/pulls/{{ .Pull.PullId }}/{{ $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>
    {{ end }}
    {{ if and (not $loading) .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="/{{ .Pull.RepoDid }}/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 or $loading $isConflicted }}disabled{{ end }}
      >
        {{ if $loading }}
          {{ i "loader-circle" "w-4 h-4 animate-spin" }}
        {{ else }}
          {{ 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" }}
        {{ end }}
        Merge
      </button>
    {{ end }}

    {{ if and $isPullAuthor $isOpen $isLastRound (not .ResubmitCheck.Unknown) }}
      <button id="resubmitBtn"
          hx-post="/{{ .Pull.RepoDid }}/pulls/{{ .Pull.PullId }}/resubmit"
          hx-swap="none"

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

          {{ if $loading }}
          title="Checking for updates…"
          disabled
          {{ else if $isUpToDate }}
          title="Update this branch to resubmit this pull request"
          disabled
          {{ else }}
          title="Resubmit this pull request"
          {{ end }}
          >
          {{ if $loading }}
            {{ i "loader-circle" "w-4 h-4 animate-spin" }}
          {{ else }}
            {{ 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" }}
          {{ end }}
          Resubmit
      </button>
    {{ end }}

    {{ if and (or $isPullAuthor $isPushAllowed) $isOpen $isLastRound }}
    <button
      hx-post="/{{ .Pull.RepoDid }}/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="/{{ .Pull.RepoDid }}/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>

  {{ if and (not $loading) $isLastRound }}
    <div id="mergecheck-banner" hx-swap-oob="innerHTML">{{ template "mergeCheck" . }}</div>
    <div id="resubmit-banner" hx-swap-oob="innerHTML">{{ template "resubmitStatus" . }}</div>
  {{ end }}
{{ end }}

{{ define "mergeCheck" }}
  {{ $isOpen := .Pull.State.IsOpen }}
  {{ if and $isOpen .MergeCheck .MergeCheck.Error }}
    <div class="flex items-center gap-2 text-red-600 dark:text-red-500">
      {{ i "triangle-alert" "w-4 h-4" }}
      <span class="font-medium">{{ .MergeCheck.Error }}</span>
    </div>
  {{ else if and $isOpen .MergeCheck .MergeCheck.IsConflicted }}
    <details class="group/conflict">
      <summary class="flex items-center justify-between cursor-pointer list-none">
        <div class="flex items-center gap-2 text-red-600 dark:text-red-500">
          {{ i "triangle-alert" "w-4 h-4" }}
          <span class="font-medium">Merge conflicts detected</span>
          <div class="text-sm text-gray-500 dark:text-gray-400">
            <span class="group-open/conflict:hidden inline">Expand</span>
            <span class="hidden group-open/conflict:inline">Collapse</span>
          </div>
        </div>
      </summary>
      {{ if gt (len .MergeCheck.Conflicts) 0 }}
        <ul class="space-y-1 mt-2 overflow-x-auto">
          {{ range .MergeCheck.Conflicts }}
            {{ if .Filename }}
              <li class="flex items-center whitespace-nowrap">
                {{ i "file-warning" "inline-flex w-4 h-4 mr-1.5 text-red-600 dark:text-red-500 flex-shrink-0" }}
                <span class="font-mono">{{ .Filename }}</span>
              </li>
            {{ else if .Reason }}
            <li class="flex items-center whitespace-nowrap">
              {{ i "file-warning" "w-4 h-4 mr-1.5 text-red-600 dark:text-red-500 " }}
              <span>{{.Reason}}</span>
            </li>
            {{ end }}
          {{ end }}
        </ul>
      {{ end }}
    </details>
  {{ else if and $isOpen .MergeCheck }}
    <div class="flex items-center gap-2 text-green-600 dark:text-green-500">
      {{ i "check" "w-4 h-4" }}
      <span class="font-medium">No conflicts, ready to merge</span>
    </div>
  {{ end }}
{{ end }}

{{ define "resubmitStatus" }}
  {{ if .ResubmitCheck.Yes }}
  <div class="bg-amber-50 dark:bg-amber-900 border border-amber-500 rounded shadow-sm px-6 py-2 relative">
    <div class="flex items-center gap-2 text-amber-500 dark:text-amber-300">
      {{ i "triangle-alert" "w-4 h-4" }}
      <span class="font-medium">This branch has been updated, consider resubmitting</span>
    </div>
  </div>
  {{ end }}
{{ end }}
