/**
 * AI eShop Chat - Admin JavaScript
 * Additional admin functionality
 *
 * @package AI_eShop_Chat
 */

(function($) {
    'use strict';

    var __ = ( window.wp && window.wp.i18n && window.wp.i18n.__ ) ? window.wp.i18n.__ : function (s) { return s; };
    var _x = ( window.wp && window.wp.i18n && window.wp.i18n._x ) ? window.wp.i18n._x : function (s) { return s; };
    var _n = ( window.wp && window.wp.i18n && window.wp.i18n._n ) ? window.wp.i18n._n : function (s, p, n) { return n === 1 ? s : p; };
    var _sprintf = ( window.wp && window.wp.i18n && window.wp.i18n.sprintf ) ? window.wp.i18n.sprintf : function (s) { return s; };

    $(document).ready(function() {
        // API Key validation
        $('#aieo_chat_anthropic_api_key, #aieo_chat_voyage_api_key').on('blur', function() {
            const $field = $(this);
            const value = $field.val().trim();

            if (value && value.length < 20) {
                $field.css('border-color', '#DC2626');
                $field.next('.description').append(
                    '<span class="error" style="color: #DC2626; display: block; margin-top: 5px;">⚠️ ' + __( 'API key seems invalid', 'ai-eshop-optimizer' ) + '</span>'
                );
            } else {
                $field.css('border-color', '');
                $field.next('.description').find('.error').remove();
            }
        });

        // Temperature slider visual feedback
        $('#aieo_chat_temperature').on('input', function() {
            const val = parseFloat($(this).val());
            let color = '#03163c';

            if (val < 0.3) {
                color = '#3B82F6'; // Blue for focused
            } else if (val > 0.7) {
                color = '#EF4444'; // Red for creative
            }

            $(this).css('border-color', color);
        });

        // Character counter enhancement
        $('#aieo_chat_custom_prompt').on('input', function() {
            const $textarea = $(this);
            const length = $textarea.val().length;
            const maxLength = 6000;
            const percentage = (length / maxLength) * 100;

            let color = '#10B981'; // Green
            if (percentage > 80) {
                color = '#F59E0B'; // Orange
            }
            if (percentage > 95) {
                color = '#EF4444'; // Red
            }

            $textarea.css('border-color', color);
        });

        // Animate stats on page load
        $('[style*="grid-template-columns"] > div').each(function(index) {
            $(this).css({
                opacity: 0,
                transform: 'translateY(20px)'
            }).delay(index * 100).animate({
                opacity: 1
            }, 500).css({
                transform: 'translateY(0)'
            });
        });

        // Confirmation for potentially destructive actions
        $('button[name*="delete"], button[name*="clear"]').on('click', function(e) {
            if (!confirm( __( 'Are you sure? This action cannot be undone.', 'ai-eshop-optimizer' ) )) {
                e.preventDefault();
                return false;
            }
        });

        // Show success message animation
        $('.notice-success').hide().slideDown(300);

        // Auto-hide notices after 5 seconds
        setTimeout(function() {
            $('.notice-success').slideUp(300);
        }, 5000);
    });

})(jQuery);
