const { body, param, validationResult } = require("express-validator");

const uservalidator = {
    adduser: [
        body("name").notEmpty().isLength({ min: 3, max: 100 }).withMessage("Name must be between 3 and 100 characters"),
        body("mobile").isMobilePhone().isLength({ min: 11 }).withMessage("Valid mobail is required"),
        body("club").notEmpty().withMessage("club is not valid"),
        body("password").isLength({ min: 6 }).withMessage("Password must be at least 6 characters long"),
    ],
    add: [
        body("name").notEmpty().isLength({ min: 2, max: 100 }).withMessage("Name must be between 2 and 100 characters"),
        body("email").isEmail().withMessage("Valid email is required"),
        body("password").isLength({ min: 6 }).withMessage("Password must be at least 6 characters long"),
    ],
    find: [
        param('id').isNumeric().notEmpty().withMessage('ID is required'),
    ],
    update: [
        param('id').isNumeric().notEmpty().withMessage('ID is required'),
        body('name').notEmpty().isLength({ min: 2, max: 100 }).withMessage('Name must be between 2 and 100 characters'),
        body('email').notEmpty().withMessage('Valid email is required'),
    ],
    updatePassword: [
        param('id').isNumeric().notEmpty().withMessage('ID is required'),
        body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long'),
    ],
    delete: [
        param('id').isNumeric().notEmpty().withMessage('ID is required'),
    ],
    error: (req, res, next) => {
        const error = validationResult(req);
        console.log('Validation Errors:', error.array());
        if (!error.isEmpty()) {
            return res.error(error.array().filter((err) => err.msg).map((err) => err.msg).join(', '));
        }
        next();
    }
}
module.exports = uservalidator;