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

/**
 * Create task validation
 */
const createTaskValidation = [
  body("street").isMongoId().withMessage("Valid street ID is required"),
  body("description")
    .trim()
    .notEmpty()
    .withMessage("Task description is required")
    .isLength({ min: 5, max: 500 })
    .withMessage("Description must be between 5 and 500 characters"),
  validate,
];

/**
 * Task ID validation
 */
const taskIdValidation = [
  param("id").isMongoId().withMessage("Invalid task ID"),
  validate,
];

module.exports = {
  createTaskValidation,
  taskIdValidation,
  validate,
};
