jQuery(document).ready(function($) {
    var $commentForm = $('#commentform');
    var $submitButton = $commentForm.find('#submit');
    var $commentTextarea = $commentForm.find('#comment');
    var $messageContainer = $('<div class="thia-message-container"></div>');
    
    $commentForm.before($messageContainer);
    var isProcessing = false;
    
    $commentForm.on('submit', function(e) {
        var commentContent = $commentTextarea.val().trim();
        if (commentContent === '') {
            return true;
        }

        if (isProcessing) {
            e.preventDefault();
            return false;
        }
        
        e.preventDefault();
        isProcessing = true;
        
        showLoadingMessage('checking');
        $submitButton.prop('disabled', true);

        $.ajax({
            url: thia_ajax.ajax_url,
            type: 'POST',
            data: {
                action: 'thia_moderate_comment',
                nonce: thia_ajax.moderate_nonce,
                comment_content: commentContent,
                comment_author: $commentForm.find('#author').val() || '',
                comment_email: $commentForm.find('#email').val() || '',
                comment_url: $commentForm.find('#url').val() || '',
                comment_post_ID: $commentForm.find('#comment_post_ID').val() || 0
            },
            success: function(response) {
                if (response.success) {
                    var classification = response.data.classification;
                    var reason = response.data.reason || '';
                    var suggestion = response.data.suggestion || '';
                    
                    if (classification === 'safe') {
                        showResultMessage('approved');
                        
                        // Save the comment
                        $.ajax({
                            url: thia_ajax.ajax_url,
                            type: 'POST',
                            data: {
                                action: 'thia_save_comment',
                                nonce: thia_ajax.save_nonce,
                                comment_content: commentContent,
                                comment_author: $commentForm.find('#author').val() || '',
                                comment_email: $commentForm.find('#email').val() || '',
                                comment_url: $commentForm.find('#url').val() || '',
                                comment_post_ID: $commentForm.find('#comment_post_ID').val() || 0
                            },
                            success: function(saveResponse) {
                                if (saveResponse.success) {
                                    $commentTextarea.val('');
                                    if ($commentForm.find('#author').length) {
                                        $commentForm.find('#author').val('');
                                    }
                                    if ($commentForm.find('#email').length) {
                                        $commentForm.find('#email').val('');
                                    }
                                    if ($commentForm.find('#url').length) {
                                        $commentForm.find('#url').val('');
                                    }
                                    
                                    setTimeout(function() {
                                        location.reload();
                                    }, 1500);
                                } else {
                                    showResultMessage('error');
                                    $submitButton.prop('disabled', false);
                                    isProcessing = false;
                                }
                            },
                            error: function() {
                                showResultMessage('error');
                                $submitButton.prop('disabled', false);
                                isProcessing = false;
                            }
                        });
                    } else if (classification === 'warning') {
                        showWarningMessage(reason, suggestion);
                        $submitButton.prop('disabled', false);
                        isProcessing = false;
                    } else {
                        showResultMessage(classification);
                        $submitButton.prop('disabled', false);
                        isProcessing = false;
                        setTimeout(function() {
                            $messageContainer.empty();
                        }, 5000);
                    }
                } else {
                    showResultMessage('error');
                    $submitButton.prop('disabled', false);
                    isProcessing = false;
                    setTimeout(function() {
                        $messageContainer.empty();
                    }, 3000);
                }
            },
            error: function() {
                showResultMessage('error');
                $submitButton.prop('disabled', false);
                isProcessing = false;
                setTimeout(function() {
                    $messageContainer.empty();
                }, 3000);
            }
        });
    });
    
    function showLoadingMessage(type) {
        var message = thia_ajax.messages[type];
        $messageContainer.html(
            '<div class="thia-loading-indicator" id="thia-loading-status">' +
                '<div class="thia-spinner"></div>' +
                '<div class="thia-loading-text">' + message + '</div>' +
            '</div>'
        );
        // Стартираме пулс ефекта
        startPulseEffect();
    }
    
    function showResultMessage(type) {
    stopPulseEffect();
    
    var message = thia_ajax.messages[type] || thia_ajax.messages.error;
    var iconSvg = '';
    
    if (type === 'approved' || type === 'safe') {
        iconSvg = '<svg width="40" height="40" viewBox="0 0 40 40" fill="none">' +
            '<circle cx="20" cy="20" r="18" fill="#28a745" stroke="white" stroke-width="1.5"/>' +
            '<path d="M12 20 L17 25 L28 14" stroke="white" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>' +
            '</svg>';
    } else if (type === 'warning') {
        iconSvg = '<svg width="40" height="40" viewBox="0 0 40 40" fill="none">' +
            '<path d="M20 5 L35 32 L5 32 L20 5Z" fill="#ffc107" stroke="white" stroke-width="1.5"/>' +
            '<rect x="19" y="22" width="2" height="8" fill="white"/>' +
            '<circle cx="20" cy="32" r="1.5" fill="white"/>' +
            '</svg>';
    } else {
        // Fallback за blocked, spam, error
        iconSvg = '<svg width="40" height="40" viewBox="0 0 40 40" fill="none">' +
            '<circle cx="20" cy="20" r="18" fill="#dc3545" stroke="white" stroke-width="1.5"/>' +
            '<path d="M14 14 L26 26" stroke="white" stroke-width="2.5" stroke-linecap="round"/>' +
            '<path d="M26 14 L14 26" stroke="white" stroke-width="2.5" stroke-linecap="round"/>' +
            '</svg>';
    }
    
    $messageContainer.html(
        '<div class="thia-result-message ' + type + '">' +
            '<div class="thia-result-icon">' + iconSvg + '</div>' +
            '<div class="thia-result-text">' + message + '</div>' +
        '</div>'
    );
}
    
    function showWarningMessage(reason, suggestion) {
    stopPulseEffect();
    
    var message = '';
    if (reason) {
        message += '<strong>Thia Analysis:</strong> ' + reason + '<br>';
    }
    if (suggestion) {
        message += '<strong>Suggestion:</strong> ' + suggestion;
    } else {
        message += 'Please review your comment before posting.';
    }
    
    var iconSvg = '<svg width="40" height="40" viewBox="0 0 40 40" fill="none">' +
        '<path d="M20 5 L35 32 L5 32 L20 5Z" fill="#ffc107" stroke="white" stroke-width="1.5"/>' +
        '<rect x="19" y="22" width="2" height="8" fill="white"/>' +
        '<circle cx="20" cy="32" r="1.5" fill="white"/>' +
        '</svg>';
    
    $messageContainer.html(
        '<div class="thia-result-message warning">' +
            '<div class="thia-result-icon">' + iconSvg + '</div>' +
            '<div class="thia-result-text">' + message + '</div>' +
        '</div>'
    );
    
    setTimeout(function() {
        $messageContainer.fadeOut(300, function() {
            $(this).empty().show();
        });
    }, 18000);
}
});

