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

const validateRequest = (req, res, next) => {
  const schema = Joi.object({
    body: req.method !== 'GET' ? Joi.object().required() : Joi.forbidden(),
    query: Joi.object(),
    params: Joi.object()
  });

  const { error } = schema.validate({
    body: req.body,
    query: req.query,
    params: req.params
  }, { abortEarly: false });

  if (error) {
    const errorMessage = error.details.map(detail => detail.message).join(', ');
    throw new CustomError(errorMessage, 400);
  }

  next();
};

module.exports = { validateRequest };