/**
 * Deactivation Feedback Popup
 * Shows a popup to collect feedback when user tries to deactivate the plugin
 */
(function ($) {
    'use strict';

    var CSCF_Deactivation = {
        pluginSlug: 'cross-site-copy-field-for-acf',

        init: function () {
            this.createModal();
            this.bindEvents();
        },

        createModal: function () {
            var modalHTML = `
                <div id="cscf-deactivation-modal" class="cscf-deact-overlay" style="display:none;">
                    <div class="cscf-deact-modal">
                        <div class="cscf-deact-header">
                            <h3>Deactivation Reason</h3>
                            <button type="button" class="cscf-deact-close">&times;</button>
                        </div>
                        <div class="cscf-deact-body">
                            <div class="cscf-deact-reasons">
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="debugging">
                                    <span class="cscf-reason-icon dashicons dashicons-hammer"></span>
                                    <span class="cscf-reason-text">Just Debugging</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="plugin_issues">
                                    <span class="cscf-reason-icon dashicons dashicons-warning"></span>
                                    <span class="cscf-reason-text">Plugin Issues</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="slow_performance">
                                    <span class="cscf-reason-icon dashicons dashicons-performance"></span>
                                    <span class="cscf-reason-text">Slow Performance</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="switched_alternative">
                                    <span class="cscf-reason-icon dashicons dashicons-randomize"></span>
                                    <span class="cscf-reason-text">Switched to Alternative</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="no_longer_needed">
                                    <span class="cscf-reason-icon dashicons dashicons-dismiss"></span>
                                    <span class="cscf-reason-text">No Longer Needed</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="compatibility_issues">
                                    <span class="cscf-reason-icon dashicons dashicons-no-alt"></span>
                                    <span class="cscf-reason-text">Compatibility Issues</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="missing_feature">
                                    <span class="cscf-reason-icon dashicons dashicons-star-filled"></span>
                                    <span class="cscf-reason-text">Missing Feature</span>
                                </label>
                                <label class="cscf-reason-option">
                                    <input type="radio" name="cscf_deact_reason" value="other">
                                    <span class="cscf-reason-icon dashicons dashicons-editor-help"></span>
                                    <span class="cscf-reason-text">Other Reason</span>
                                </label>
                            </div>
                            <div class="cscf-deact-details" style="display:none;">
                                <textarea id="cscf-deact-details-text" placeholder="Please share more details (optional)..." rows="3"></textarea>
                            </div>
                            <div class="cscf-deact-info">
                                <p>After submitting, we'll get in touch with you via email to provide the support you need. If you require any help, please <a href="https://wordpress.org/support/plugin/cross-site-copy-field-for-acf/" target="_blank">Create A Ticket</a>, we reply within 24 working hours.</p>
                            </div>
                            <div class="cscf-deact-consent">
                                <label>
                                    <input type="checkbox" id="cscf-deact-consent-check">
                                    <span>I agree to be contacted via email for support with this plugin.</span>
                                </label>
                            </div>
                        </div>
                        <div class="cscf-deact-footer">
                            <button type="button" class="cscf-deact-skip">Skip & Deactivate</button>
                            <button type="button" class="cscf-deact-submit" disabled>Submit & Deactivate</button>
                        </div>
                    </div>
                </div>
            `;
            $('body').append(modalHTML);
        },

        bindEvents: function () {
            var self = this;

            // Intercept deactivation link click
            $(document).on('click', 'tr[data-slug="' + this.pluginSlug + '"] .deactivate a', function (e) {
                e.preventDefault();
                self.deactivateUrl = $(this).attr('href');
                self.showModal();
            });

            // Close modal
            $(document).on('click', '.cscf-deact-close, .cscf-deact-overlay', function (e) {
                if (e.target === this) {
                    self.hideModal();
                }
            });

            // Prevent modal close when clicking inside
            $(document).on('click', '.cscf-deact-modal', function (e) {
                e.stopPropagation();
            });

            // Reason selection
            $(document).on('change', 'input[name="cscf_deact_reason"]', function () {
                // Show textarea for all selections
                $('.cscf-deact-details').slideDown(200);
                self.updateSubmitButtonState();
            });

            // Consent checkbox change
            $(document).on('change', '#cscf-deact-consent-check', function () {
                self.updateSubmitButtonState();
            });

            // Skip & Deactivate
            $(document).on('click', '.cscf-deact-skip', function () {
                window.location.href = self.deactivateUrl;
            });

            // Submit & Deactivate
            $(document).on('click', '.cscf-deact-submit', function () {
                self.submitFeedback();
            });

            // ESC key to close
            $(document).on('keyup', function (e) {
                if (e.key === 'Escape') {
                    self.hideModal();
                }
            });
        },

        updateSubmitButtonState: function () {
            var reasonSelected = $('input[name="cscf_deact_reason"]:checked').length > 0;
            // var consentChecked = $('#cscf-deact-consent-check').is(':checked');
            // Enable submit if just reason is selected
            $('.cscf-deact-submit').prop('disabled', !reasonSelected);
        },

        showModal: function () {
            $('#cscf-deactivation-modal').fadeIn(200);
            $('body').css('overflow', 'hidden');
        },

        hideModal: function () {
            $('#cscf-deactivation-modal').fadeOut(200);
            $('body').css('overflow', '');
            this.resetModal();
        },

        resetModal: function () {
            $('input[name="cscf_deact_reason"]').prop('checked', false);
            $('#cscf-deact-details-text').val('');
            $('#cscf-deact-consent-check').prop('checked', false);
            $('.cscf-deact-details').hide();
            $('.cscf-deact-submit').prop('disabled', true);
        },

        submitFeedback: function () {
            var self = this;
            var reason = $('input[name="cscf_deact_reason"]:checked').val();
            var details = $('#cscf-deact-details-text').val();
            var consent = $('#cscf-deact-consent-check').is(':checked');

            // Show loading state
            $('.cscf-deact-submit').text('Submitting...').prop('disabled', true);

            // Send feedback via AJAX
            $.ajax({
                url: cscf_deactivation.ajax_url,
                type: 'POST',
                data: {
                    action: 'cscf_deactivation_feedback',
                    nonce: cscf_deactivation.nonce,
                    reason: reason,
                    details: details,
                    consent: consent ? 'yes' : 'no'
                },
                complete: function () {
                    // Redirect to deactivate regardless of response
                    window.location.href = self.deactivateUrl;
                }
            });
        }
    };

    $(document).ready(function () {
        CSCF_Deactivation.init();
    });

})(jQuery);
