<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://profiles.wordpress.org/nishatbd31/
 * @since      1.0.0
 *
 * @package    Autocomplete_Google_Address
 * @subpackage Autocomplete_Google_Address/admin
 */

defined( 'ABSPATH' ) || exit;

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Autocomplete_Google_Address
 * @subpackage Autocomplete_Google_Address/admin
 * @author     Md Nishath Khandakar <https://profiles.wordpress.org/nishatbd31/>
 */
class AGA_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $is_plugin_page = ( 'aga_form' === $screen->post_type ) ||
                          ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'aga-settings', 'aga-help', 'aga-wizard', 'aga-analytics' ), true ) );

        if ( ! $is_plugin_page ) {
            return;
        }

		wp_enqueue_style( $this->plugin_name, AGA_PLUGIN_URL . 'admin/css/admin.css', array(), filemtime( AGA_PLUGIN_DIR . 'admin/css/admin.css' ), 'all' );
        wp_enqueue_style( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css' );

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $is_plugin_page = ( 'aga_form' === $screen->post_type ) ||
                          ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'aga-settings', 'aga-help', 'aga-wizard', 'aga-analytics' ), true ) );

        if ( ! $is_plugin_page ) {
            return;
        }

		wp_enqueue_script( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js', array( 'jquery' ), null, true );
		wp_enqueue_style( 'wp-pointer' );
		wp_enqueue_script( 'wp-pointer' );
		wp_enqueue_script( $this->plugin_name, AGA_PLUGIN_URL . 'admin/js/admin.js', array( 'jquery', 'select2' ), filemtime( AGA_PLUGIN_DIR . 'admin/js/admin.js' ), true );

        $settings = get_option( 'Nish_aga_settings' );
        wp_localize_script( $this->plugin_name, 'aga_admin_data', array(
            'ajax_url'  => admin_url( 'admin-ajax.php' ),
            'nonce'     => wp_create_nonce( 'aga_admin_nonce' ),
            'is_paying' => function_exists( 'google_autocomplete' ) && google_autocomplete()->is_paying(),
            'api_key'   => isset( $settings['api_key'] ) ? $settings['api_key'] : '',
        ) );

        // Visual Selector Tool (Pro only, form edit screen only).
        $is_paying = function_exists( 'google_autocomplete' ) && google_autocomplete()->is_paying();
        if ( $is_paying && 'aga_form' === $screen->post_type ) {
            wp_enqueue_script(
                'aga-visual-selector',
                AGA_PLUGIN_URL . 'admin/js/visual-selector.js',
                array( 'jquery' ),
                filemtime( AGA_PLUGIN_DIR . 'admin/js/visual-selector.js' ),
                true
            );

            $wc_checkout_url = '';
            if ( function_exists( 'wc_get_checkout_url' ) ) {
                $wc_checkout_url = wc_get_checkout_url();
            }

            wp_localize_script( 'aga-visual-selector', 'aga_vst_data', array(
                'home_url'        => home_url( '/' ),
                'ajax_url'        => admin_url( 'admin-ajax.php' ),
                'nonce'           => wp_create_nonce( 'aga_vst_nonce' ),
                'wc_checkout_url' => $wc_checkout_url,
            ) );
        }
	}
    
        public function add_admin_menu() {
            // The Custom Post Type itself creates the top-level menu due to show_in_menu => true.
            // We only need to add the sub-menus now.
    
            // 'All Configurations' submenu is automatically generated by WordPress for the CPT,
            // so we don't need to add it manually here.
    
            // Add 'Add New' submenu.
;
    
            // Add 'Settings' submenu.
            add_submenu_page(
                'edit.php?post_type=aga_form', // Parent is the CPT's main menu slug
                __( 'Settings', 'autocomplete-google-address' ),
                __( 'Settings', 'autocomplete-google-address' ),
                'manage_options',
                'aga-settings',
                array( $this, 'render_settings_page' )
            );
    
            // Add 'Analytics' submenu (Pro).
            add_submenu_page(
                'edit.php?post_type=aga_form',
                __( 'Analytics', 'autocomplete-google-address' ),
                __( 'Analytics', 'autocomplete-google-address' ),
                'manage_options',
                'aga-analytics',
                array( $this, 'render_analytics_page' )
            );

            // Add 'Help' submenu.
            add_submenu_page(
                'edit.php?post_type=aga_form', // Parent is the CPT's main menu slug
                __( 'Help', 'autocomplete-google-address' ),
                __( 'Help', 'autocomplete-google-address' ),
                'manage_options',
                'aga-help',
                array( $this, 'render_help_page' )
            );
        }
    /**
     * Renders the settings page view.
     *
     * @since 1.0.0
     */
    public function render_settings_page() {
        require_once AGA_PLUGIN_DIR . 'admin/views/html-admin-page-settings.php';
    }
    
    /**
     * Renders the help page view.
     *
     * @since 1.0.0
     */
    public function render_help_page() {
        require_once AGA_PLUGIN_DIR . 'admin/views/html-admin-page-help.php';
    }

    /**
     * Renders the analytics dashboard page view.
     *
     * @since 5.1.0
     */
    public function render_analytics_page() {
        require_once AGA_PLUGIN_DIR . 'admin/views/html-admin-page-analytics.php';
    }

    /**
     * Renders the review request banner on plugin admin pages after 14 days.
     */
    public function render_review_banner() {
        // Permanently dismissed.
        if ( get_option( 'aga_review_dismissed' ) === 'permanent' ) {
            return;
        }

        // Dismissed temporarily — check expiration.
        $dismissed_until = get_option( 'aga_review_dismissed' );
        if ( $dismissed_until && is_numeric( $dismissed_until ) && time() < (int) $dismissed_until ) {
            return;
        }

        // Set activation time if not set.
        $activation_time = get_option( 'aga_activation_time' );
        if ( ! $activation_time ) {
            update_option( 'aga_activation_time', time() );
            return;
        }

        // Check if 14 days have passed.
        if ( ( time() - (int) $activation_time ) < ( 14 * DAY_IN_SECONDS ) ) {
            return;
        }

        // Only show on plugin pages.
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $is_plugin_page = ( 'aga_form' === $screen->post_type ) ||
                          ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'aga-settings', 'aga-help', 'aga-wizard', 'aga-analytics' ), true ) );

        if ( ! $is_plugin_page ) {
            return;
        }

        $review_url = 'https://wordpress.org/support/plugin/autocomplete-google-address/reviews/#new-post';
        ?>
        <div class="aga-review-banner" id="aga-review-banner" style="background:#f0f6fc;border:1px solid #c3d4e7;border-left:4px solid #2271b1;border-radius:4px;padding:16px 20px;margin:15px 0;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:12px;">
            <div style="flex:1;min-width:280px;">
                <p style="margin:0;font-size:14px;color:#1d2327;line-height:1.5;">
                    <?php esc_html_e( "You've been using Autocomplete Google Address for 2 weeks! If it's been helpful, would you mind leaving a quick review? It helps us a lot!", 'autocomplete-google-address' ); ?>
                </p>
            </div>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <a href="<?php echo esc_url( $review_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-primary" id="aga-review-sure" style="background:#2271b1;border-color:#2271b1;box-shadow:none;text-shadow:none;">
                    <?php esc_html_e( "Sure, I'll leave a review", 'autocomplete-google-address' ); ?>
                </a>
                <button type="button" class="button" id="aga-review-later">
                    <?php esc_html_e( 'Maybe later', 'autocomplete-google-address' ); ?>
                </button>
                <button type="button" class="button" id="aga-review-already">
                    <?php esc_html_e( 'I already did', 'autocomplete-google-address' ); ?>
                </button>
            </div>
        </div>
        <script>
        (function($) {
            function agaDismissReview(type) {
                $('#aga-review-banner').slideUp(200);
                $.post(aga_admin_data.ajax_url, {
                    action: 'aga_dismiss_review',
                    nonce:  aga_admin_data.nonce,
                    type:   type
                });
            }
            $('#aga-review-sure').on('click', function() { agaDismissReview('permanent'); });
            $('#aga-review-already').on('click', function() { agaDismissReview('permanent'); });
            $('#aga-review-later').on('click', function() { agaDismissReview('later'); });
        })(jQuery);
        </script>
        <?php
    }

    /**
     * AJAX handler for dismissing the review banner.
     */
    public function dismiss_review_banner() {
        check_ajax_referer( 'aga_admin_nonce', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized' );
        }

        $type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : 'later';

        if ( 'permanent' === $type ) {
            update_option( 'aga_review_dismissed', 'permanent' );
        } else {
            // Dismiss for 30 days.
            update_option( 'aga_review_dismissed', time() + ( 30 * DAY_IN_SECONDS ) );
        }

        wp_send_json_success();
    }

    /**
     * Check for conflicting Google Maps API scripts loaded by other plugins or themes.
     * Shows a warning notice in the admin area when a conflict is detected.
     */
    public function check_script_conflicts() {
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $is_plugin_page = ( 'aga_form' === $screen->post_type ) ||
                          ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'aga-settings', 'aga-help', 'aga-wizard', 'aga-analytics' ), true ) );

        if ( ! $is_plugin_page ) {
            return;
        }

        $wp_scripts    = wp_scripts();
        $conflicts     = array();

        foreach ( $wp_scripts->registered as $handle => $script ) {
            if ( 'aga-google-maps' === $handle ) {
                continue;
            }

            if ( ! empty( $script->src ) &&
                 ( strpos( $script->src, 'maps.googleapis.com/maps/api/js' ) !== false ||
                   strpos( $script->src, 'maps.google.com/maps/api/js' ) !== false ) ) {
                $conflicts[] = array(
                    'handle' => $handle,
                    'src'    => $script->src,
                );
            }
        }

        if ( empty( $conflicts ) ) {
            return;
        }

        ?>
        <div class="notice notice-warning is-dismissible aga-conflict-notice">
            <p><strong><?php esc_html_e( 'Autocomplete Google Address — Script Conflict Detected', 'autocomplete-google-address' ); ?></strong></p>
            <p><?php esc_html_e( 'Another plugin or theme is loading the Google Maps API which may conflict with Autocomplete Google Address.', 'autocomplete-google-address' ); ?></p>
            <?php foreach ( $conflicts as $conflict ) : ?>
                <p>
                    <code><?php echo esc_html( $conflict['handle'] ); ?></code> &mdash;
                    <code><?php echo esc_html( $conflict['src'] ); ?></code>
                </p>
            <?php endforeach; ?>
            <p><?php esc_html_e( "Go to Settings > Advanced and enable 'Do not load Google Maps JS API' if you see issues.", 'autocomplete-google-address' ); ?></p>
        </div>
        <?php
    }

    /**
     * Renders the Import/Export UI on the aga_form list screen.
     */
    public function render_import_export_ui() {
        $screen = get_current_screen();
        if ( ! $screen || 'edit-aga_form' !== $screen->id ) {
            return;
        }

        $export_url   = wp_nonce_url( admin_url( 'admin-ajax.php?action=aga_export_configs' ), 'aga_import_export_nonce', 'nonce' );
        $import_nonce = wp_create_nonce( 'aga_import_export_nonce' );
        ?>
        <style>
            .aga-import-export-bar {
                display: flex;
                align-items: center;
                gap: 10px;
                margin: 12px 0 0;
                padding: 12px 16px;
                background: #fff;
                border: 1px solid #c3c4c7;
                border-radius: 4px;
            }
            .aga-import-export-bar .aga-ie-label {
                font-weight: 600;
                font-size: 13px;
                color: #1d2327;
                margin-right: 4px;
            }
            .aga-import-modal-overlay {
                display: none;
                position: fixed;
                top: 0; left: 0; right: 0; bottom: 0;
                background: rgba(0,0,0,0.5);
                z-index: 100100;
                align-items: center;
                justify-content: center;
            }
            .aga-import-modal-overlay.aga-active {
                display: flex;
            }
            .aga-import-modal {
                background: #fff;
                border-radius: 8px;
                padding: 24px 28px;
                max-width: 460px;
                width: 100%;
                box-shadow: 0 4px 20px rgba(0,0,0,0.15);
            }
            .aga-import-modal h2 {
                margin: 0 0 16px;
                font-size: 18px;
                color: #1d2327;
            }
            .aga-import-modal p.description {
                margin: 0 0 16px;
                color: #646970;
                font-size: 13px;
            }
            .aga-import-modal .aga-import-file-row {
                margin-bottom: 16px;
            }
            .aga-import-modal .aga-import-file-row input[type="file"] {
                width: 100%;
            }
            .aga-import-modal .aga-import-actions {
                display: flex;
                gap: 8px;
                justify-content: flex-end;
            }
            .aga-import-modal .aga-import-status {
                margin-top: 12px;
                padding: 10px 12px;
                border-radius: 4px;
                font-size: 13px;
                display: none;
            }
            .aga-import-modal .aga-import-status.aga-success {
                display: block;
                background: #edfaef;
                border: 1px solid #46b450;
                color: #1e4620;
            }
            .aga-import-modal .aga-import-status.aga-error {
                display: block;
                background: #fcf0f1;
                border: 1px solid #d63638;
                color: #8a1f1f;
            }
        </style>

        <div class="aga-import-export-bar" id="aga-import-export-bar">
            <span class="aga-ie-label"><?php esc_html_e( 'Import / Export:', 'autocomplete-google-address' ); ?></span>
            <a href="<?php echo esc_url( $export_url ); ?>" class="button button-secondary" id="aga-export-btn">
                <span class="dashicons dashicons-download" style="vertical-align: middle; margin-right: 2px; line-height: 1.4;"></span>
                <?php esc_html_e( 'Export All', 'autocomplete-google-address' ); ?>
            </a>
            <button type="button" class="button button-secondary" id="aga-import-btn">
                <span class="dashicons dashicons-upload" style="vertical-align: middle; margin-right: 2px; line-height: 1.4;"></span>
                <?php esc_html_e( 'Import', 'autocomplete-google-address' ); ?>
            </button>
        </div>

        <div class="aga-import-modal-overlay" id="aga-import-modal-overlay">
            <div class="aga-import-modal">
                <h2><?php esc_html_e( 'Import Configurations', 'autocomplete-google-address' ); ?></h2>
                <p class="description"><?php esc_html_e( 'Upload a JSON file previously exported from this plugin. New configurations will be created for each entry in the file.', 'autocomplete-google-address' ); ?></p>
                <div class="aga-import-file-row">
                    <input type="file" id="aga-import-file" accept=".json" />
                </div>
                <div class="aga-import-actions">
                    <button type="button" class="button" id="aga-import-cancel"><?php esc_html_e( 'Cancel', 'autocomplete-google-address' ); ?></button>
                    <button type="button" class="button button-primary" id="aga-import-submit"><?php esc_html_e( 'Import', 'autocomplete-google-address' ); ?></button>
                </div>
                <div class="aga-import-status" id="aga-import-status"></div>
            </div>
        </div>

        <script>
        (function($){
            // Insert the bar after the page title action area.
            var $bar = $('#aga-import-export-bar').detach();
            $('.wp-header-end').length ? $bar.insertAfter('.wp-header-end') : $bar.insertAfter('.wrap h1');

            // Open modal.
            $('#aga-import-btn').on('click', function(){
                $('#aga-import-modal-overlay').addClass('aga-active');
                $('#aga-import-status').removeClass('aga-success aga-error').hide().text('');
                $('#aga-import-file').val('');
            });

            // Close modal.
            $('#aga-import-cancel, #aga-import-modal-overlay').on('click', function(e){
                if ( e.target === this ) {
                    $('#aga-import-modal-overlay').removeClass('aga-active');
                }
            });

            // Submit import.
            $('#aga-import-submit').on('click', function(){
                var fileInput = document.getElementById('aga-import-file');
                if ( ! fileInput.files.length ) {
                    $('#aga-import-status').removeClass('aga-success').addClass('aga-error').text('<?php echo esc_js( __( 'Please select a JSON file.', 'autocomplete-google-address' ) ); ?>').show();
                    return;
                }

                var formData = new FormData();
                formData.append('action', 'aga_import_configs');
                formData.append('nonce', '<?php echo esc_js( $import_nonce ); ?>');
                formData.append('aga_import_file', fileInput.files[0]);

                var $btn = $(this);
                $btn.prop('disabled', true).text('<?php echo esc_js( __( 'Importing...', 'autocomplete-google-address' ) ); ?>');

                $.ajax({
                    url: '<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        if ( response.success ) {
                            $('#aga-import-status').removeClass('aga-error').addClass('aga-success').text(response.data.message).show();
                            setTimeout(function(){ location.reload(); }, 1500);
                        } else {
                            $('#aga-import-status').removeClass('aga-success').addClass('aga-error').text(response.data.message || '<?php echo esc_js( __( 'Import failed.', 'autocomplete-google-address' ) ); ?>').show();
                        }
                    },
                    error: function() {
                        $('#aga-import-status').removeClass('aga-success').addClass('aga-error').text('<?php echo esc_js( __( 'An unexpected error occurred.', 'autocomplete-google-address' ) ); ?>').show();
                    },
                    complete: function() {
                        $btn.prop('disabled', false).text('<?php echo esc_js( __( 'Import', 'autocomplete-google-address' ) ); ?>');
                    }
                });
            });
        })(jQuery);
        </script>
        <?php
    }

    /**
     * Renders the WhatsApp chat widget on plugin admin pages.
     */
    public function render_whatsapp_chat() {
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $is_plugin_page = ( 'aga_form' === $screen->post_type ) ||
                          ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'aga-settings', 'aga-help', 'aga-wizard', 'aga-analytics' ), true ) );

        if ( ! $is_plugin_page ) {
            return;
        }

        $whatsapp_number = '8801767591988';
        $default_message = rawurlencode( 'Hi, I need help with the Autocomplete Google Address plugin.' );
        $whatsapp_url    = 'https://wa.me/' . $whatsapp_number . '?text=' . $default_message;
        ?>
        <div id="aga-whatsapp-widget">
            <div id="aga-whatsapp-popup" style="display:none;">
                <div class="aga-wa-popup-header">
                    <div class="aga-wa-popup-header-info">
                        <div class="aga-wa-avatar">N</div>
                        <div>
                            <div class="aga-wa-name">Nishath - Plugin Support</div>
                            <div class="aga-wa-status">Typically replies within minutes</div>
                        </div>
                    </div>
                    <button type="button" id="aga-wa-close" aria-label="Close">&times;</button>
                </div>
                <div class="aga-wa-popup-body">
                    <div class="aga-wa-message">
                        <p>Hi there! Need help setting up the plugin or have any questions? I'm available 24/7 to assist you.</p>
                        <span class="aga-wa-time"><?php echo esc_html( current_time( 'g:i A' ) ); ?></span>
                    </div>
                </div>
                <div class="aga-wa-popup-footer">
                    <a href="<?php echo esc_url( $whatsapp_url ); ?>" target="_blank" rel="noopener noreferrer" class="aga-wa-send-btn">
                        <svg width="20" height="20" viewBox="0 0 24 24" fill="#fff"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/><path d="M12 0C5.373 0 0 5.373 0 12c0 2.625.846 5.059 2.284 7.034L.789 23.492a.5.5 0 00.611.611l4.458-1.495A11.94 11.94 0 0012 24c6.627 0 12-5.373 12-12S18.627 0 12 0zm0 22c-2.344 0-4.507-.796-6.227-2.132l-.435-.344-2.636.884.884-2.636-.344-.435A9.96 9.96 0 012 12C2 6.486 6.486 2 12 2s10 4.486 10 10-4.486 10-10 10z"/></svg>
                        Start Chat
                    </a>
                </div>
            </div>
            <button type="button" id="aga-whatsapp-btn" aria-label="Chat on WhatsApp">
                <svg width="28" height="28" viewBox="0 0 24 24" fill="#fff"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/><path d="M12 0C5.373 0 0 5.373 0 12c0 2.625.846 5.059 2.284 7.034L.789 23.492a.5.5 0 00.611.611l4.458-1.495A11.94 11.94 0 0012 24c6.627 0 12-5.373 12-12S18.627 0 12 0zm0 22c-2.344 0-4.507-.796-6.227-2.132l-.435-.344-2.636.884.884-2.636-.344-.435A9.96 9.96 0 012 12C2 6.486 6.486 2 12 2s10 4.486 10 10-4.486 10-10 10z"/></svg>
            </button>
        </div>
        <script>
        (function(){
            var btn = document.getElementById('aga-whatsapp-btn');
            var popup = document.getElementById('aga-whatsapp-popup');
            var closeBtn = document.getElementById('aga-wa-close');
            btn.addEventListener('click', function(){
                var isOpen = popup.style.display === 'flex';
                popup.style.display = isOpen ? 'none' : 'flex';
            });
            closeBtn.addEventListener('click', function(){
                popup.style.display = 'none';
            });
        })();
        </script>
        <?php
    }

    /**
     * Renders the Free-to-Pro upgrade banner on plugin admin pages after 7 days.
     * Only shown to free users.
     */
    public function render_upgrade_banner() {
        // Only show for FREE users.
        if ( function_exists( 'google_autocomplete' ) && google_autocomplete()->is_paying() ) {
            return;
        }

        // Permanently dismissed.
        if ( get_option( 'aga_upgrade_dismissed' ) === 'permanent' ) {
            return;
        }

        // Dismissed temporarily — check expiration (14 days).
        $dismissed_until = get_option( 'aga_upgrade_dismissed' );
        if ( $dismissed_until && is_numeric( $dismissed_until ) && time() < (int) $dismissed_until ) {
            return;
        }

        // Check activation time — only show after 7 days.
        $activation_time = get_option( 'aga_activation_time' );
        if ( ! $activation_time ) {
            update_option( 'aga_activation_time', time() );
            return;
        }

        if ( ( time() - (int) $activation_time ) < ( 7 * DAY_IN_SECONDS ) ) {
            return;
        }

        // Only show on plugin pages.
        $screen = get_current_screen();
        if ( ! $screen ) {
            return;
        }

        $is_plugin_page = ( 'aga_form' === $screen->post_type ) ||
                          ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'aga-settings', 'aga-help', 'aga-wizard', 'aga-analytics' ), true ) );

        if ( ! $is_plugin_page ) {
            return;
        }

        $checkout_url = function_exists( 'google_autocomplete' ) ? google_autocomplete()->checkout_url() : '#';

        $features = array(
            'Smart Mapping',
            'WooCommerce Integration',
            'Map Preview',
            'Address Validation',
            'Geolocation',
            'Form Presets',
            'Country Restriction',
            'Priority Support',
        );
        ?>
        <div class="aga-upgrade-banner" id="aga-upgrade-banner" style="position:relative;background:linear-gradient(135deg,#4361ee,#7c3aed);border-radius:8px;padding:28px 32px;margin:15px 0;color:#fff;box-shadow:0 4px 16px rgba(67,97,238,0.3);">
            <button type="button" id="aga-upgrade-dismiss" aria-label="Dismiss" style="position:absolute;top:12px;right:14px;background:rgba(255,255,255,0.2);border:none;color:#fff;font-size:18px;cursor:pointer;width:28px;height:28px;border-radius:50%;display:flex;align-items:center;justify-content:center;line-height:1;transition:background 0.2s;">&times;</button>

            <h3 class="aga-upgrade-banner-title" style="margin:0 0 6px 0;font-size:20px;font-weight:700;color:#fff;">
                <?php esc_html_e( 'Unlock the Full Power of Autocomplete Google Address', 'autocomplete-google-address' ); ?>
            </h3>
            <p class="aga-upgrade-banner-subtitle" style="margin:0 0 20px 0;font-size:14px;color:rgba(255,255,255,0.85);">
                <?php esc_html_e( 'Upgrade to Pro and supercharge your address forms with these premium features:', 'autocomplete-google-address' ); ?>
            </p>

            <div class="aga-upgrade-banner-features" style="display:grid;grid-template-columns:1fr 1fr;gap:10px 24px;margin-bottom:22px;max-width:520px;">
                <?php foreach ( $features as $feature ) : ?>
                    <div class="aga-upgrade-banner-feature" style="display:flex;align-items:center;gap:8px;font-size:14px;color:#fff;">
                        <span style="display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;background:rgba(255,255,255,0.2);border-radius:50;flex-shrink:0;">
                            <svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M2 6l3 3 5-5" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
                        </span>
                        <?php echo esc_html( $feature ); ?>
                    </div>
                <?php endforeach; ?>
            </div>

            <a href="<?php echo esc_url( $checkout_url ); ?>" class="aga-upgrade-banner-cta" style="display:inline-block;background:#fff;color:#4361ee;font-weight:600;font-size:14px;padding:10px 28px;border-radius:6px;text-decoration:none;transition:opacity 0.2s;box-shadow:0 2px 8px rgba(0,0,0,0.15);">
                <?php esc_html_e( 'Upgrade to Pro', 'autocomplete-google-address' ); ?>
            </a>
        </div>

        <script>
        (function($) {
            var $banner = $('#aga-upgrade-banner');

            $('#aga-upgrade-dismiss').on('click', function(e) {
                e.preventDefault();
                $banner.slideUp(200);
                $.post(aga_admin_data.ajax_url, {
                    action: 'aga_dismiss_upgrade',
                    nonce:  aga_admin_data.nonce,
                    type:   'temporary'
                });
            });

            // Right-click or shift-click dismiss = permanent.
            $('#aga-upgrade-dismiss').on('contextmenu', function(e) {
                e.preventDefault();
                $banner.slideUp(200);
                $.post(aga_admin_data.ajax_url, {
                    action: 'aga_dismiss_upgrade',
                    nonce:  aga_admin_data.nonce,
                    type:   'permanent'
                });
            });
        })(jQuery);
        </script>
        <?php
    }

    /**
     * AJAX handler for dismissing the upgrade banner.
     */
    public function dismiss_upgrade_banner() {
        check_ajax_referer( 'aga_admin_nonce', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized' );
        }

        $type = isset( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : 'temporary';

        if ( 'permanent' === $type ) {
            update_option( 'aga_upgrade_dismissed', 'permanent' );
        } else {
            // Dismiss for 14 days.
            update_option( 'aga_upgrade_dismissed', time() + ( 14 * DAY_IN_SECONDS ) );
        }

        wp_send_json_success();
    }

    /**
     * AJAX: Search pages/posts for the Visual Selector Tool.
     * Returns paginated results with 20 items per page.
     */
    public function vst_search_pages() {
        check_ajax_referer( 'aga_vst_nonce', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_send_json_error( 'Unauthorized', 403 );
        }

        $search    = isset( $_GET['search'] ) ? sanitize_text_field( wp_unslash( $_GET['search'] ) ) : '';
        $page      = isset( $_GET['page'] ) ? absint( $_GET['page'] ) : 1;
        $filter    = isset( $_GET['filter'] ) ? sanitize_text_field( $_GET['filter'] ) : 'all';
        $per_page  = 20;

        $args = array(
            'post_type'      => array( 'page', 'post' ),
            'post_status'    => 'publish',
            'posts_per_page' => 'forms' === $filter ? -1 : $per_page,
            'paged'          => 'forms' === $filter ? 1 : $page,
            'orderby'        => 'title',
            'order'          => 'ASC',
        );

        if ( ! empty( $search ) ) {
            $args['s'] = $search;
        }

        $query   = new WP_Query( $args );
        $results = array();

        // Form shortcode patterns to detect pages with forms.
        $form_patterns = array(
            'contact-form-7',    // CF7
            'contact-form',      // CF7 alternate
            'wpforms',           // WPForms
            'gravityform',       // Gravity Forms
            'gravityforms',
            'formidable',        // Formidable Forms
            'ninja_form',        // Ninja Forms
            'fluentform',        // Fluent Forms
            'elementor-form',    // Elementor
            'forminator_form',   // Forminator
            'ws_form',           // WS Form
            'happyforms',        // HappyForms
            'caldera_form',      // Caldera
            'woocommerce_checkout', // WooCommerce
            'aga_autocomplete',  // Our own shortcode
            'aga_form',          // Our own shortcode
        );

        // WooCommerce special page IDs.
        $wc_page_ids = array();
        if ( function_exists( 'wc_get_page_id' ) ) {
            foreach ( array( 'checkout', 'myaccount', 'cart' ) as $wc_page ) {
                $wc_id = wc_get_page_id( $wc_page );
                if ( $wc_id > 0 ) {
                    $wc_page_ids[] = $wc_id;
                }
            }
        }

        foreach ( $query->posts as $post ) {
            $has_form = false;

            if ( 'forms' === $filter ) {
                $content = $post->post_content;

                // Check for form shortcodes.
                foreach ( $form_patterns as $pattern ) {
                    if ( stripos( $content, '[' . $pattern ) !== false ) {
                        $has_form = true;
                        break;
                    }
                }

                // Check for HTML form/input tags.
                if ( ! $has_form && ( stripos( $content, '<form' ) !== false || stripos( $content, '<input' ) !== false ) ) {
                    $has_form = true;
                }

                // Check for Elementor form widget in metadata.
                if ( ! $has_form && stripos( $content, '"widgetType":"form"' ) !== false ) {
                    $has_form = true;
                }

                // WooCommerce special pages always have forms.
                if ( ! $has_form && in_array( $post->ID, $wc_page_ids, true ) ) {
                    $has_form = true;
                }

                if ( ! $has_form ) {
                    continue;
                }
            }

            $results[] = array(
                'id'    => $post->ID,
                'title' => $post->post_title ?: '(no title)',
                'url'   => get_permalink( $post->ID ),
                'type'  => $post->post_type,
            );
        }

        // Handle pagination for forms filter (we fetched all, now paginate in PHP).
        $total = count( $results );
        if ( 'forms' === $filter ) {
            $results = array_slice( $results, ( $page - 1 ) * $per_page, $per_page );
            $pages   = max( 1, ceil( $total / $per_page ) );
        } else {
            $total = $query->found_posts;
            $pages = $query->max_num_pages;
        }

        wp_send_json_success( array(
            'items'    => array_values( $results ),
            'page'     => $page,
            'pages'    => $pages,
            'total'    => $total,
            'has_more' => $page < $pages,
        ) );
    }
}
