---
import { Image } from "astro:assets";
import { getCollection, render } from "astro:content";
import BlogPostItem from "@/components/BlogPostItem.astro";
import BaseLayout from "@/layouts/BaseLayout.astro";

export async function getStaticPaths() {
  const categories = await getCollection("categories");
  const allPosts = await getCollection("blog", ({ data }) => !data.draft);

  return categories.map((category) => ({
    params: { slug: category.id },
    props: {
      category,
      posts: allPosts
        .filter((p) => p.data.category?.id === category.id)
        .sort(
          (a, b) => b.data.publishDate.valueOf() - a.data.publishDate.valueOf(),
        ),
    },
  }));
}

const { category, posts } = Astro.props;
const { Content } = await render(category);

const allPosts = await getCollection("blog", ({ data }) => !data.draft);
const seriesCountMap = new Map<string, number>();
for (const post of allPosts) {
  if (post.data.seriesParent) {
    seriesCountMap.set(
      post.data.seriesParent,
      (seriesCountMap.get(post.data.seriesParent) ?? 1) + 1,
    );
  }
}
---

<BaseLayout title={category.data.title} description={category.data.description}>
  <div class="page">
    <main class="main">
      <a href="/blog" class="back">← Blog</a>
      <p class="eyebrow">Category</p>
      <h1 class="heading">{category.data.title}</h1>
      {category.data.cover && (
        <Image class="category-cover" src={category.data.cover} alt={category.data.coverAlt ?? ""} />
      )}
      <div class="category-intro">
        <Content />
      </div>
      <p class="count">{posts.length} article{posts.length !== 1 ? "s" : ""}</p>
      <ul class="list">
        {posts.map((post) => (
          <BlogPostItem post={post} seriesCountMap={seriesCountMap} />
        ))}
      </ul>
    </main>
  </div>
</BaseLayout>

<style>
  .page {
    display: grid;
    grid-template-rows: auto 1fr auto;
    min-height: 100dvh;
    background: #f5f5f3;
    color: #111;
  }

  .main {
    max-width: var(--width-prose);
    margin: 0 auto;
    padding: 2rem 2rem 4rem;
    width: 100%;
  }

  .back {
    display: inline-block;
    font-size: 0.9rem;
    color: #555;
    text-decoration: none;
    border-bottom: 1px solid #ccc;
    padding-bottom: 1px;
    margin-bottom: 1.5rem;
  }

  .back:hover {
    color: var(--accent);
    border-color: var(--accent);
  }

  .eyebrow {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 0.4rem;
  }

  .heading {
    font-weight: 400;
    letter-spacing: -0.02em;
    margin-bottom: 1rem;
  }

  .category-cover {
    width: 100%;
    height: auto;
    border-radius: 6px;
    display: block;
    margin-bottom: 1.5rem;
  }

  .category-intro {
    font-size: 0.95rem;
    line-height: 1.6;
    color: #555;
    margin-bottom: 0.5rem;
  }

  .count {
    font-size: 0.875rem;
    color: #888;
    margin-bottom: 2.5rem;
  }

  .list {
    list-style: none;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
  }

  @media (max-width: 640px) {
    .item__link {
      flex-direction: column;
      gap: 0.2rem;
    }
  }
</style>
