import { getDocBySlug, getAllDocSlugs } from "../../../lib/docs";
import { DocContent } from "../../../components/DocContent";
import { notFound } from "next/navigation";
import { Metadata } from "next";

export async function generateStaticParams() {
    const slugs = getAllDocSlugs();
    return slugs.map((slug) => ({
        slug,
    }));
}

export async function generateMetadata(
    { params }: { params: Promise<{ slug: string[] }> }
): Promise<Metadata> {
    const resolvedParams = await params;
    const slug = resolvedParams?.slug || [];
    const doc = await getDocBySlug(slug);
    if (!doc) {
        return {
            title: "Not Found",
        };
    }
    return {
        title: doc.meta.title,
        description: doc.meta.description || doc.meta.summary,
    };
}

export default async function DocPage({ params }: { params: Promise<{ slug: string[] }> }) {
    const resolvedParams = await params;
    const slug = resolvedParams?.slug || [];
    const doc = await getDocBySlug(slug);

    if (!doc) {
        notFound();
    }

    return (
        <article className="doc-page-container">
            <h1 className="doc-page-title">{doc!.meta.title}</h1>
            {doc!.meta.description && (
                <p className="doc-page-description">{doc!.meta.description}</p>
            )}
            <DocContent contentHtml={doc!.contentHtml} />
        </article>
    );
}
