const { body, param, validationResult } = require('express-validator');
// Validation middleware
const handleValidationErrors = (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({
            status: 'error',
            message: 'Validation failed',
            errors: errors.array().map(error => ({
                field: error.path,
                message: error.msg,
                value: error.value
            }))
        });
    }
    next();
};
// Permission validation rules
const validatePermission = [
    body('name')
        .notEmpty()
        .withMessage('Permission name is required')
        .isLength({ min: 2, max: 50 })
        .withMessage('Permission name must be between 2 and 50 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Permission name can only contain letters, numbers, dots, underscores, and hyphens'),
    body('description')
        .notEmpty()
        .withMessage('Permission description is required')
        .isLength({ min: 5, max: 200 })
        .withMessage('Permission description must be between 5 and 200 characters'),
    body('resource')
        .notEmpty()
        .withMessage('Resource is required')
        .isLength({ min: 2, max: 30 })
        .withMessage('Resource must be between 2 and 30 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Resource can only contain letters, numbers, dots, underscores, and hyphens'),
    body('action')
        .notEmpty()
        .withMessage('Action is required')
        .isLength({ min: 2, max: 20 })
        .withMessage('Action must be between 2 and 20 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Action can only contain letters, numbers, dots, underscores, and hyphens'),
    handleValidationErrors
];
// Permission ID validation
const validatePermissionId = [
    param('id')
        .isInt({ min: 1 })
        .withMessage('Permission ID must be a positive integer'),
    handleValidationErrors
];
// Update permission validation (optional fields)
const validatePermissionUpdate = [
    param('id')
        .isInt({ min: 1 })
        .withMessage('Permission ID must be a positive integer'),
    body('name')
        .optional()
        .isLength({ min: 2, max: 50 })
        .withMessage('Permission name must be between 2 and 50 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Permission name can only contain letters, numbers, dots, underscores, and hyphens'),
    body('description')
        .optional()
        .isLength({ min: 5, max: 200 })
        .withMessage('Permission description must be between 5 and 200 characters'),
    body('resource')
        .optional()
        .isLength({ min: 2, max: 30 })
        .withMessage('Resource must be between 2 and 30 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Resource can only contain letters, numbers, dots, underscores, and hyphens'),
    body('action')
        .optional()
        .isLength({ min: 2, max: 20 })
        .withMessage('Action must be between 2 and 20 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Action can only contain letters, numbers, dots, underscores, and hyphens'),
    handleValidationErrors
];
module.exports = {
    validatePermission,
    validatePermissionId,
    validatePermissionUpdate
};
//# sourceMappingURL=permissionValidation.js.map