import { getAllPostSlugs, getPostBySlug } from "@/lib/posts"
import type { Metadata } from "next"
import readingTime from "reading-time"

export async function generateStaticParams() {
  return getAllPostSlugs().map((slug) => ({ slug }))
}

export const dynamicParams = false

export async function generateMetadata({
  params,
}: {
  params: Promise<{ slug: string }>
}): Promise<Metadata> {
  const { slug } = await params
  const { meta } = await getPostBySlug(slug)
  return { title: meta.title, description: meta.description }
}

export default async function Post({
  params,
}: {
  params: Promise<{ slug: string }>
}) {
  const { slug } = await params
  const { meta, contentHtml } = await getPostBySlug(slug)

  return (
    <article className="prose mx-auto my-8 max-w-3xl px-4">
      <h1>{meta.title}</h1>
      <time>{new Date(meta.date).toLocaleTimeString()}</time>
      <span>{readingTime(contentHtml).text}</span>
      <div
        dangerouslySetInnerHTML={{ __html: contentHtml }}
        className="custom-prose"
      />
    </article>
  )
}
