//next.js page to show all locatons in the database with a link to the location page
import { useState } from "react";
import { useRouter } from 'next/router';
import Layout from "../../../components/layout";
import axiosServer from '../../../src/axiosServer';
import { useSession } from 'next-auth/react';
import ProtectedRoute from '../../../components/protectedRoute';
import PublisherForm from "../../../components/publisher/PublisherForm";
import { Publisher, UserRole } from "@prisma/client";


export default function NewPubPage(item: Publisher) {
  item = item.item;
  const [publisher, setPublisher] = useState<Publisher>(item);
  const router = useRouter();
  const { id, self } = router.query;
  const { data: session, status } = useSession();

  const userRole = session?.user?.role as UserRole;
  const userId = session?.user?.id;

  // Check if the user is editing their own profile and adjust allowedRoles accordingly
  let allowedRoles = [UserRole.POWERUSER, UserRole.ADMIN];
  if (status === 'authenticated' && userId && userId === id) {
    allowedRoles.push(userRole);
  }

  return (
    <Layout>
      <ProtectedRoute allowedRoles={allowedRoles}>
        <div className="h-5/6 grid place-items-center">
          <PublisherForm key={item?.id} item={item} me={self} />
        </div>
      </ProtectedRoute>
    </Layout>
  );
}

//------------------pages\cart\publishers\edit\[id].tsx------------------

// export const getServerSideProps = async (context) => {
//   const axios = await axiosServer(context);

//   context.res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate");
//   if (!context.query || !context.query.id) {
//     return {
//       props: {}
//     };
//   }
//   var url = process.env.NEXT_PUBLIC_PUBLIC_URL + "/api/data/publishers/" + context.query.id + "?include=availabilities,shifts";
//   console.log("GET PUBLISHER FROM:" + url)
//   const { data } = await axios.get(url);

//   //filter old availabilities
//   data.availabilities = data.availabilities.filter((a) => new Date(a.startTime) >= new Date());
//   return {
//     props: {
//       data
//     },
//   };
// };
