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

const schemas = {
  '/api/auth/register': Joi.object({
    email: Joi.string().email().required(),
    password: Joi.string().min(8).required()
  }),
  '/api/auth/login': Joi.object({
    email: Joi.string().email().required(),
    password: Joi.string().required()
  })
};

module.exports = {
  validateRequestSchema: (req, res, next) => {
    const schema = schemas[req.path];
    if (schema) {
      const { error } = schema.validate(req.body);
      if (error) {
        throw new ApiError(400, error.details[0].message);
      }
    }
    next();
  }
};