// Пулс ефект - глобални функции
var pulseInterval = null;

function startPulseEffect() {
    var messageDiv = document.getElementById('thia-loading-status');
    if (!messageDiv) return;
    
    // Спираме ако вече работи
    if (pulseInterval) {
        clearInterval(pulseInterval);
        pulseInterval = null;
    }
    
    var step = 0;
    pulseInterval = setInterval(function() {
        if (!messageDiv || !messageDiv.parentNode) {
            clearInterval(pulseInterval);
            pulseInterval = null;
            return;
        }
        
        step++;
        var intensity = Math.sin(step * 0.3) * 0.5 + 0.5;
        var glow = 5 + (intensity * 13);
        
        messageDiv.style.transition = 'all 0.1s ease';
        messageDiv.style.boxShadow = '0 0 ' + glow + 'px rgba(0, 255, 255, 0.8)';
        messageDiv.style.border = '1px solid rgba(0, 255, 255, ' + (0.3 + intensity * 0.7) + ')';
        messageDiv.style.borderRadius = '12px';
    }, 100);
}

function stopPulseEffect() {
    if (pulseInterval) {
        clearInterval(pulseInterval);
        pulseInterval = null;
    }
    
    var messageDiv = document.getElementById('thia-loading-status');
    if (messageDiv) {
        messageDiv.style.boxShadow = '';
        messageDiv.style.border = '';
        messageDiv.style.borderRadius = '';
        messageDiv.style.transition = '';
    }
}