/**
 * Affiliate Registration Form
 *
 * Handles password strength validation and AJAX form submission
 * for the affiliate registration form.
 *
 * @package SubscriptionManagerForStripe
 */

/* global jQuery, wp, lucide, strp_sub_affiliate_params */
jQuery(document).ready(function($) {
    var $form = $('#strp-sub-affiliate-registration-form');
    var $password = $('#affiliate_password');
    var $meter = $('#affiliate-password-strength-meter');
    var $submitBtn = $('#strp-sub-register-affiliate-btn');
    var $btnText = $submitBtn.find('.btn-text');
    var $btnLoader = $submitBtn.find('.btn-loader');

    // Password strength validation
    function validatePassword() {
        var pw = $password.val();

        if (pw.length === 0) {
            $meter.hide();
            $submitBtn.prop('disabled', true);
        } else {
            $meter.show();

            var score = 0;
            if (typeof wp !== 'undefined' && wp.passwordStrength && typeof wp.passwordStrength.meter === 'function') {
                var email = $('#affiliate_email').val();
                var firstName = $('#affiliate_first_name').val();
                var lastName = $('#affiliate_last_name').val();
                var blocklist = [email, firstName, lastName].filter(Boolean);
                score = wp.passwordStrength.meter(pw, blocklist);
            } else {
                if (pw.length < 8) {
                    score = 0;
                } else {
                    var hasUpper = /[A-Z]/.test(pw);
                    var hasLower = /[a-z]/.test(pw);
                    var hasNumber = /[0-9]/.test(pw);
                    var hasSpecial = /[^A-Za-z0-9]/.test(pw);

                    var complexity = 0;
                    if (hasUpper) complexity++;
                    if (hasLower) complexity++;
                    if (hasNumber) complexity++;
                    if (hasSpecial) complexity++;

                    if (pw.length >= 8 && complexity >= 2) score = 3;
                    else if (pw.length >= 8 && complexity >= 1) score = 2;
                    else if (pw.length >= 8) score = 1;
                    else score = 0;
                }
            }

            var labels = [
                strp_sub_affiliate_params.i18n_very_weak,
                strp_sub_affiliate_params.i18n_weak,
                strp_sub_affiliate_params.i18n_medium,
                strp_sub_affiliate_params.i18n_strong,
                strp_sub_affiliate_params.i18n_strong
            ];
            $meter.html('<div>' + labels[score] + '</div>')
                .removeClass('strength-0 strength-1 strength-2 strength-3 strength-4')
                .addClass('strength-' + score);

            var isStrong = score >= 3;
            $submitBtn.prop('disabled', !isStrong);
        }
    }

    $password.on('input', validatePassword);
    $('#affiliate_first_name, #affiliate_last_name, #affiliate_email').on('input', validatePassword);

    function showFormError(msg) {
        $form.find('.strp-sub-form-error').remove();
        var $err = $('<div class="strp-sub-form-error" style="color:#991b1b;background:#fef2f2;border:1px solid #fecaca;padding:10px 14px;border-radius:4px;margin-bottom:12px;font-size:14px;"></div>').text(msg);
        $form.prepend($err);
        setTimeout(function() { $err.fadeOut(400, function() { $(this).remove(); }); }, 6000);
    }

    // Form submission
    $form.on('submit', function(e) {
        e.preventDefault();

        $submitBtn.prop('disabled', true);
        $btnText.hide();
        $btnLoader.show();

        if (typeof lucide !== 'undefined') {
            lucide.createIcons();
        }

        var formData = new FormData(this);
        formData.append('action', 'strp_sub_register_affiliate');

        $.ajax({
            url: strp_sub_affiliate_params.ajax_url,
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                if (response.success) {
                    window.location.href = strp_sub_affiliate_params.redirect_url;
                } else {
                    showFormError(response.data.message || strp_sub_affiliate_params.i18n_error);
                    $submitBtn.prop('disabled', false);
                    $btnText.show();
                    $btnLoader.hide();
                }
            },
            error: function() {
                showFormError(strp_sub_affiliate_params.i18n_error);
                $submitBtn.prop('disabled', false);
                $btnText.show();
                $btnLoader.hide();
            }
        });
    });
});
