import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { PageArticle } from "@/components/public/PageArticle";
import { pageAlternates } from "@/lib/seo";
import { getPublishedPageBySlug } from "@/lib/services/pages";

type Props = { params: Promise<{ slug: string }> };

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params;
  const page = await getPublishedPageBySlug(slug);
  if (!page) return {};
  return {
    title: page.title,
    alternates: pageAlternates(`/pages/${page.slug}`),
    openGraph: { title: page.title, url: `/pages/${page.slug}` },
  };
}

export default async function StaticPage({ params }: Props) {
  const { slug } = await params;
  // Draft pages are filtered inside the query — they 404 like unknown slugs.
  const page = await getPublishedPageBySlug(slug);
  if (!page) notFound();
  return <PageArticle page={page} />;
}
