{{ define "title" }}
  {{ $messageParts := splitN .Diff.Commit.Message "\n\n" 2 }}
  {{ index $messageParts 0 }} &middot; {{ .RepoInfo.FullName }}@{{ slice .Diff.Commit.This 0 7 }}
{{ end }}

{{ define "extrameta" }}
    {{ $messageParts := splitN .Diff.Commit.Message "\n\n" 2 }}
    {{ $title := printf "%s &middot; %s@%s" (index $messageParts 0) .RepoInfo.FullName (slice .Diff.Commit.This 0 7) }}
    {{ $url := printf "https://tangled.org/%s/commit/%s" .RepoInfo.FullName .Diff.Commit.This }}

    {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }}
{{ end }}


{{ define "repoContent" }}

{{ $repo := .RepoInfo.FullName }}
{{ $commit := .Diff.Commit }}

<section class="commit dark:text-white">
  <div id="commit-message">
    {{ $messageParts := splitN $commit.Message "\n\n" 2 }}
    <div>
      <p class="pb-2">{{ index $messageParts 0 }}</p>
      {{ if gt (len $messageParts) 1 }}
      <p class="mt-1 cursor-text pb-2 text-sm">{{ nl2br (index $messageParts 1) }}</p>
      {{ end }}
    </div>
  </div>

  <div class="flex flex-wrap items-center space-x-2 text-sm">
      <p class="flex flex-wrap items-center gap-1 text-gray-500 dark:text-gray-300">
          {{ template "attribution" . }}

          <span class="px-1 select-none before:content-['\00B7']"></span>
            {{ template "repo/fragments/time" $commit.Committer.When }}
          <span class="px-1 select-none before:content-['\00B7']"></span>

          <a href="/{{ $repo }}/commit/{{ $commit.This }}" class="no-underline hover:underline text-gray-500 dark:text-gray-300">{{ slice $commit.This 0 8 }}</a>

          {{ if $commit.Parent }}
            {{ i "arrow-left" "w-3 h-3 mx-1" }}
            <a href="/{{ $repo }}/commit/{{ $commit.Parent }}" class="no-underline hover:underline text-gray-500 dark:text-gray-300">{{ slice $commit.Parent 0 8 }}</a>
          {{ end }}
      </p>

      {{ if .VerifiedCommit.IsVerified $commit.This }}
      <div class="group relative inline-block text-sm">
          <div class="bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200 px-2 py-1 rounded cursor-pointer">
              <div class="flex items-center gap-2">
                  {{ i "shield-check" "w-4 h-4" }}
                  verified
              </div>
          </div>
          <div class="absolute z-[9999] hidden group-hover:block bg-white dark:bg-gray-900 text-sm text-black dark:text-white rounded-md shadow-md p-4 w-80 top-full mt-2">
              <div class="mb-1">This commit was signed with the committer's <span class="text-green-600 font-semibold">known signature</span>.</div>
              <div class="flex items-center gap-2 my-2">
                  {{ i "user" "w-4 h-4" }}
                  {{ $committerDid := index $.EmailToDid $commit.Committer.Email }}
                  {{ template "user/fragments/picHandleLink" $committerDid }}
              </div>
              <div class="my-1 pt-2 text-xs border-t border-gray-200 dark:border-gray-700">
                  <div class="text-gray-600 dark:text-gray-300">SSH Key Fingerprint:</div>
                  <div class="break-all">{{ .VerifiedCommit.Fingerprint $commit.This }}</div>
              </div>
          </div>
      </div>
      {{ end }}

      <div class="text-sm">
        {{ if $.Pipeline }}
          {{ template "repo/pipelines/fragments/pipelineSymbolLong" (dict "Pipeline" $.Pipeline "RepoInfo" $.RepoInfo) }}
        {{ end }}
      </div>
  </div>

</section>
{{end}}

{{ define "attribution" }}
  {{ $commit := .Diff.Commit }}
  {{ $showCommitter := true }}
  {{ if eq $commit.Author.Email $commit.Committer.Email }}
    {{ $showCommitter = false }}
  {{ end }}

  {{ if $showCommitter }}
    authored by {{ template "attributedUser" (list $commit.Author.Email $commit.Author.Name $.EmailToDid) }}
    {{ range $commit.CoAuthors }} 
      {{ template "attributedUser" (list .Email .Name $.EmailToDid) }}
    {{ end }}
    and committed by  {{ template "attributedUser" (list $commit.Committer.Email $commit.Committer.Name $.EmailToDid) }}
  {{ else }}
    {{ template "attributedUser" (list $commit.Author.Email $commit.Author.Name $.EmailToDid )}}
  {{ end }}
{{ end }}

{{ define "attributedUser" }}
  {{ $email := index . 0 }}
  {{ $name := index . 1 }}
  {{ $map := index . 2 }}
  {{ $did := index $map $email }}

  {{ if $did }}
    {{ template "user/fragments/picHandleLink" $did }}
  {{ else }}
    <span class="flex items-center gap-1">
      {{ placeholderAvatar "tiny" }}
      <a href="mailto:{{ $email }}" class="no-underline hover:underline text-gray-500 dark:text-gray-300">{{ $name }}</a>
    </span>
  {{ end }}
{{ end }}

{{ define "mainLayout" }}
  <div class="px-1 flex-grow col-span-full flex flex-col gap-4">
    {{ block "contentLayout" . }}
      {{ block "content" . }}{{ end }}
    {{ end }}

    {{ block "contentAfter" . }}{{ end }}
  </div>
{{ end }}



{{ define "contentAfter" }}
  {{ template "repo/fragments/diff" (list .Diff .DiffOpts) }}
{{end}}

