import type { Metadata } from "next";
import { notFound } from "next/navigation";

import { getPageBySlug } from "@/app/lib/pages";

// Catches any single-segment URL no other route owns (/withdraw-taxes-policy,
// /astroeye-faq-...) and renders the matching `pages` row. Rendered per
// request from the table: a page added in the
// database is live immediately — there is nothing to rebuild.
export const dynamic = "force-dynamic";

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

export async function generateMetadata({ params }: Params): Promise<Metadata> {
  const { slug } = await params;
  const page = await getPageBySlug(slug);

  if (!page) return {};

  return {
    title: page.metaTitle || page.title,
    description: page.metaDescription || undefined,
  };
}

export default async function CmsPage({ params }: Params) {
  const { slug } = await params;
  const page = await getPageBySlug(slug);

  if (!page) notFound();

  return (
    <main className="static-page">
      <div className="container py-5">
        <h1 className="mb-4">{page.title.split("|")[0].trim()}</h1>

        {/* Trusted: authored by admins in this project's own database. */}
        <div
          className="static-page-content"
          dangerouslySetInnerHTML={{ __html: page.description }}
        />
      </div>
    </main>
  );
}
