// Add password requirement + confirmation match hint (Employer + Freelancer)
add_action( 'wp_footer', 'freeio_password_requirement_script' );
function freeio_password_requirement_script() {
    ?>
    <script>
    document.addEventListener("DOMContentLoaded", function() {

        // Reusable function for password + confirm hints
        function setupPasswordValidation(passwordSelector, confirmSelector) {
            const passwordField = document.querySelector(passwordSelector);
            const confirmField = document.querySelector(confirmSelector);

            if (passwordField) {
                // Create password strength hint
                let hint = document.createElement('div');
                hint.className = 'password-hint';
                hint.style.fontSize = '12px';
                hint.style.color = '#666';
                hint.style.marginTop = '4px';
                hint.innerHTML = "⚠️ Le mot de passe doit contenir au moins 8 caractères, dont une majuscule, une minuscule et un chiffre.";

                passwordField.parentNode.insertBefore(hint, passwordField.nextSibling);

                // Live validation feedback for password strength
                passwordField.addEventListener('input', function() {
                    let value = passwordField.value;
                    let valid = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/.test(value);
                    hint.style.color = valid ? "green" : "#d9534f";
                    hint.innerHTML = valid 
                        ? "✅ Mot de passe fort." 
                        : "⚠️ Le mot de passe doit contenir au moins 8 caractères, dont une majuscule, une minuscule et un chiffre.";
                });
            }

            if (passwordField && confirmField) {
                // Create confirm password hint
                let confirmHint = document.createElement('div');
                confirmHint.className = 'confirm-hint';
                confirmHint.style.fontSize = '12px';
                confirmHint.style.marginTop = '4px';
                confirmHint.style.color = '#666';
                confirmHint.innerHTML = "Veuillez confirmer votre mot de passe.";

                confirmField.parentNode.insertBefore(confirmHint, confirmField.nextSibling);

                // Live validation feedback for match
                function checkMatch() {
                    if (confirmField.value.length > 0) {
                        if (passwordField.value === confirmField.value) {
                            confirmHint.style.color = "green";
                            confirmHint.innerHTML = "✅ Les mots de passe correspondent.";
                        } else {
                            confirmHint.style.color = "#d9534f";
                            confirmHint.innerHTML = "❌ Les mots de passe ne correspondent pas.";
                        }
                    } else {
                        confirmHint.style.color = "#666";
                        confirmHint.innerHTML = "Veuillez confirmer votre mot de passe.";
                    }
                }

                passwordField.addEventListener('input', checkMatch);
                confirmField.addEventListener('input', checkMatch);
            }
        }

        // Apply to Employer fields
        setupPasswordValidation('input[name="_employer_password"]', 'input[name="_employer_confirmpassword"]');

        // Apply to Freelancer fields
        setupPasswordValidation('input[name="_freelancer_password"]', 'input[name="_freelancer_confirmpassword"]');

    });
    </script>
    <style>
      .password-hint, .confirm-hint { font-size: 12px; margin-top: 3px; }
    </style>
    <?php
}
