const { param, validationResult } = require("express-validator");

const validate = (req, res, next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({
      success: false,
      errors: errors.array().map((err) => ({
        field: err.path,
        message: err.msg,
      })),
    });
  }
  next();
};

/**
 * User ID validation
 */
const userIdValidation = [
  param("id").isMongoId().withMessage("Invalid user ID"),
  validate,
];

module.exports = {
  userIdValidation,
  validate,
};
