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 report validation
 */
const createReportValidation = [
  body("street").isMongoId().withMessage("Valid street ID is required"),
  body("issue")
    .trim()
    .notEmpty()
    .withMessage("Issue description is required")
    .isLength({ min: 10, max: 1000 })
    .withMessage("Issue description must be between 10 and 1000 characters"),
  validate,
];

/**
 * Report ID validation
 */
const reportIdValidation = [
  param("id").isMongoId().withMessage("Invalid report ID"),
  validate,
];

module.exports = {
  createReportValidation,
  reportIdValidation,
  validate,
};
