import type { GetStaticProps, NextPage } from 'next'
import { format, parseISO } from 'date-fns';
import Link from 'next/link';
import { Layout } from '../components/index'
import { PostType } from '../types/post';
import { getAllPosts } from '../lib/api';

type IndexProps = {
  posts: PostType[];
};

export const Home = ({ posts }: IndexProps): JSX.Element => {
  return (
    <Layout>
      <h1>Красников Павел</h1>
      <p>Робототехника, программирование</p>
      {posts.map((post) => (
        <article key={post.slug} className="mt-12">
          <p className="mb-1 text-sm text-gray-500 dark:text-gray-400">
            {format(parseISO(post.date), 'MMMM dd, yyyy')}
          </p>
          <h1 className="mb-2 text-xl">
            <Link as={`/posts/${post.slug}`} href={`/posts/[slug]`}>
              <a className="text-gray-900 dark:hover:text-blue-400">
                {post.title}
              </a>
            </Link>
          </h1>
          <p className="mb-3">{post.description}</p>
          <p>
            <Link as={`/posts/${post.slug}`} href={`/posts/[slug]`}>
              <a>Подробнее...</a>
            </Link>
          </p>
        </article>
      ))}
    </Layout>
  )
}

export const getStaticProps: GetStaticProps = async () => {
  const posts = getAllPosts(['date', 'description', 'slug', 'title']);

  return {
    props: { posts },
  };
};

export default Home
