import { PagePropsType } from "@/src/types";
import matter from "gray-matter";
import { DSQL_TBEN_ME_BLOG_POSTS } from "@/src/types/dsql";
import { BunextPageServerFn } from "@moduletrace/bunext/types";
import sql from "@/src/utils/db";

export const server: BunextPageServerFn<PagePropsType> = async (ctx) => {
    const blog_post_res = await sql`
        SELECT * FROM blog_posts 
        WHERE slug=${ctx.query?.slug}
    `;

    const blog_post: DSQL_TBEN_ME_BLOG_POSTS = [...blog_post_res]?.[0];

    if (!blog_post?.body) {
        return {
            props: {},
        };
    }

    const pageMdString = blog_post.body;

    const pageGrayMatter = matter(pageMdString);

    const content = pageGrayMatter.content;

    return {
        props: {
            blogPost: blog_post,
        },
    };
};
