---
import { getCollection, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';

export async function getStaticPaths() {
  const posts = await getCollection('posts');
  return posts.map((post) => ({
    params: { slug: post.id },
    props: { post },
  }));
}

const { post } = Astro.props;
const { Content } = await render(post);

const fmt = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
});
---

<BaseLayout title={post.data.title} description={post.data.description}>
  <article>
    <header class="post-header">
      <h1>{post.data.title}</h1>
      <p class="meta">
        <time datetime={post.data.date.toISOString()}>
          {fmt.format(post.data.date)}
        </time>
        <span class="dot">·</span>
        <a href={`/categories/${post.data.category}/`} class="category">
          {post.data.category}
        </a>
      </p>
    </header>

    <div class="body">
      <Content />
    </div>

    {
      post.data.tags.length > 0 && (
        <footer class="tags">
          {post.data.tags.map((tag) => (
            <a href={`/tags/${tag}/`} class="tag">#{tag}</a>
          ))}
        </footer>
      )
    }

    <p class="back"><a href="/">← back</a></p>
  </article>

  <style>
    .post-header {
      margin-bottom: 2rem;
      padding-bottom: 1rem;
      border-bottom: 1px solid var(--rule);
    }
    h1 { margin: 0 0 0.5rem; }
    .meta { margin: 0; color: var(--muted); font-size: 0.9rem; }
    .meta .dot { margin: 0 0.4rem; }
    .meta .category { color: var(--muted); }
    .meta .category:hover { color: var(--accent); }
    .body :global(p) { margin: 0 0 1rem; }
    .body :global(h2) { font-size: 1.3rem; margin: 2rem 0 0.75rem; }
    .body :global(h3) { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; }
    .body :global(ul), .body :global(ol) { padding-left: 1.4rem; }
    .body :global(li) { margin: 0.25rem 0; }
    .body :global(blockquote) {
      font-size: 1.1rem;
      line-height: 1.5;
      font-style: normal;
      border-left: 3px solid var(--accent);
      padding: 0.15rem 0 0.15rem 1rem;
      color: var(--fg);
      margin: 0 0 1.75rem;
    }
    .body :global(blockquote p) { margin: 0; }
    .tags {
      margin-top: 2.5rem;
      padding-top: 1rem;
      border-top: 1px solid var(--rule);
      display: flex;
      gap: 0.75rem;
      flex-wrap: wrap;
    }
    .tag { font-size: 0.85rem; color: var(--muted); }
    .tag:hover { color: var(--accent); }
    .back { margin-top: 2rem; font-size: 0.9rem; }
  </style>
</BaseLayout>
