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="text-4xl font-bold mb-4">{doc.meta.title}</h1>
            {doc.meta.description && (
                <p className="text-xl text-gray-500 dark:text-gray-400 mb-8">{doc.meta.description}</p>
            )}
            <DocContent contentHtml={doc.contentHtml} />
        </article>
    );
}
