import { ParsedUrlQuery } from "querystring";

import { GetStaticPaths, GetStaticProps } from "next";
import { MDXRemote } from "next-mdx-remote";
import React from "react";

import { EventCard } from "../../../../components/EventCard";
import {
  Event,
  getEventYears,
  getEventTermsByYear,
  getEventsByTerm,
  getEventBySlug,
} from "../../../../lib/events";

export default function EventInfoPage(props: Props) {
  return <p>Hello, world!</p>;
}

interface Props {
  // event: Event;
  event: string;
  poster?: string;
  registerLink?: string;
}

interface Params extends ParsedUrlQuery {
  year: string;
  term: string;
  event: string;
}

export const getStaticProps: GetStaticProps<Props, Params> = async () => {
  return {
    props: {
      event: "test",
    },
  };
};

export const getStaticPaths: GetStaticPaths<Params> = async () => {
  const years = await getEventYears();
  const terms = (
    await Promise.all(
      years.map(async (year) => {
        const termsInYear = await getEventTermsByYear(year);
        return termsInYear.map((term) => ({ year, term }));
      })
    )
  ).flat();
  const events = (
    await Promise.all(
      terms.map(async (term) => {
        const eventsInYear = await getEventsByTerm(term.year, term.term);
        return eventsInYear.map((event) => {
          return { year: term.year, term: term.term, event: event };
        });
      })
    )
  ).flat();

  return {
    paths: events.map((params) => ({ params })),
    fallback: false,
  };
};
