import type { Metadata } from "next";
import Link from "next/link";
import { notFound, permanentRedirect } from "next/navigation";
import { PageHero } from "@/components/ui";
import { getBlogPost } from "@/lib/blog/repository";
import { formatPostDate } from "@/lib/blog/types";
import { articleMetadata, articleSchema, safeJsonLd } from "@/lib/seo";
import "@/components/blog-list.css";
import { getCmsSettings, findRedirect } from "@/lib/cms/store";
import { ArticleActions } from "@/components/article-actions";
type Props = { params: Promise<{ slug: string }> };
export const dynamic = "force-dynamic";
export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const post = await getBlogPost((await params).slug);
  return post
    ? articleMetadata(post, await getCmsSettings())
    : { title: "Article not found", robots: { index: false, follow: false } };
}
export default async function PostPage({ params }: Props) {
  const post = await getBlogPost((await params).slug);
  if (!post) {
    const target = await findRedirect((await params).slug);
    if (target) permanentRedirect(`/post/${target}/`);
    notFound();
  }
  const settings = await getCmsSettings();
  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{
          __html: safeJsonLd(articleSchema(post, await getCmsSettings())),
        }}
      />
      <PageHero
        title={post.title}
        description="Commentary from the Movement for Better Government."
        kicker="From the movement"
      />
      <section className="page-content">
        <div className="wrap">
          <article className="article">
            <Link className="article-return" href="/blog/">
              ← Back to all articles
            </Link>
            <div className="article-meta">
              <span>By {post.author}</span>
              <time dateTime={post.publishedAt}>
                {formatPostDate(post.publishedAt)}
              </time>
              <span>{post.readingMinutes} min read</span>
              {post.modifiedAt.slice(0, 10) !==
                post.publishedAt.slice(0, 10) && (
                <span>
                  Updated{" "}
                  <time dateTime={post.modifiedAt}>
                    {formatPostDate(post.modifiedAt)}
                  </time>
                </span>
              )}
            </div>
            {post.image && (
              <img
                className="article-featured"
                src={post.image.url}
                alt={post.image.alt}
                style={{
                  width: "100%",
                  maxHeight: 560,
                  objectFit: "contain",
                  marginBottom: 32,
                }}
              />
            )}
            <div
              className="prose"
              dangerouslySetInnerHTML={{ __html: post.body }}
            />
            <ArticleActions
              title={post.title}
              url={new URL(
                `/post/${encodeURIComponent(post.slug)}/`,
                settings.siteUrl,
              ).toString()}
            />
          </article>
        </div>
      </section>
    </>
  );
}
