const HttpException = require('../../../utils/HttpException.js');

exports.validateCreateSalesRequest = (body) => {
    const { username, email, firstname, lastname, password, province_ids } = body;
    const errors = {};

    if (!username || username.trim() === '') {
        errors.username = ['Username is required'];
    }

    if (!email || email.trim() === '') {
        errors.email = ['Email is required'];
    }

    if (!firstname || firstname.trim() === '') {
        errors.firstname = ['First name is required'];
    }

    if (!lastname || lastname.trim() === '') {
        errors.lastname = ['Last name is required'];
    }

    if (!password || password.trim().length === 0) {
        errors.password = ['Password is required'];
    } else {
        const trimmed = password.trim();

        const messages = [];

        if (trimmed.length < 8) {
            messages.push('Password must be at least 8 characters');
        }

        if (!/[A-Z]/.test(trimmed)) {
            messages.push('Password must contain uppercase letter');
        }

        if (!/[0-9]/.test(trimmed)) {
            messages.push('Password must contain digit');
        }

        if (!/[^a-zA-Z0-9]/.test(trimmed)) {
            messages.push('Password must contain special character');
        }

        if (messages.length > 0) {
            errors.password = messages;
        }
    }

    if (!Array.isArray(province_ids) || province_ids.length === 0) {
        errors.province_ids = ['At least one province must be selected'];
    }

    if (Object.keys(errors).length > 0) {
        throw new HttpException(errors, 422);
    }

    return {
        username: username.trim(),
        email: email.trim(),
        firstname: firstname.trim(),
        lastname: lastname.trim(),
        password: password.trim(),
        province_ids
    };
};

exports.validateUpdateSalesRequest = (body) => {
    const { email, firstname, lastname, password, province_ids } = body;
    const errors = {};

    if (!email || email.trim() === '') {
        errors.email = ['Email is required'];
    }

    if (!firstname || firstname.trim() === '') {
        errors.firstname = ['First name is required'];
    }

    if (!lastname || lastname.trim() === '') {
        errors.lastname = ['Last name is required'];
    }

    if (!password || password.trim().length === 0) {
        errors.password = ['Password is required'];
    } else {
        const trimmed = password.trim();

        const messages = [];

        if (trimmed.length < 8) {
            messages.push('Password must be at least 8 characters');
        }

        if (!/[A-Z]/.test(trimmed)) {
            messages.push('Password must contain uppercase letter');
        }

        if (!/[0-9]/.test(trimmed)) {
            messages.push('Password must contain digit');
        }

        if (!/[^a-zA-Z0-9]/.test(trimmed)) {
            messages.push('Password must contain special character');
        }

        if (messages.length > 0) {
            errors.password = messages;
        }
    }

    if (!Array.isArray(province_ids) || province_ids.length === 0) {
        errors.province_ids = ['At least one province must be selected'];
    }

    if (Object.keys(errors).length > 0) {
        throw new HttpException(errors, 422);
    }

    return {
        email: email.trim(),
        firstname: firstname.trim(),
        lastname: lastname.trim(),
        password: password.trim(),
        province_ids
    };
};
