---
import Layout from "../layouts/Layout.astro";
import { getCollection, type CollectionEntry } from "astro:content";
import { config } from "../config";

// Get all posts from the content collection
const posts = await getCollection("posts");

// Sort posts by date, newest first
const sortedPosts = posts.sort(
    (a: CollectionEntry<"posts">, b: CollectionEntry<"posts">) =>
        new Date(b.data.pubDate).valueOf() - new Date(a.data.pubDate).valueOf(),
);

function formatDate(date: Date): string {
    return date.toLocaleDateString("en-us", {
        month: "short",
        day: "numeric",
        year: "numeric",
    });
}
---

<Layout
    title={config.siteConfig.pageOpenGraph.posts.title}
    description={config.siteConfig.pageOpenGraph.posts.description}
    ogImage={config.siteConfig.pageOpenGraph.posts.image}
    ogType={config.siteConfig.pageOpenGraph.posts.type}
>
    <div class="w-full max-w-3xl mx-auto p-4 sm:p-8">
        <h1
            class="text-3xl sm:text-4xl font-bold text-primary mb-6 sm:mb-8 text-center"
        >
            Posts
        </h1>

        {
            sortedPosts.length === 0 ? (
                <p class="text-center text-gray-500 mt-12">
                    No posts available yet. Check back soon!
                </p>
            ) : (
                <ul class="flex flex-col bg-base-100 rounded-box shadow-md border border-base-content/20 divide-y divide-base-content/20">
                    {sortedPosts.map((post) => (
                        <li class="flex items-center hover:bg-base-200/50 transition-colors p-4 group relative rounded-none first:rounded-t-box last:rounded-b-box">
                            <a href={`/post/${post.id}`} class="absolute inset-0 z-0" aria-label={`Read ${post.data.title}`}></a>
                            
                            <div class="w-24 sm:w-32 flex-none opacity-80 flex flex-col justify-center text-right pr-4 border-r border-base-content/20 z-10 pointer-events-none">
                                <span class="font-mono text-sm sm:text-base font-bold">
                                    {post.data.pubDate.toLocaleDateString("en-us", { month: "short", day: "numeric" })}
                                </span>
                                <span class="text-xs opacity-70">
                                    {post.data.pubDate.getFullYear()}
                                </span>
                            </div>

                            <div class="flex-grow flex flex-col gap-1 justify-center pl-4 z-10 pointer-events-none">
                                <h2
                                    class="font-bold text-lg sm:text-xl text-primary group-hover:text-accent transition-colors"
                                >
                                    {post.data.title}
                                </h2>
                                <p class="text-sm opacity-80 line-clamp-2 leading-relaxed">
                                    {post.data.description || "No description available."}
                                </p>
                                {post.data.tags && post.data.tags.length > 0 && (
                                    <div class="flex gap-2 mt-1">
                                        {post.data.tags.slice(0, 3).map((tag: string) => (
                                            <span class="badge badge-xs badge-outline opacity-70">
                                                {tag}
                                            </span>
                                        ))}
                                    </div>
                                )}
                            </div>
                        </li>
                    ))}
                </ul>
            )
        }
    </div>
</Layout>
