<?php
// Show the notice only once after theme activation
add_action( 'admin_init', function () {
    global $pagenow;

    if ( is_admin() && $pagenow === 'themes.php' ) {
        add_action( 'admin_notices', 'vw_bakery_blocks_activation_notice' );
    }
});

// Admin Notice Function
function vw_bakery_blocks_activation_notice() {

    $vw_bakery_blocks_meta = get_option( 'vw_bakery_blocks_admin_notice' );

    if ( $vw_bakery_blocks_meta ) {
        return;
    }
    ?>
    <div id="vw-bakery-blocks-welcome-notice" class="notice notice-success is-dismissible welcome-notice">
            <div class="notice-row">
                <div class="notice-text">
                    <p class="welcome-text1"><?php esc_html_e( '🎉 Welcome to VW Themes,', 'vw-bakery-blocks' ); ?></p>
                    <p class="welcome-text2"><?php esc_html_e( 'You are now using the VW Bakery Blocks, a beautifully designed theme to kickstart your website.', 'vw-bakery-blocks' ); ?></p>
                    <p class="welcome-text3"><?php esc_html_e( 'To help you get started quickly, use the options below:', 'vw-bakery-blocks' ); ?></p>

                    <span class="import-btn">
                        <a href="javascript:void(0);" id="install-activate-button" class="button admin-button info-button">
                           <?php echo __('GET STARTED', 'vw-bakery-blocks'); ?>
                        </a>
                        <script type="text/javascript">
                            document.getElementById('install-activate-button').addEventListener('click', function () {
                                const vw_bakery_blocks_button = this;
                                const vw_bakery_blocks_redirectUrl = '<?php echo esc_url(admin_url("themes.php?page=vw-bakery-blocks-info")); ?>';
                                // First, check if plugin is already active
                                jQuery.post(ajaxurl, { action: 'check_plugin_activation' }, function (response) {
                                    if (response.success && response.data.active) {
                                        // Plugin already active — just redirect
                                        window.location.href = vw_bakery_blocks_redirectUrl;
                                    } else {
                                        // Show Installing & Activating only if not already active
                                        vw_bakery_blocks_button.textContent = 'Installing & Activating...';

                                        jQuery.post(ajaxurl, {
                                            action: 'install_and_activate_required_plugin',
                                            nonce: '<?php echo wp_create_nonce("install_activate_nonce"); ?>'
                                        }, function (response) {
                                            if (response.success) {
                                                window.location.href = vw_bakery_blocks_redirectUrl;
                                            } else {
                                                alert('Failed to activate the plugin.');
                                                vw_bakery_blocks_button.textContent = 'Try Again';
                                            }
                                        });
                                    }
                                });
                            });
                        </script>
                    </span>

                    <span class="demo-btn">
                        <a href="https://www.vwthemes.net/vw-bakery-blocks/" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'VIEW DEMO', 'vw-bakery-blocks' ); ?>
                        </a>
                    </span>

                    <span class="upgrade-btn">
                        <a href="https://www.vwthemes.com/products/gutenberg-cake-wordpress-theme" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'UPGRADE TO PRO', 'vw-bakery-blocks' ); ?>
                        </a>
                    </span>

                    <span class="bundle-btn">
                        <a href="https://www.vwthemes.com/products/wp-theme-bundle" class="button button-primary" target="_blank">
                            <?php esc_html_e( 'BUNDLE OF 400+ THEMES', 'vw-bakery-blocks' ); ?>
                        </a>
                    </span>
                </div>

                <div class="notice-img1">
                    <img src="<?php echo esc_url( get_template_directory_uri() . '/images/arrow-notice.png' ); ?>" width="180" alt="<?php esc_attr_e( 'VW Bakery Blocks', 'vw-bakery-blocks' ); ?>" />
                </div>

                <div class="notice-img2">
                    <img src="<?php echo esc_url( get_template_directory_uri() . '/images/bundle-notice.png' ); ?>" width="180" alt="<?php esc_attr_e( 'VW Bakery Blocks', 'vw-bakery-blocks' ); ?>" />
                </div>
            </div>
        </div>
        <?php
}
