{# Social Feed Widget - Tabbed Bluesky/Mastodon #}
{% if (blueskyFeed and blueskyFeed.length) or (mastodonFeed and mastodonFeed.length) %}
{% set id = homepageConfig.identity if (homepageConfig and homepageConfig.identity) else {} %}
{% set socialLinks = id.social if (id.social is defined) else site.social %}

{% set blueskyProfileUrl = "" %}
{% set mastodonProfileUrl = "" %}
{% for link in socialLinks %}
  {% if not blueskyProfileUrl and (link.icon == "bluesky" or "bsky.app/profile/" in link.url) %}
    {% set blueskyProfileUrl = link.url %}
  {% endif %}
  {% if not mastodonProfileUrl and (link.icon == "mastodon" or "@" in link.url) %}
    {% set mastodonProfileUrl = link.url %}
  {% endif %}
{% endfor %}
{% if not blueskyProfileUrl and site.feeds.bluesky %}
  {% set blueskyProfileUrl = "https://bsky.app/profile/" + site.feeds.bluesky %}
{% endif %}
{% if not mastodonProfileUrl and site.feeds.mastodon.instance and site.feeds.mastodon.username %}
  {% set mastodonProfileUrl = "https://" + site.feeds.mastodon.instance + "/@" + site.feeds.mastodon.username %}
{% endif %}

<is-land on:visible>
{% set defaultSocialTab = "mastodon" if mastodonFeed and mastodonFeed.length else "bluesky" %}
<div class="widget" x-data="{ activeTab: '{{ defaultSocialTab }}' }">
  <h3 class="widget-title">Social Activity</h3>

  {# Tab buttons #}
  <div class="flex gap-1 mb-4 border-b border-surface-200 dark:border-surface-700" role="tablist" aria-label="Social feeds">
    {% if blueskyFeed and blueskyFeed.length %}
    <button
      @click="activeTab = 'bluesky'"
      :class="activeTab === 'bluesky' ? 'border-b-2 border-[#0085ff] text-[#0057c7]' : 'text-surface-600 dark:text-surface-400 hover:text-surface-700 dark:hover:text-surface-300'"
      :aria-selected="(activeTab === 'bluesky').toString()"
      role="tab" id="social-tab-bluesky" aria-controls="social-panel-bluesky"
      class="flex items-center gap-1.5 px-3 py-2 text-sm font-medium transition-colors -mb-px"
    >
      <svg class="w-4 h-4 text-[#0085ff]" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true">
        <path d="M12 10.8c-1.087-2.114-4.046-6.053-6.798-7.995C2.566.944 1.561 1.266.902 1.565.139 1.908 0 3.08 0 3.768c0 .69.378 5.65.624 6.479.815 2.736 3.713 3.66 6.383 3.364.136-.02.275-.039.415-.056-.138.022-.276.04-.415.056-3.912.58-7.387 2.005-2.83 7.078 5.013 5.19 6.87-1.113 7.823-4.308.953 3.195 2.05 9.271 7.733 4.308 4.267-4.308 1.172-6.498-2.74-7.078a8.741 8.741 0 0 1-.415-.056c.14.017.279.036.415.056 2.67.297 5.568-.628 6.383-3.364.246-.828.624-5.79.624-6.478 0-.69-.139-1.861-.902-2.206-.659-.298-1.664-.62-4.3 1.24C16.046 4.748 13.087 8.687 12 10.8Z"></path>
      </svg>
      Bluesky
    </button>
    {% endif %}
    {% if mastodonFeed and mastodonFeed.length %}
    <button
      @click="activeTab = 'mastodon'"
      :class="activeTab === 'mastodon' ? 'border-b-2 border-[#a730b8] text-[#a730b8]' : 'text-surface-600 dark:text-surface-400 hover:text-surface-700 dark:hover:text-surface-300'"
      :aria-selected="(activeTab === 'mastodon').toString()"
      role="tab" id="social-tab-mastodon" aria-controls="social-panel-mastodon"
      class="flex items-center gap-1.5 px-3 py-2 text-sm font-medium transition-colors -mb-px"
    >
      <svg class="w-4 h-4 text-[#6364ff]" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true">
        <path d="M23.268 5.313c-.35-2.578-2.617-4.61-5.304-5.004C17.51.242 15.792 0 11.813 0h-.03c-3.98 0-4.835.242-5.288.309C3.882.692 1.496 2.518.917 5.127.64 6.412.61 7.837.661 9.143c.074 1.874.088 3.745.26 5.611.118 1.24.325 2.47.62 3.68.55 2.237 2.777 4.098 4.96 4.857 2.336.792 4.849.923 7.256.38.265-.061.527-.132.786-.213.585-.184 1.27-.39 1.774-.753a.057.057 0 0 0 .023-.043v-1.809a.052.052 0 0 0-.02-.041.053.053 0 0 0-.046-.01 20.282 20.282 0 0 1-4.709.545c-2.73 0-3.463-1.284-3.674-1.818a5.593 5.593 0 0 1-.319-1.433.053.053 0 0 1 .066-.054c1.517.363 3.072.546 4.632.546.376 0 .75 0 1.125-.01 1.57-.044 3.224-.124 4.768-.422.038-.008.077-.015.11-.024 2.435-.464 4.753-1.92 4.989-5.604.008-.145.03-1.52.03-1.67.002-.512.167-3.63-.024-5.545zm-3.748 9.195h-2.561V8.29c0-1.309-.55-1.976-1.67-1.976-1.23 0-1.846.79-1.846 2.35v3.403h-2.546V8.663c0-1.56-.617-2.35-1.848-2.35-1.112 0-1.668.668-1.668 1.977v6.218H4.822V8.102c0-1.31.337-2.35 1.011-3.12.696-.77 1.608-1.164 2.74-1.164 1.311 0 2.302.5 2.962 1.498l.638 1.06.638-1.06c.66-.999 1.65-1.498 2.96-1.498 1.13 0 2.043.395 2.74 1.164.675.77 1.012 1.81 1.012 3.12v6.406z"></path>
      </svg>
      Mastodon
    </button>
    {% endif %}
  </div>

  {# Bluesky Tab Content #}
  {% if blueskyFeed and blueskyFeed.length %}
  <div x-show="activeTab === 'bluesky'" x-cloak role="tabpanel" id="social-panel-bluesky" aria-labelledby="social-tab-bluesky">
    <ul class="space-y-3">
      {% for post in blueskyFeed | head(5) %}
      <li class="border-b border-surface-200 dark:border-surface-700 pb-3 last:border-0">
        <a href="{{ post.url }}" target="_blank" rel="noopener" class="block group">
          <p class="text-sm text-surface-700 dark:text-surface-300 group-hover:text-[#0085ff] transition-colors">
            {{ post.text | truncate(140) }}
          </p>
          <div class="flex items-center gap-3 mt-2 text-xs text-surface-600 dark:text-surface-400">
            <time class="font-mono" datetime="{{ post.createdAt }}">{{ post.createdAt | date("MMM d, yyyy") }}</time>
            {% if post.likeCount > 0 %}
            <span class="flex items-center gap-1">
              <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 24 24"><path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/></svg>
              {{ post.likeCount }}
            </span>
            {% endif %}
          </div>
        </a>
      </li>
      {% endfor %}
    </ul>
    {% if blueskyProfileUrl %}
    <a href="{{ blueskyProfileUrl }}" target="_blank" rel="noopener" class="text-sm text-[#0057c7] dark:text-[#0085ff] hover:underline mt-3 inline-flex items-center gap-1">
      View on Bluesky
      <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"/></svg>
    </a>
    {% endif %}
  </div>
  {% endif %}

  {# Mastodon Tab Content #}
  {% if mastodonFeed and mastodonFeed.length %}
  <div x-show="activeTab === 'mastodon'" x-cloak role="tabpanel" id="social-panel-mastodon" aria-labelledby="social-tab-mastodon">
    <ul class="space-y-3">
      {% for post in mastodonFeed | head(5) %}
      <li class="border-b border-surface-200 dark:border-surface-700 pb-3 last:border-0">
        <a href="{{ post.url }}" target="_blank" rel="noopener" class="block group">
          {% if post.isBoost %}
          <p class="text-xs text-surface-500 dark:text-surface-400 mb-1">boosted from @{{ post.author.username }}</p>
          {% endif %}
          <p class="text-sm text-surface-700 dark:text-surface-300 group-hover:text-[#a730b8] transition-colors">
            {{ post.text | truncate(140) }}
          </p>
          <div class="flex items-center gap-3 mt-2 text-xs text-surface-600 dark:text-surface-400">
            <time class="font-mono" datetime="{{ post.createdAt }}">{{ post.createdAt | date("MMM d, yyyy") }}</time>
            {% if post.favouritesCount > 0 %}
            <span class="flex items-center gap-1">
              <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 24 24"><path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/></svg>
              {{ post.favouritesCount }}
            </span>
            {% endif %}
          </div>
        </a>
      </li>
      {% endfor %}
    </ul>
    {% if mastodonProfileUrl %}
    <a href="{{ mastodonProfileUrl }}" target="_blank" rel="noopener" class="text-sm text-[#a730b8] hover:underline mt-3 inline-flex items-center gap-1">
      View on Mastodon
      <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"/></svg>
    </a>
    {% endif %}
  </div>
  {% endif %}
</div>
</is-land>
{% endif %}
