import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { BlockRenderer } from "@/components/blocks/BlockRenderer";
import Navbar from "@/components/Navbar";
import Footer from "@/components/Footer";
import { getWpPage } from "@/lib/wp/queries/page";
import { getProduit } from "@/lib/wp/queries/produit";
import { getToiture } from "@/lib/wp/queries/toiture";
import { getIndustrie } from "@/lib/wp/queries/industrie";
import { getReference } from "@/lib/wp/queries/reference";
import { isWpConfigured } from "@/lib/wp/client";
import type { WpSection } from "@/lib/wp/types";

// Préviz : rend n'importe quel contenu WordPress via le BlockRenderer,
// avant le branchement des vraies routes. Jamais indexée.
// - /wp/<slug-de-page>            → page native
// - /wp/<cpt>/<slug>              → produit | toiture | industrie | reference
export const metadata: Metadata = { robots: { index: false, follow: false } };
export const dynamic = "force-dynamic";

const CPT_GETTERS: Record<
  string,
  (slug: string) => Promise<{ sections?: { sections?: WpSection[] | null } | null } | null>
> = {
  produit: getProduit,
  toiture: getToiture,
  industrie: getIndustrie,
  reference: getReference,
};

export default async function WpPreviewPage({
  params,
}: {
  params: Promise<{ uri: string[] }>;
}) {
  if (!isWpConfigured()) notFound();
  const { uri } = await params;

  const content =
    uri.length === 2 && CPT_GETTERS[uri[0]]
      ? await CPT_GETTERS[uri[0]](uri[1])
      : await getWpPage(`/${uri.join("/")}`);
  if (!content) notFound();

  return (
    <div className="min-h-screen bg-background">
      <Navbar />
      <main>
        <BlockRenderer sections={content.sections?.sections} />
      </main>
      <Footer />
    </div>
  );
}
