import CardLink from "../components/CardLink";

interface PostMeta {
  slug: string;
  title: string;
  date: string;
  excerpt: string;
  cover?: string;
}

const postFiles = import.meta.glob("../blog/*.md", { eager: true }) as Record<
  string,
  { attributes: Omit<PostMeta, "slug"> }
>;

const posts: PostMeta[] = Object.entries(postFiles)
  .map(([path, mod]) => ({
    slug: path.split("/").pop()!.replace(".md", ""),
    ...(mod.attributes as Omit<PostMeta, "slug">),
  }))
  .sort(
    (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
  );

export default function Blog() {
  return (
    <section className="container mx-auto px-4 py-10">
      <h1 className="text-4xl font-bold mb-8">Blog</h1>
      
      {posts.map((post) => (
        <a key={post.slug} href={`/blog/${post.slug}`} className="block py-1">
          <CardLink
            title={post.title}
            body={post.excerpt}
            imgSrc={post.cover}
          />
        </a>
      ))}
    </section>
  );
}
