{{ define "title" }}{{ resolve .Card.UserDid }} · vouches {{ end }}

{{ define "profileContent" }}
  {{ $isSelf := and .LoggedInUser (eq .LoggedInUser.Did .Card.UserDid) }}
  <div id="all-vouches" class="md:col-span-8 order-2 md:order-2 flex flex-col gap-6">
    {{ if $isSelf }}{{ template "vouchSuggestions" . }}{{ end }}
    {{ block "vouchTimeline" . }}{{ end }}
    {{ if ge (len .Vouches) .Page.Limit }}
      {{ $handle := resolve .Card.UserDid }}
      {{ template "fragments/pagination" (dict
          "Page" .Page
          "TotalCount" (add (len .Vouches) .Page.Offset)
          "BasePath" (printf "/%s" $handle)
          "QueryParams" (queryParams "tab" "vouches")
      ) }}
    {{ end }}
  </div>
{{ end }}

{{ define "vouchTimeline" }}
  {{ $isSelf := and .LoggedInUser (eq .LoggedInUser.Did .Card.UserDid) }}
  {{ if not .LoggedInUser }}
    <div class="text-base text-gray-500 flex items-center justify-center italic p-12 border border-gray-200 dark:border-gray-700 rounded">
      <span>You need to <a href="/login" class="underline">log in</a> to view vouches.</span>
    </div>
  {{ else if not .Vouches }}
    {{ if not (and $isSelf .Suggestions) }}
      <div class="text-base text-gray-500 flex items-center justify-center italic p-12 border border-gray-200 dark:border-gray-700 rounded">
        <span>No vouches in your network yet.</span>
      </div>
    {{ end }}
  {{ else }}
    <div class="relative flex flex-col gap-6 ml-5 px-4 border border-transparent">
      <div class="absolute top-8 bottom-8 w-0.5 bg-gray-200 dark:bg-gray-700 z-0"></div>
      {{ range .Vouches }}
        {{ template "vouchTimelineItem" (list $.Card.UserDid $.LoggedInUser.Did .) }}
      {{ end }}
    </div>
  {{ end }}
{{ end }}

{{ define "vouchSuggestions" }}
  {{ if .Suggestions }}
    <div class="flex flex-col gap-2">
      {{ range .Suggestions }}
        <div class="flex items-center gap-4 border border-gray-200 dark:border-gray-700 rounded-sm p-4 bg-white dark:bg-gray-800">
          <img src="{{ tinyAvatar .Did.String }}" alt="" class="rounded-full size-10 border border-gray-300 dark:border-gray-700 shrink-0" />
          <div class="flex flex-col gap-0.5 min-w-0 flex-1">
            <a href="/{{ resolve .Did.String }}" class="font-medium hover:underline dark:text-white truncate">{{ resolve .Did.String }}</a>
            <span class="text-sm text-gray-500 dark:text-gray-400">{{ .Reason }}</span>
          </div>
          <div class="shrink-0 w-32">
            {{ template "user/fragments/vouch" (dict "VouchRelationship" .VouchRelationship) }}
          </div>
          <button
            hx-post="/vouch/skip?subject={{ .Did.String }}"
            hx-trigger="click"
            hx-disabled-elt="this"
            title="Skip suggestion"
            class="group hit-area hit-area-4 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 p-0.5"
          >
            {{ i "x" "size-4 block group-[.htmx-request]:hidden" }}
            {{ i "loader-circle" "size-4 animate-spin hidden group-[.htmx-request]:block" }}
          </button>
        </div>
      {{ end }}
    </div>
  {{ end }}
{{ end }}

{{ define "vouchTimelineItem" }}
  {{ $profileDid := index . 0 }}
  {{ $viewerDid := index . 1 }}
  {{ $v := index . 2 }}
  {{ $isOutgoing := eq $v.Did $profileDid }}
  {{ $isVouch := $v.IsVouch }}
  {{ $isSelf := eq $profileDid $viewerDid }}

  <div class="-ml-5 flex items-center gap-3 relative z-10">
    <div class="flex-shrink-0 flex items-center justify-center size-10 rounded-full
      {{ if $isVouch }}bg-green-200 dark:bg-green-800{{ else }}bg-red-200 dark:bg-red-800{{ end }}">
      {{ if $isVouch }}
        {{ if $isOutgoing }}
          {{ i "arrow-up-right" "size-5 text-green-500 dark:text-green-400" }}
        {{ else }}
          {{ i "arrow-down-left" "size-5 text-green-500 dark:text-green-400" }}
        {{ end }}
      {{ else }}
        {{ if $isOutgoing }}
          {{ i "arrow-up-right" "size-5 text-red-500 dark:text-red-400" }}
        {{ else }}
          {{ i "arrow-down-left" "size-5 text-red-500 dark:text-red-400" }}
        {{ end }}
      {{ end }}
    </div>

    <div class="flex flex-col gap-1">
      <div class="flex flex-wrap items-center gap-1 text-sm dark:text-white">
        {{ if and $isSelf $isOutgoing }}
          <span class="font-medium text-gray-700 dark:text-gray-300">you</span>
        {{ else }}
          <a href="/{{ resolve $v.Did.String }}" class="font-medium hover:underline">{{ resolve $v.Did.String }}</a>
        {{ end }}
        <span class="text-gray-500 dark:text-gray-400">
          {{ if $isVouch }}vouched for{{ else }}denounced{{ end }}
        </span>
        {{ if and $isSelf (not $isOutgoing) }}
          <span class="font-medium text-gray-700 dark:text-gray-300">you</span>
        {{ else }}
          <a href="/{{ resolve $v.SubjectDid.String }}" class="font-medium hover:underline">{{ resolve $v.SubjectDid.String }}</a>
        {{ end }}
        <span class="text-gray-400 dark:text-gray-500 text-xs">{{ relTimeFmt $v.CreatedAt }}</span>
      </div>
      {{ with $v.Reason }}
        <p class="text-gray-500 dark:text-gray-400">{{ . }}</p>
      {{ end }}
    </div>
  </div>
{{ end }}
