{{ define "timeline/fragments/timeline" }}
    <div class="py-4">
        <h3 class="text-xl font-bold dark:text-white flex items-center gap-4 px-4 pb-4">
          {{ i "gallery-vertical" "size-5 flex-shrink-0" }}
          Timeline
        </h3>

        <div class="flex flex-col gap-4">
          {{ range $i, $e := .Timeline }}
            <div class="relative">
              {{ if ne $i 0 }}
                <div class="absolute left-8 -top-4 w-px h-4 bg-gray-300 dark:bg-gray-600"></div>
              {{ end }}
              {{ with $e }}
                <div class="flex flex-col divide-y divide-gray-200 dark:divide-gray-700 border border-gray-200 dark:border-gray-700 rounded-sm">
                  {{ if .Repo }}
                    {{ template "timeline/fragments/repoEvent" (list $ .) }}
                  {{ else if .RepoStar }}
                    {{ template "timeline/fragments/starEvent" (list $ .) }}
                  {{ else if .Follow }}
                    {{ template "timeline/fragments/followEvent" (list $ .) }}
                  {{ end }}
                </div>
              {{ end }}
            </div>
          {{ end }}
        </div>
    </div>
{{ end }}

{{ define "timeline/fragments/repoEvent" }}
  {{ $root := index . 0 }}
  {{ $event := index . 1 }}
  {{ $repo := $event.Repo }}
  {{ $source := $event.Source }}
  {{ $userHandle := resolve $repo.Did }}
    <div class="pl-6 py-2 bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 flex flex-wrap items-center gap-2 text-sm">
      {{ template "user/fragments/picHandleLink" $repo.Did }}
      {{ with $source }}
        {{ $sourceDid := resolve .Did }}
        forked
        <a href="/{{ $sourceDid }}/{{ .Name }}"class="no-underline hover:underline">
            {{ $sourceDid }}/{{ .Name }}
        </a>
        to
        <a href="/{{ $userHandle }}/{{ $repo.Name }}" class="no-underline hover:underline">{{ $repo.Name }}</a>
      {{ else }}
        created
        <a href="/{{ $userHandle }}/{{ $repo.Name }}" class="no-underline hover:underline">
          {{ $repo.Name }}
        </a>
      {{ end }}
      <span class="text-gray-700 dark:text-gray-400 text-xs">{{ template "repo/fragments/time" $repo.Created }}</span>
    </div>
  {{ with $repo }}
    {{ template "user/fragments/repoCard" (list $root . true true (dict "IsStarred" $event.IsStarred "SubjectAt" .RepoAt "StarCount" $event.StarCount)) }}
  {{ end }}
{{ end }}

{{ define "timeline/fragments/starEvent" }}
  {{ $root := index . 0 }}
  {{ $event := index . 1 }}
  {{ $star := $event.RepoStar }}
  {{ with $star }}
    {{ $starrerHandle := resolve .Did }}
    {{ $repoOwnerHandle :=  resolve .Repo.Did }}
    <div class="pl-6 py-2 bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 flex flex-wrap items-center gap-2 text-sm">
        {{ template "user/fragments/picHandleLink" $starrerHandle }}
        starred
        <a href="/{{ $repoOwnerHandle }}/{{ .Repo.Name }}" class="no-underline hover:underline">
          {{ $repoOwnerHandle | truncateAt30 }}/{{ .Repo.Name }}
        </a>
        <span class="text-gray-700 dark:text-gray-400 text-xs">{{ template "repo/fragments/time" .Created }}</span>
    </div>
    {{ with .Repo }}
      {{ template "user/fragments/repoCard" (list $root . true true (dict "IsStarred" $event.IsStarred "SubjectAt" .RepoAt "StarCount" $event.StarCount)) }}
    {{ end }}
  {{ end }}
{{ end }}

{{ define "timeline/fragments/followEvent" }}
  {{ $root := index . 0 }}
  {{ $event := index . 1 }}
  {{ $follow := $event.Follow }}
  {{ $profile := $event.Profile }}
  {{ $followStats := $event.FollowStats }}
  {{ $followStatus := $event.FollowStatus }}

  {{ $userHandle :=  resolve $follow.UserDid }}
  {{ $subjectHandle :=  resolve $follow.SubjectDid }}
  <div class="pl-6 py-2 bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-300 flex flex-wrap items-center gap-2 text-sm">
      {{ template "user/fragments/picHandleLink" $userHandle }}
      followed
      {{ template "user/fragments/picHandleLink" $subjectHandle }}
      <span class="text-gray-700 dark:text-gray-400 text-xs">{{ template "repo/fragments/time" $follow.FollowedAt }}</span>
  </div>
  {{ template "user/fragments/followCard" 
      (dict
          "LoggedInUser" $root.LoggedInUser
          "UserDid" $follow.SubjectDid
          "Profile" $profile
          "FollowStatus" $followStatus
          "FollowersCount" $followStats.Followers
          "FollowingCount" $followStats.Following) }}
{{ end }}
