import { type MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
import { MDXRemote } from 'next-mdx-remote/rsc'
// import { promises as fs } from "fs";
import { MdxContent } from "../../../components/mdx-content";
import { redirect } from "next/navigation";

type Frontmatter = {
  title: string;
  date: string;
};

type Post<TFrontmatter> = {
  serialized: MDXRemoteSerializeResult;
  frontmatter: TFrontmatter;
};

export const runtime = "nodejs";

async function getPost(filepath: string): Promise<Post<Frontmatter>> {
  // Read the file from the filesystem
  // const raw = await fs.readFile(filepath, "utf-8");


  const raw = `# Hello World

  This is from Server Components!
  `

  // Serialize the MDX content and parse the frontmatter
  const serialized = await serialize(raw, {
    parseFrontmatter: true,
  });

  // Typecast the frontmatter to the correct type
  const frontmatter = serialized.frontmatter as Frontmatter;

  // Return the serialized content and frontmatter
  return {
    frontmatter,
    serialized,
  };
}

export default async function PostsPage() {
  // Get the serialized content and frontmatter
  // const { serialized, frontmatter } = await getPost("content/post.mdx");

  return redirect("/");

  return (
    <div style={{ maxWidth: 600, margin: "auto" }}>
      {/* <h1>{frontmatter.title}</h1>
      <p>Published {frontmatter.date}</p>
      <hr />
      <MdxContent source={serialized} />
      qqqqqq */}

      {/* <MDXRemote
        source={`# Hello World

      This is from Server Components!
      `}
      /> */}


    </div>
  );
}
