import { getAllPostsMeta } from "@/lib/posts"
import type { Metadata } from "next"
import Link from "next/link"

export async function generateMetadata(): Promise<Metadata> {
  return { title: "Categories", description: "List of categories" }
}

export default function CategoriesPage() {
  const posts = getAllPostsMeta()
  return (
    <main>
      <h1>Categories</h1>
      <ul>
        {Array.from(new Set(posts.flatMap((post) => post.categories))).map(
          (category) => (
            <li key={category}>
              <h2>{category}</h2>
              <ul>
                {posts
                  .filter((post) => post.categories.includes(category))
                  .map((post) => (
                    <li key={post.slug}>
                      <Link href={`/${post.slug}`}>{post.title}</Link>
                    </li>
                  ))}
              </ul>
            </li>
          ),
        )}
      </ul>
    </main>
  )
}
