const { body } = require('express-validator');
// Validation rules for creating a contact
const createContactValidation = [
    body('name')
        .optional({ nullable: true, checkFalsy: true })
        .trim()
        .isLength({ min: 2, max: 255 })
        .withMessage('Name must be between 2 and 255 characters'),
    body('rank_id')
        .optional()
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true; // Allow null, undefined, or empty string
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Rank ID must be a positive integer');
        }
        return true;
    }),
    body('service_no')
        .optional()
        .trim()
        .isLength({ max: 50 })
        .withMessage('Service number must not exceed 50 characters'),
    body('login_id')
        .optional()
        .trim()
        .isLength({ min: 3, max: 100 })
        .withMessage('Login ID must be between 3 and 100 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Login ID can only contain letters, numbers, dots, underscores, and hyphens'),
    body('unit_id')
        .optional({ nullable: true, checkFalsy: true })
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true; // Allow null, undefined, or empty string
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Unit ID must be a positive integer');
        }
        return true;
    }),
    body('department_id')
        .optional({ nullable: true, checkFalsy: true })
        .isInt({ min: 1 })
        .withMessage('Department ID must be a positive integer'),
    body('designation_id')
        .optional({ nullable: true, checkFalsy: true })
        .isInt({ min: 1 })
        .withMessage('Designation ID must be a positive integer'),
    body('phone')
        .optional()
        .custom((value) => {
        if (!value || value.trim() === '') {
            return true; // Allow empty or null values
        }
        if (value.length > 20) {
            throw new Error('Phone number must not exceed 20 characters');
        }
        if (!/^[\+]?[0-9\s\-\(\)]+$/.test(value)) {
            throw new Error('Phone number contains invalid characters');
        }
        return true;
    }),
    body('mobile')
        .optional({ nullable: true, checkFalsy: true })
        .custom((value) => {
        if (!value || String(value).trim() === '') {
            return true;
        }
        const v = String(value).trim();
        if (v.length > 20) {
            throw new Error('Mobile number must not exceed 20 characters');
        }
        if (!/^[\+]?[0-9\s\-\(\)]+$/.test(v)) {
            throw new Error('Mobile number contains invalid characters');
        }
        return true;
    }),
    body('alternative_mobile')
        .optional()
        .custom((value) => {
        if (!value || value.trim() === '') {
            return true; // Allow empty or null values
        }
        if (value.length > 20) {
            throw new Error('Alternative mobile number must not exceed 20 characters');
        }
        if (!/^[\+]?[0-9\s\-\(\)]+$/.test(value)) {
            throw new Error('Alternative mobile number contains invalid characters');
        }
        return true;
    }),
    body('present_address')
        .optional()
        .trim()
        .isLength({ max: 500 })
        .withMessage('Present address must not exceed 500 characters'),
    body('office_address')
        .optional()
        .trim()
        .isLength({ max: 500 })
        .withMessage('Office address must not exceed 500 characters'),
    body('permanent_address')
        .optional()
        .trim()
        .isLength({ max: 500 })
        .withMessage('Permanent address must not exceed 500 characters'),
    body('occupation')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Occupation must not exceed 255 characters'),
    body('organization')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Organization must not exceed 255 characters'),
    body('designation')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Designation must not exceed 255 characters'),
    body('facebook')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Facebook link must not exceed 255 characters'),
    body('linkedin')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('LinkedIn link must not exceed 255 characters'),
    body('email')
        .optional({ nullable: true, checkFalsy: true })
        .trim()
        .isEmail()
        .withMessage('Please provide a valid email address')
        .isLength({ max: 255 })
        .withMessage('Email must not exceed 255 characters'),
    body('password_hash')
        .optional()
        .trim()
        .isLength({ min: 60, max: 255 })
        .withMessage('Password hash must be between 60 and 255 characters'),
    body('parent_id')
        .optional()
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true; // Allow null, undefined, or empty string
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Parent ID must be a positive integer');
        }
        return true;
    })
];
// Validation rules for updating a contact
const updateContactValidation = [
    body('name')
        .optional({ nullable: true, checkFalsy: true })
        .trim()
        .isLength({ min: 2, max: 255 })
        .withMessage('Name must be between 2 and 255 characters'),
    body('rank_id')
        .optional()
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true; // Allow null, undefined, or empty string
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Rank ID must be a positive integer');
        }
        return true;
    }),
    body('service_no')
        .optional()
        .trim()
        .isLength({ max: 50 })
        .withMessage('Service number must not exceed 50 characters'),
    body('login_id')
        .optional()
        .trim()
        .notEmpty()
        .withMessage('Login ID cannot be empty')
        .isLength({ min: 3, max: 100 })
        .withMessage('Login ID must be between 3 and 100 characters')
        .matches(/^[a-zA-Z0-9._-]+$/)
        .withMessage('Login ID can only contain letters, numbers, dots, underscores, and hyphens'),
    body('unit_id')
        .optional()
        .isInt({ min: 1 })
        .withMessage('Unit ID must be a positive integer'),
    body('department_id')
        .optional()
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true; // Allow null, undefined, or empty string
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Department ID must be a positive integer');
        }
        return true;
    }),
    body('designation_id')
        .optional()
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true;
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Designation ID must be a positive integer');
        }
        return true;
    }),
    body('phone')
        .optional()
        .custom((value) => {
        if (!value || value.trim() === '') {
            return true; // Allow empty or null values
        }
        if (value.length > 20) {
            throw new Error('Phone number must not exceed 20 characters');
        }
        if (!/^[\+]?[0-9\s\-\(\)]+$/.test(value)) {
            throw new Error('Phone number contains invalid characters');
        }
        return true;
    }),
    body('mobile')
        .optional()
        .custom((value) => {
        if (!value || value.trim() === '') {
            return true; // Allow empty or null values
        }
        if (value.length > 20) {
            throw new Error('Mobile number must not exceed 20 characters');
        }
        if (!/^[\+]?[0-9\s\-\(\)]+$/.test(value)) {
            throw new Error('Mobile number contains invalid characters');
        }
        return true;
    }),
    body('alternative_mobile')
        .optional()
        .custom((value) => {
        if (!value || value.trim() === '') {
            return true; // Allow empty or null values
        }
        if (value.length > 20) {
            throw new Error('Alternative mobile number must not exceed 20 characters');
        }
        if (!/^[\+]?[0-9\s\-\(\)]+$/.test(value)) {
            throw new Error('Alternative mobile number contains invalid characters');
        }
        return true;
    }),
    body('present_address')
        .optional()
        .trim()
        .isLength({ max: 500 })
        .withMessage('Present address must not exceed 500 characters'),
    body('office_address')
        .optional()
        .trim()
        .isLength({ max: 500 })
        .withMessage('Office address must not exceed 500 characters'),
    body('permanent_address')
        .optional()
        .trim()
        .isLength({ max: 500 })
        .withMessage('Permanent address must not exceed 500 characters'),
    body('occupation')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Occupation must not exceed 255 characters'),
    body('organization')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Organization must not exceed 255 characters'),
    body('designation')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Designation must not exceed 255 characters'),
    body('facebook')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('Facebook link must not exceed 255 characters'),
    body('linkedin')
        .optional()
        .trim()
        .isLength({ max: 255 })
        .withMessage('LinkedIn link must not exceed 255 characters'),
    body('email')
        .optional({ nullable: true, checkFalsy: true })
        .trim()
        .isEmail()
        .withMessage('Please provide a valid email address')
        .isLength({ max: 255 })
        .withMessage('Email must not exceed 255 characters'),
    body('password_hash')
        .optional()
        .trim()
        .isLength({ min: 60, max: 255 })
        .withMessage('Password hash must be between 60 and 255 characters'),
    body('parent_id')
        .optional()
        .custom((value) => {
        if (value === null || value === undefined || value === '') {
            return true; // Allow null, undefined, or empty string
        }
        if (!Number.isInteger(Number(value)) || Number(value) < 1) {
            throw new Error('Parent ID must be a positive integer');
        }
        return true;
    }),
    body('is_active')
        .optional()
        .isBoolean()
        .withMessage('is_active must be a boolean value'),
    body('status')
        .optional()
        .isIn(['online', 'offline', 'busy'])
        .withMessage('Status must be online, offline, or busy')
];
module.exports = {
    createContactValidation,
    updateContactValidation
};
//# sourceMappingURL=contactValidation.js.map