// require("../models/roleModel")

// const authorize = (allowedRoles) => {
//   return (req, res, next) => {
//     try {
//       if (typeof allowedRoles === "string") {
//         allowedRoles = [allowedRoles];
//       }

//       if (!allowedRoles.includes(req.user.role.name)) {
//         return res.status(403).json({
//           success: false,
//           message: "Access denied. You do not have permission.",
//         });
//       }
//       // if (!roles.includes(req.user.role?.name)) {
//       //   return res.status(403).json({
//       //     success: false,
//       //     message: "Access denied. Insufficient role",
//       //   });
//       // }

//       next();
//     } catch (error) {
//       res.status(403).json({
//         success: false,
//         message: "Access denied. You do not have permission.",
//       });
//     }
//   };
// };

// module.exports = authorize;


const authorize = (allowedRoles) => {
  return (req, res, next) => {
    console.log("ROLE FROM TOKEN:", req.user.role);
    try {
      if (typeof allowedRoles === "string") {
        allowedRoles = [allowedRoles];
      }

      if (!allowedRoles.includes(req.user.role)) {
        return res.status(403).json({
          success: false,
          message: "Access denied. You do not have permission.",
        });
      }

      next();
    } catch (error) {
      res.status(403).json({
        success: false,
        message: "Access denied. You do not have permission.",
      });
    }
  };
};

module.exports = authorize;

