import { withAuth } from "next-auth/middleware";

import { UserRole } from "@prisma/client";

// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
export default withAuth({
  callbacks: {
    authorized({ req, token }) {
      console.log("req", req);
      // `/admin` requires admin role
      if (req.nextUrl.pathname === "/examples/admin") {
        return token?.userRole === "adminer"
      }

      if (req.nextUrl.pathname === "/cart" && token?.role === UserRole.ADMIN) {
        // return NextResponse.redirect(new URL("/", req.url));
        return true;
      }
      // if(req.nextUrl.pathname === "/cart"){
      //   return token?.role !== Role.ADMIN;
      // }


      // `/me` only requires the user to be logged in
      return !!token
    },
  },
})

export const config = {
  // matcher: ["/admin", "/me"]
  matcher: ["/admin/:path*", "/me", "/cart, /"],
  // callbackUrl: {
  //   name: `__Secure-next-auth.callback-url`,
  //   options: {
  //     sameSite: 'lax',
  //     path: '/', 
  //     secure: true
  //   }
  // },
}