// app/[slug]/page.tsx
import { cache } from "react";
import { renderBySlug } from "./renderBySlug";
import { metadataBySlug } from "./metadataBySlug";
import { findBySlug } from "@/lib/slug/slugMap";
import { notFound } from "next/navigation";
import type { Metadata } from "next";

const getCachedSlugData = cache(async (slug: string) => {
    if (!slug) return null;
    // fetch data

    return findBySlug(slug);
});

export async function generateMetadata({
    params,
}: {
    params: Promise<{ slug: string }>;
}): Promise<Metadata> {
    const { slug }  = await params;
    const result    = await getCachedSlugData(slug);
    
    if (!result) {
        return { 
            title: "Local PC" 
        };
    }
    
    return metadataBySlug(result);
}

export default async function SlugPage({
    params,
}: {
    params: Promise<{ slug: string }>;
}) {
    const { slug }  = await params;
    const result    = await getCachedSlugData(slug);
    
    if (!result) {
        notFound();
    }
    return renderBySlug(result, slug);
}