const Joi = require('joi');
const { ValidationError } = require('../utils/errors');

const validateRequestSchema = (schema) => {
  return (req, res, next) => {
    if (!schema) return next();

    const validationResult = schema.validate(req.body, {
      abortEarly: false,
      stripUnknown: true
    });

    if (validationResult.error) {
      const errors = validationResult.error.details.map(detail => detail.message);
      return next(new ValidationError(errors));
    }

    req.validatedData = validationResult.value;
    next();
  };
};

module.exports = { validateRequestSchema };