{{ define "title" }}
  {{ $messageParts := splitN .Diff.Commit.Message "\n\n" 2 }}
  {{ index $messageParts 0 }} &middot; {{ .RepoInfo.FullName }}@{{ slice .Diff.Commit.This 0 7 }} &middot; Tangled
{{ 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-col gap-2 text-sm pt-4">
    {{ $showCommitter := ne $commit.Author.Email $commit.Committer.Email }}
    <div class="flex flex-col gap-1 font-mono text-gray-500 dark:text-gray-300">
      <span class="inline-flex items-center flex-wrap">
        <span class="w-24 text-gray-400 dark:text-gray-500 select-none">author</span>
        {{ template "attributedUser" (list $commit.Author.Email $commit.Author.Name $.EmailToDid) }}
      </span>

      {{ range $commit.CoAuthors }}
      <span class="inline-flex items-center flex-wrap">
        <span class="w-24 text-gray-400 dark:text-gray-500 select-none">co-author</span>
        {{ template "attributedUser" (list .Email .Name $.EmailToDid) }}
      </span>
      {{ end }}

      {{ if $showCommitter }}
      <span class="inline-flex items-center flex-wrap">
        <span class="w-24 text-gray-400 dark:text-gray-500 select-none">committer</span>
        {{ template "attributedUser" (list $commit.Committer.Email $commit.Committer.Name $.EmailToDid) }}
      </span>
      {{ end }}

      <span class="inline-flex items-center flex-wrap">
        <span class="w-24 text-gray-400 dark:text-gray-500 select-none">date</span>
        <span class="inline-flex items-center flex-wrap gap-1">
          {{ template "repo/fragments/time" $commit.Committer.When }}
          <span class="text-gray-500 dark:text-gray-400">({{ $commit.Committer.When | longTimeFmt }})</span>
        </span>
      </span>

      <span class="inline-flex items-center flex-wrap">
        <span class="w-24 text-gray-400 dark:text-gray-500 select-none">commit</span>
        <a href="/{{ $repo }}/commit/{{ $commit.This }}" class="no-underline hover:underline break-all">
          <span class="md:hidden">{{ shortId $commit.This }}</span>
          <span class="hidden md:inline">{{ $commit.This }}</span>
        </a>
      </span>

      {{ if $commit.Parent }}
      <span class="inline-flex items-center flex-wrap">
        <span class="w-24 text-gray-400 dark:text-gray-500 select-none">parent</span>
        <a href="/{{ $repo }}/commit/{{ $commit.Parent }}" class="no-underline hover:underline break-all">
          <span class="md:hidden">{{ shortId $commit.Parent }}</span>
          <span class="hidden md:inline">{{ $commit.Parent }}</span>
        </a>
      </span>
      {{ end }}

      {{ if $commit.ChangeId }}
        <span class="inline-flex items-center flex-wrap">
          <span class="w-24 text-gray-400 dark:text-gray-500 select-none">change-id</span>
          <span class="break-all">
            <span class="md:hidden">{{ shortId $commit.ChangeId }}</span>
            <span class="hidden md:inline">{{ $commit.ChangeId }}</span>
          </span>
        </span>
      {{ end }}
    </div>

    {{ if or (.VerifiedCommit.IsVerified $commit.This) $.Pipeline }}
    <div class="flex flex-wrap items-center space-x-2">
      {{ 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>
    {{ end }}
  </div>

</section>
{{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}}
