---
import LinkCard from "@/components/links/LinkCard.astro";
import LinksLayout from "@/components/links/LinksLayout.astro";
import { getLinks } from "@/lib/collections";
import { filterByTag, getUniqueTags } from "@/lib/links";

export async function getStaticPaths() {
  const links = await getLinks();
  const tags = getUniqueTags(links);
  return tags.map((tag) => ({
    params: { tag: tag.toLowerCase() },
    props: { tag, links },
  }));
}

const { tag, links } = Astro.props;
const tagLinks = filterByTag(links, tag);
---

<LinksLayout title={`#${tag}`} description={`Links mit Tag "${tag}".`} {links}>
  <div class="page">
    <header class="page__header">
      <span class="page__hash" aria-hidden="true">#</span>
      <h1 class="page__title">{tag}</h1>
      <span class="page__count">{tagLinks.length}</span>
    </header>
    {tagLinks.length === 0 ? (
      <p class="empty">Keine Links mit diesem Tag.</p>
    ) : (
      <ul class="link-list">
        {tagLinks.map((link) => <LinkCard {link} />)}
      </ul>
    )}
  </div>
</LinksLayout>

<style>
  .page {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
  }

  .page__header {
    display: flex;
    align-items: baseline;
    gap: 0.35rem;
  }

  .page__hash {
    font-size: 1.4rem;
    font-weight: 600;
    color: #aaa;
  }

  .page__title {
    font-size: 1.4rem;
    font-weight: 600;
    letter-spacing: -0.02em;
    color: #111;
  }

  .page__count {
    font-size: 0.875rem;
    color: #999;
    margin-left: 0.4rem;
  }

  .empty {
    color: #888;
    font-size: 0.95rem;
  }

  .link-list {
    margin: 0;
    padding: 0;
  }
</style>
