(function($) {
    'use strict';

    var ie = window.seoFuryIE || {};

    // Export
    $('#seo-fury-export-btn').on('click', function() {
        var $btn = $(this);
        var $result = $('#seo-fury-ie-result');

        $btn.prop('disabled', true).text(ie.i18n.exporting || 'Exporting...');

        $.ajax({
            url: ie.ajax_url,
            type: 'POST',
            data: {
                action: 'seo_fury_export_settings',
                nonce: ie.nonce
            },
            success: function(response) {
                if (response.success) {
                    var blob = new Blob([JSON.stringify(response.data.data, null, 2)], { type: 'application/json' });
                    var url = URL.createObjectURL(blob);
                    var a = document.createElement('a');
                    a.href = url;
                    a.download = response.data.filename;
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                    URL.revokeObjectURL(url);

                    $result.html('<div style="background:#ECFDF5;border:1px solid #10B981;border-radius:6px;padding:10px 14px;color:#065F46;">&#10004; ' + (ie.i18n.exportSuccess || 'Settings exported successfully!') + '</div>');
                } else {
                    $result.html('<div style="background:#FEF2F2;border:1px solid #EF4444;border-radius:6px;padding:10px 14px;color:#991B1B;">&#10006; ' + response.data.message + '</div>');
                }
            },
            error: function() {
                $result.html('<div style="background:#FEF2F2;border:1px solid #EF4444;border-radius:6px;padding:10px 14px;color:#991B1B;">&#10006; ' + (ie.i18n.networkError || 'Network error') + '</div>');
            },
            complete: function() {
                $btn.prop('disabled', false).html('<span class="dashicons dashicons-download" style="margin-top:4px;"></span> ' + (ie.i18n.exportBtn || 'Export Settings'));
            }
        });
    });

    // Import - choose file
    $('#seo-fury-import-choose-btn').on('click', function() {
        $('#seo-fury-import-file').trigger('click');
    });

    var importFileData = null;

    $('#seo-fury-import-file').on('change', function() {
        var file = this.files[0];
        if (!file) return;

        if (!file.name.endsWith('.json')) {
            $('#seo-fury-ie-result').html('<div style="background:#FEF2F2;border:1px solid #EF4444;border-radius:6px;padding:10px 14px;color:#991B1B;">&#10006; ' + (ie.i18n.invalidFile || 'Please select a JSON file') + '</div>');
            return;
        }

        var reader = new FileReader();
        reader.onload = function(e) {
            importFileData = e.target.result;
            $('#seo-fury-import-filename').text(file.name + ' (' + (file.size / 1024).toFixed(1) + ' KB)');
            $('#seo-fury-import-btn').show();
        };
        reader.readAsText(file);
    });

    // Import - run
    $('#seo-fury-import-btn').on('click', function() {
        if (!importFileData) return;

        if (!confirm(ie.i18n.confirmImport || 'This will overwrite your current settings. Continue?')) {
            return;
        }

        var $btn = $(this);
        var $result = $('#seo-fury-ie-result');

        $btn.prop('disabled', true).text(ie.i18n.importing || 'Importing...');

        $.ajax({
            url: ie.ajax_url,
            type: 'POST',
            data: {
                action: 'seo_fury_import_settings',
                nonce: ie.nonce,
                import_data: importFileData
            },
            success: function(response) {
                if (response.success) {
                    $result.html('<div style="background:#ECFDF5;border:1px solid #10B981;border-radius:6px;padding:10px 14px;color:#065F46;">&#10004; ' + response.data.message + '</div>');
                    importFileData = null;
                    $('#seo-fury-import-filename').text('');
                    $('#seo-fury-import-btn').hide();
                    $('#seo-fury-import-file').val('');
                } else {
                    $result.html('<div style="background:#FEF2F2;border:1px solid #EF4444;border-radius:6px;padding:10px 14px;color:#991B1B;">&#10006; ' + response.data.message + '</div>');
                }
            },
            error: function() {
                $result.html('<div style="background:#FEF2F2;border:1px solid #EF4444;border-radius:6px;padding:10px 14px;color:#991B1B;">&#10006; ' + (ie.i18n.networkError || 'Network error') + '</div>');
            },
            complete: function() {
                $btn.prop('disabled', false).text(ie.i18n.importBtn || 'Import Settings');
            }
        });
    });

})(jQuery);
