const { query, 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();
};

/**
 * Task suggestions validation
 */
const taskSuggestionsValidation = [
  query("streetId")
    .optional()
    .isString()
    .withMessage("Street ID must be a string"),
  query("count")
    .optional()
    .isInt({ min: 1, max: 10 })
    .withMessage("Count must be between 1 and 10"),
  validate,
];

module.exports = {
  taskSuggestionsValidation,
  validate,
};
