---
import type { PaginateFunction, Page } from "astro";
import { getCollection } from "astro:content";
import Layout from "@/layouts/Layout.astro";
import BlogPostEntry from "@/components/BlogPostEntry.astro";
import PageTitle from "@/components/PageTitle.astro";
import type { CollectionEntry } from "astro:content";
import Pagination from "@/components/Pagination.astro";

export async function getStaticPaths({
  paginate,
}: {
  paginate: PaginateFunction;
}) {
  const posts = await getCollection("blog");
  const filteredPosts = posts.filter((post) => !post.data.draft);
  filteredPosts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
  return paginate(filteredPosts, { pageSize: 10 });
}

interface Props {
  page: Page<CollectionEntry<"blog">>;
}

const { page } = Astro.props;
---

<Layout
  title="Posts | fiatcode"
  description="Read the latest blog posts by fiatcode"
>
  <PageTitle title="Posts" />
  <ul class="space-y-6">
    {
      page.data.map((post) => (
        <li>
          <BlogPostEntry
            id={post.id}
            title={post.data.title}
            date={post.data.date}
            description={post.data.description}
          />
        </li>
      ))
    }
  </ul>
  <Pagination
    currentPage={page.currentPage}
    lastPage={page.lastPage}
    getPageUrl={(page) => (page === 1 ? `/posts/` : `/posts/${page}/`)}
  />
</Layout>
