/**
 * Middleware to verify admin role
 */
const verifyRole = (allowedRoles) => {
  return (req, res, next) => {
    try {
      // Check if admin info exists in request
      if (!req.admin) {
        return res.status(401).json({
          status: false,
          message: 'Admin information not found. Please authenticate first.',
          data: null,
        });
      }

      // Check if role is allowed
      if (!allowedRoles.includes(req.admin.role)) {
        return res.status(403).json({
          status: false,
          message: 'Insufficient permissions to access this resource',
          data: null,
        });
      }

      next();
    } catch (error) {
      return res.status(500).json({
        status: false,
        message: 'Role verification failed',
        data: null,
        error: error.message,
      });
    }
  };
};

module.exports = verifyRole;
