import { GetStaticPaths, GetStaticProps } from "next";
import fs from "fs";
import { ParsedUrlQuery } from "querystring";
import {
  getHeadings,
  getProjectContentBySlug,
  PROJECTS_PATH,
} from "../../utils/markdown";
import { MarkdownRenderingResult } from "../../types/types";
import { MainLayout } from "../../layouts/MainLayout";
import Link from "next/link";
import { IoMdArrowRoundBack as BackIcon } from "react-icons/io";
import { readingTime } from "../../utils/general";
import { PostHeader } from "../../components/PostHeader";
import { StyledMarkdown } from "../../components/StyledMarkdown";

export const ProjectArticle = ({
  frontMatter,
  html,
}: MarkdownRenderingResult) => {
  return (
    <MainLayout>
      <div className="rounded-md border border-gray-200 shadow-md shadow-gray-200 dark:shadow-gray-900 dark:border-gray-700 gap-4 bg-black bg-opacity-30 round">
        <div className="mx-10 my-10">
          <Link href="/projects">
            <button className="flex flex-row items-center gap-2 text-primary hover:text-primary-dark dark:text-secondary dark:hover:text-secondary/50 transition-all ease-in duration-75">
              <BackIcon />
              <span>Go Back</span>
            </button>
          </Link>

          <PostHeader frontMatter={frontMatter} estTime={readingTime(html)} />

          <StyledMarkdown html={html} />
        </div>
      </div>
    </MainLayout>
  );
};
interface ProjectProps extends ParsedUrlQuery {
  slug: string;
}
export const getStaticPaths: GetStaticPaths<ProjectProps> = async () => {
  const paths = fs
    .readdirSync(PROJECTS_PATH)
    .map((path) => path.replace(/\.mdx?$/, ""))
    .map((slug) => ({ params: { slug } }));

  return {
    paths,
    fallback: false,
  };
};

export const getStaticProps: GetStaticProps<MarkdownRenderingResult> = async ({
  params,
}) => {
  const markdownContent = getProjectContentBySlug(params?.slug as string);

  console.log(getHeadings(markdownContent.content));

  return {
    props: {
      frontMatter: markdownContent.frontMatter,
      html: markdownContent.content,
    },
  };
};
export default ProjectArticle;
