const handleError = (message, status, res) => {
  res.status(status).json({ success: false, message });
};

const handleSuccess = (res, data = null, status = 200) => {
  res.status(status).json({
    success: true,
    ...(data && { data }),
  });
};

// Centralize Sequelize error handling
const handleSequelizeError = (err, res) => {
  if (err.name === "SequelizeValidationError") {
    return handleError(err.errors.map((e) => e.message).join(", "), 400, res);
  }
  handleError(err.message, 500, res);
};

function deepEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}

module.exports = {
  handleError,
  handleSuccess,
  handleSequelizeError,
  deepEqual,
};
