import jwt from 'jsonwebtoken';

export function signToken(payload) {
  return jwt.sign(payload, process.env.JWT_SECRET || 'secret', { expiresIn: '1d' });
}

export function verifyToken(token) {
  try {
    return jwt.verify(token, process.env.JWT_SECRET || 'secret');
  } catch (error) {
    return null;
  }
}

export function hasRole(userRoles, allowedRoles) {
  if (!userRoles || !Array.isArray(userRoles)) return false;
  if (!Array.isArray(allowedRoles)) allowedRoles = [allowedRoles];
  return allowedRoles.some(role => userRoles.includes(role));
}
