jQuery(document).ready(function($) {
    'use strict';
    
    $('#user_login').on('blur', function() {
        var username = $(this).val().trim();
        
        if (username.length < 1) {
            $('#moesec-2fa-field').hide();
            $('#moesec-2fa-notice').hide();
            return;
        }
        
        console.log('Checking 2FA status for: ' + username);
        
        $.ajax({
            url: moesec_2fa.ajax_url,
            type: 'POST',
            data: {
                action: 'moesec_check_2fa',
                username: username,
                nonce: moesec_2fa.nonce
            },
            success: function(response) {
                console.log('2FA check response:', response);
                
                if (response.success && response.data.enabled) {
                    $('#moesec-2fa-field').slideDown();
                    $('#moesec-2fa-notice').slideDown();
                    
                    var methodText = response.data.method === 'email' 
                        ? 'A verification code has been sent to your email address.' 
                        : 'Enter the 6-digit code from your authenticator app.';
                    
                    $('#moesec-method-text').text(methodText);
                    
                    setTimeout(function() {
                        $('#moesec_2fa_code').focus().select();
                    }, 100);
                    
                } else {
                    $('#moesec-2fa-field').slideUp();
                    $('#moesec-2fa-notice').slideUp();
                }
            },
            error: function(xhr, status, error) {
                console.error('2FA check failed:', error);
                $('#moesec-2fa-field').hide();
                $('#moesec-2fa-notice').hide();
            }
        });
    });
    
    $('#moesec_2fa_code').on('input', function() {
        var value = $(this).val().replace(/[^0-9]/g, '');
        if (value.length > 6) {
            value = value.substr(0, 6);
        }
        $(this).val(value);
    });
    
    $('#user_login').on('keypress', function(e) {
        if (e.which === 13) { // Enter key
            $(this).trigger('blur');
        }
    });
    
    $('#loginform').on('submit', function(e) {
        var $codeField = $('#moesec_2fa_code');
        
        if ($codeField.is(':visible') && $codeField.val().length !== 6) {
            e.preventDefault();
            $codeField.addClass('error').focus();
            
            $codeField.css('animation', 'none');
            setTimeout(function() {
                $codeField.css('animation', 'shake 0.5s');
            }, 10);
            
            return false;
        }
    });
    
    if (!$('#moesec-2fa-css').length) {
        $('head').append('<style id="moesec-2fa-css">\
            @keyframes shake {\
                0%, 100% { transform: translateX(0); }\
                10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }\
                20%, 40%, 60%, 80% { transform: translateX(5px); }\
            }\
            #moesec_2fa_code.error {\
                border-color: #dc3232 !important;\
                background-color: #ffeaea !important;\
            }\
        </style>');
    }
});
