---
import BaseLayout from "../../layouts/BaseLayout.astro";
import { getCollection } from "astro:content";

const posts = (await getCollection("blog", ({ data }) => !data.draft))
    .sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf());

function formatDate(date: Date) {
    return date.toISOString().split("T")[0];
}
---

<BaseLayout
    title="Blog — Hidden Den Cafe"
    description="Latte's blog. Thoughts, technical things, and whatever else is on my mind."
>
    <div class="matrix-bg" aria-hidden="true"></div>

    <main class="main">
        <div class="container">
            <header class="header fade-in">
                <h1 class="title">Blog</h1>
                <div class="divider">══════════════════════════════</div>
            </header>

            {posts.length === 0 ? (
                <section class="section fade-in">
                    <p class="empty">No posts yet. Will get around to it.</p>
                </section>
            ) : (
                <section class="section fade-in">
                    <ul class="post-list">
                        {posts.map((post) => (
                            <li class="post-item">
                                <span class="post-date">{formatDate(post.data.date)}</span>
                                <div class="post-info">
                                    <a href={`/blog/${post.slug}`} class="post-title">{post.data.title}</a>
                                    <p class="post-desc">{post.data.description}</p>
                                </div>
                            </li>
                        ))}
                    </ul>
                </section>
            )}

            <footer class="footer fade-in">
                <p>Made with love by Latte</p>
            </footer>
        </div>
    </main>
</BaseLayout>

<style>
    .matrix-bg {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: -1;
        opacity: 0.03;
        background:
            linear-gradient(var(--color-accent) 1px, transparent 1px),
            linear-gradient(90deg, var(--color-accent) 1px, transparent 1px);
        background-size: 50px 50px;
        animation: grid-move 20s linear infinite;
    }

    @keyframes grid-move {
        0% { transform: translate(0, 0); }
        100% { transform: translate(50px, 50px); }
    }

    .main {
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: var(--space-lg) var(--space-md);
        padding-top: calc(var(--space-lg) + 3rem);
    }

    .container {
        max-width: 700px;
        width: 100%;
        background: var(--color-glass);
        backdrop-filter: blur(10px);
        border: 1px solid var(--color-surface);
        border-radius: 8px;
        padding: var(--space-xl);
    }

    .header {
        margin-bottom: var(--space-lg);
    }

    .title {
        font-size: 2rem;
        font-weight: 700;
        letter-spacing: 2px;
        text-align: center;
    }

    .divider {
        color: var(--color-surface);
        text-align: center;
        font-size: 0.75rem;
        margin: var(--space-md) 0;
        user-select: none;
    }

    .section {
        margin: var(--space-md) 0;
    }

    .post-list {
        list-style: none;
        display: flex;
        flex-direction: column;
        gap: var(--space-md);
    }

    .post-item {
        display: flex;
        gap: var(--space-md);
        align-items: flex-start;
    }

    .post-date {
        color: var(--color-text-dim);
        font-size: 0.8rem;
        white-space: nowrap;
        padding-top: 2px;
        min-width: 90px;
    }

    .post-info {
        display: flex;
        flex-direction: column;
        gap: var(--space-xs);
    }

    .post-title {
        color: var(--color-blue);
        font-size: 1rem;
        font-weight: 700;
    }

    .post-title:hover {
        color: var(--color-accent-bright);
    }

    .post-desc {
        color: var(--color-text-dim);
        font-size: 0.85rem;
        line-height: 1.6;
    }

    .empty {
        color: var(--color-text-dim);
        font-style: italic;
    }

    .footer {
        margin-top: var(--space-xl);
        text-align: center;
    }

    .footer p {
        color: var(--color-text-dim);
        font-size: 0.85rem;
    }

    .fade-in {
        animation: fadeIn 0.6s ease-out forwards;
        opacity: 0;
    }

    .fade-in:nth-child(1) { animation-delay: 0.1s; }
    .fade-in:nth-child(2) { animation-delay: 0.2s; }
    .fade-in:nth-child(3) { animation-delay: 0.3s; }

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(10px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    @media (max-width: 600px) {
        .container {
            padding: var(--space-md);
        }

        .title {
            font-size: 1.5rem;
        }

        .divider {
            font-size: 0.6rem;
        }

        .post-item {
            flex-direction: column;
            gap: var(--space-xs);
        }

        .post-date {
            min-width: unset;
        }
    }

    @media (prefers-reduced-motion: reduce) {
        .matrix-bg {
            animation: none;
        }

        .fade-in {
            animation: none;
            opacity: 1;
        }
    }
</style>
