(function ($) {
    'use strict';

    $(document).ready(function () {
        const installer = {
            container: $('.sifency-installer-cards-container'),

            init: function () {
                if (this.container.length) {
                    this.bindEvents();
                }
            },

            bindEvents: function () {
                this.container.on('click', '.plugin-action-btn', this.handleButtonClick.bind(this));
            },

            handleButtonClick: function (e) {
                e.preventDefault();
                
                const $button = $(e.currentTarget);

                if ($button.hasClass('disabled') || $button.hasClass('processing')) {
                    return;
                }

                const pluginData = {
                    slug: $button.data('slug'),
                    mainFile: $button.data('mainfile'),
                    action: $button.data('action'),
                    source: $button.data('source'),
                    downloadUrl: $button.data('download-url')
                };
                
                const originalText = $button.find('.btn-text').text();

                this.setButtonState($button, 'processing');
                this.performAjaxRequest($button, pluginData, originalText);
            },

            performAjaxRequest: function ($button, pluginData, originalText) {
                $.ajax({
                    url: sifencyInstaller.ajax_url,
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        action: 'sifency_install_plugin',
                        nonce: sifencyInstaller.nonce,
                        task: pluginData.action,
                        slug: pluginData.slug,
                        main_file: pluginData.mainFile,
                        source: pluginData.source,
                        download_url: pluginData.downloadUrl
                    },
                    success: (response) => {
                        if (response.success) {
                            this.setButtonState($button, 'activated');
                        } else {
                            alert(response.data.message || sifencyInstaller.texts.error);
                            this.setButtonState($button, 'error', originalText);
                        }
                    },
                    error: (xhr) => {
                        alert(sifencyInstaller.texts.error);
                        console.error('AJAX Error:', xhr.responseText);
                        this.setButtonState($button, 'error', originalText);
                    }
                });
            },
            
            setButtonState: function ($button, state, originalText = '') {
                const $btnText = $button.find('.btn-text');
                const action = $button.data('action');

                $button.removeClass('processing').prop('disabled', false);

                switch (state) {
                    case 'processing':
                        $button.addClass('processing').prop('disabled', true);
                        $btnText.text(action === 'install' ? sifencyInstaller.texts.installing : sifencyInstaller.texts.activating);
                        break;
                        
                    case 'activated':
                        $button.removeClass('install-btn activate-btn').addClass('activated-btn disabled').prop('disabled', true);
                        $button.data('action', '');
                        $btnText.text(sifencyInstaller.texts.activated);
                        break;
                        
                    case 'error':
                        $button.removeClass('processing');
                        $btnText.text(originalText);
                        break;
                }
            }
        };

        installer.init();
    });

})(jQuery);