import React from "react";
import { NextPage, GetStaticProps } from "next";
import Head from "next/head";
import useSWR from "swr";
import Event from "@models/Event";
import EventApi from "@api/eventApi";
import Post from "@models/Feed";
import FeedApi from "@api/feedApi";
import ActualPageView from "@views/ActualPage/ActualPageView";
import PageWrapper from "@views/common/PageWrapper";
import { fetcher, APIPath, API } from "@api/backend";

interface InitialProps {
  initialEvents: Event[];
  initialFeed: Post[];
}

const eventApi: API = {
  path: APIPath.EVENTS,
};

const feedApi: API = {
  path: APIPath.FEED,
};

const ActualPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => {
  const { data: events } = useSWR<Event[]>(eventApi, fetcher, { fallbackData: initialEvents });
  const { data: feed } = useSWR<Post[]>(feedApi, fetcher, { fallbackData: initialFeed });

  return (
    <>
      <Head>
        <link rel="canonical" href={`${process.env.NEXT_PUBLIC_SITE_URL}/kilta/toiminta`} />
      </Head>
      <PageWrapper>
        <ActualPageView events={events} feed={feed} />
      </PageWrapper>
    </>
  );
};

export const getStaticProps: GetStaticProps<InitialProps> = async () => {
  const initialEvents = await EventApi.getEvents();
  const initialFeed = await FeedApi.getFeed();
  return {
    props: {
      initialEvents,
      initialFeed,
    },
    revalidate: 10,
  };
};

export default ActualPage;
