<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Vayu Blocks – Deactivation Feedback Popup
 * Shows a "Quick Feedback" modal when the user deactivates the plugin,
 * collects the reason, and sends it via REST API before deactivating.
 */

/* =========================================================
 * 1. ENQUEUE SCRIPTS / STYLES on the Plugins admin page
 * ========================================================= */
add_action( 'admin_enqueue_scripts', 'vayu_blocks_deactivate_feedback_assets' );
function vayu_blocks_deactivate_feedback_assets( $hook ) {
    if ( $hook !== 'plugins.php' ) return;

    wp_enqueue_style(
        'vayu-blocks-deactivate-feedback-css',
        VAYU_BLOCKS_URL . 'inc/assets/css/deactivate-feedback.css',
        array(),
        VAYU_BLOCKS_VERSION
    );
    wp_enqueue_script(
        'vayu-blocks-deactivate-feedback-js',
        VAYU_BLOCKS_URL . 'inc/assets/js/deactivate-feedback.js',
        array( 'jquery' ),
        VAYU_BLOCKS_VERSION,
        true
    );

    $plugin_file = VAYU_BLOCKS_DIR_PATH . 'vayu-blocks.php';
    $plugin_data = get_plugin_data( $plugin_file, false, false );

    wp_localize_script( 'vayu-blocks-deactivate-feedback-js', 'vayuBlocksDeactivate', array(
        'pluginFile'    => 'vayu-blocks/vayu-blocks.php',
        'apiUrl'        => rest_url( 'vayu-blocks/v1/deactivate-feedback' ),
        'nonce'         => wp_create_nonce( 'wp_rest' ),
        'pluginName'    => $plugin_data['Name'],
        'pluginVersion' => $plugin_data['Version'],
        'i18n'          => array(
            'submitting' => __( 'Submitting…', 'vayu-blocks' ),
            'submit'     => __( 'Submit & Deactivate', 'vayu-blocks' ),
        ),
    ) );
}

/* =========================================================
 * 2. RENDER MODAL HTML in admin footer (plugins page only)
 * ========================================================= */
add_action( 'admin_footer', 'vayu_blocks_deactivate_feedback_modal' );
function vayu_blocks_deactivate_feedback_modal() {
    global $hook_suffix;
    if ( 'plugins.php' !== $hook_suffix ) return;

    $reasons = array(
        'no_longer_needed'   => __( 'I no longer need the plugin', 'vayu-blocks' ),
        'found_better'       => __( 'I found a better plugin', 'vayu-blocks' ),
        'not_working'        => __( 'I couldn\'t get the plugin to work', 'vayu-blocks' ),
        'temporary'          => __( 'It\'s a temporary deactivation', 'vayu-blocks' ),
        'have_premium'       => __( 'I have the premium plugin', 'vayu-blocks' ),
        'missing_feature'    => __( 'Plugin is missing a required feature', 'vayu-blocks' ),
        'other'              => __( 'Other', 'vayu-blocks' ),
    );
    ?>
    <div id="vayu-blocks-deactivate-overlay" class="vayu-df-overlay" style="display:none;" role="dialog" aria-modal="true" aria-labelledby="vayu-df-title">
        <div class="vayu-df-modal">

            <div class="vayu-df-header">
                <span class="vayu-df-icon">
                    <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <rect width="20" height="20" rx="4" fill="#7B68EE"/>
                        <text x="10" y="15" text-anchor="middle" font-size="13" font-weight="bold" fill="#fff">!</text>
                    </svg>
                </span>
                <strong id="vayu-df-title"><?php esc_html_e( 'QUICK FEEDBACK', 'vayu-blocks' ); ?></strong>
            </div>

            <div class="vayu-df-body">
                <p><?php esc_html_e( 'If you have a moment, please share why you are deactivating Vayu Blocks:', 'vayu-blocks' ); ?></p>

                <ul class="vayu-df-reasons">
                    <?php foreach ( $reasons as $value => $label ) : ?>
                        <li>
                            <label>
                                <input type="radio" name="vayu_deactivate_reason" value="<?php echo esc_attr( $value ); ?>" />
                                <span><?php echo esc_html( $label ); ?></span>
                            </label>
                        </li>
                    <?php endforeach; ?>
                </ul>

                <div class="vayu-df-detail" id="vayu-df-detail-wrap" style="display:none;">
                    <textarea id="vayu-df-detail-text" rows="3" placeholder="<?php esc_attr_e( 'Please share more details (optional)…', 'vayu-blocks' ); ?>"></textarea>
                </div>
            </div>

            <div class="vayu-df-footer">
                <button type="button" id="vayu-df-submit" class="button button-primary vayu-df-submit-btn">
                    <?php esc_html_e( 'Submit & Deactivate', 'vayu-blocks' ); ?>
                </button>
                <a href="#" id="vayu-df-skip" class="vayu-df-skip-link">
                    <?php esc_html_e( 'Skip & Deactivate', 'vayu-blocks' ); ?>
                </a>
            </div>

        </div>
    </div>
    <?php
}

/* =========================================================
 * 3. REST API ROUTE – receive feedback
 * ========================================================= */
add_action( 'rest_api_init', 'vayu_blocks_deactivate_feedback_rest_route' );
function vayu_blocks_deactivate_feedback_rest_route() {
    register_rest_route( 'vayu-blocks/v1', '/deactivate-feedback', array(
        'methods'             => WP_REST_Server::CREATABLE,
        'callback'            => 'vayu_blocks_rest_save_deactivate_feedback',
        'permission_callback' => function() {
            return current_user_can( 'manage_options' );
        },
        'args' => array(
            'reason' => array(
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'required'          => true,
            ),
            'details' => array(
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_textarea_field',
                'default'           => '',
            ),
            'site_url' => array(
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'default'           => '',
            ),
            'plugin_version' => array(
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'default'           => '',
            ),
            'plugin_name' => array(
                'type'              => 'string',
                'sanitize_callback' => 'sanitize_text_field',
                'default'           => '',
            ),
        ),
    ) );
}

function vayu_blocks_rest_save_deactivate_feedback( $request ) {
    $data = array(
        'reason'         => $request->get_param( 'reason' ),
        'details'        => $request->get_param( 'details' ),
        'site_url'       => $request->get_param( 'site_url' ),
        'plugin_version' => $request->get_param( 'plugin_version' ),
        'plugin_name'    => $request->get_param( 'plugin_name' ),
    );

    // Send to remote ThemeHunk server
    wp_remote_post(
        'https://themehunk.com/wp-json/wp/v2/themehunk/feedback',
        array(
            'method'   => 'POST',
            'timeout'  => 15,
            'blocking' => true,
            'headers'  => array(
                'Content-Type' => 'application/json',
                'Accept'       => 'application/json',
            ),
            'body'     => wp_json_encode( $data ),
        )
    );

    return rest_ensure_response( array(
        'success' => true,
        'message' => __( 'Thank you for your feedback!', 'vayu-blocks' ),
    ) );
}
