<?php
// Exit if accessed directly.
defined('ABSPATH') || exit;

class SaleCraft_Ecommerce_Welcome_Notice {
    private $bee_real_estate_pro_theme_url = 'https://www.themebee.net/products/real-estate-wordpress-theme/';
    private $bee_real_estate_doc_url = 'https://preview.themebee.net/guide/bee-real-estate-free/';
    private $theme_name;
    private $theme_version;
    private $theme_slug;

    public function __construct() {
        add_action('wp_loaded', [$this, 'show_welcome_notice'], 20);
        add_action('wp_loaded', [$this, 'handle_hide_notice'], 15);

        // ✅ Reset notice when theme is activated
        add_action('after_switch_theme', [$this, 'set_welcome_notice']);
    }

    /**
     * Enable notice on theme activation
     */
    public function set_welcome_notice() {
        update_option('bee_real_estate_admin_notice_welcome', 0);
    }

    public function show_welcome_notice() {
        // ✅ Show only if not dismissed
        if (get_option('bee_real_estate_admin_notice_welcome') != 1) {
            add_action('admin_notices', [$this, 'render_welcome_notice']);
        }
    }

    public function render_welcome_notice() {
        $bee_real_estate_dismiss_url = wp_nonce_url(
            remove_query_arg(['activated'], add_query_arg('bee-real-estate-hide-notice', 'welcome')),
            'bee_real_estate_hide_notices_nonce',
            '_bee_real_estate_notice_nonce'
        );

        $bee_real_estate_current_user = wp_get_current_user();
        $theme = wp_get_theme();
        $this->theme_name = $theme->get('Name');
        $this->theme_version = $theme->get('Version');
        $this->theme_slug = $theme->get_template();
        ?>

        <div id="message" class="notice notice-success bee-real-estate-notice">
            <a class="bee-real-estate-message-close notice-dismiss" href="<?php echo esc_url($bee_real_estate_dismiss_url); ?>"></a>
            <div class="bee-real-estate-notice-wrapper">
                <div class="bee-real-estate-notice-welcome">
                    <div class="bee-real-estate-notice-left">
                        <div class="bee-real-estate-notice-subheading">
                            <?php printf(esc_html__('Welcome!', 'bee-real-estate'), $bee_real_estate_current_user->user_login); ?>
                        </div>
                        <h2 class="bee-real-estate-notice-heading">
                            <?php esc_html_e('Thank you for choosing Bee Real Estate.', 'bee-real-estate'); ?>
                        </h2>
                    </div>
                </div>
                <div class="bee-real-estate-notice-content">
                    <div class="bee-real-estate-notice-item bee-real-estate-notice-theme-dashboard">
                        <h3><?php esc_html_e('Getting Started ', 'bee-real-estate'); ?></h3>
                        <p><?php esc_html_e("Explore the links below to get detailed guidance and make the most out of all the features our theme provides.", 'bee-real-estate'); ?></p>
                        <div class="dashboard-button-group">
                            <a href="<?php echo esc_url(admin_url('themes.php?page=' . $this->theme_slug)); ?>" class="button button-secondary dashboard-button dashboard-button-secondary">
                                <?php esc_html_e('Theme Panel', 'bee-real-estate'); ?>
                            </a>
                            <a href="<?php echo esc_url($this->bee_real_estate_doc_url); ?>" class="button button-secondary dashboard-button dashboard-button-secondary" target="_blank">
                                <?php esc_html_e('User Guide', 'bee-real-estate'); ?>
                            </a>
                            <a href="<?php echo esc_url($this->bee_real_estate_pro_theme_url); ?>" class="button button-secondary dashboard-button dashboard-button-primary" target="_blank">
                                <?php esc_html_e('Go Pro', 'bee-real-estate'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }
    public function handle_hide_notice() {
        if (isset($_GET['bee-real-estate-hide-notice']) && isset($_GET['_bee_real_estate_notice_nonce'])) {
            if (!wp_verify_nonce(wp_unslash($_GET['_bee_real_estate_notice_nonce']), 'bee_real_estate_hide_notices_nonce')) {
                wp_die(__('Action failed. Please refresh the page and retry.', 'bee-real-estate'));
            }
            if (!current_user_can('manage_options')) {
                wp_die(__('Cheatin&#8217; huh?', 'bee-real-estate'));
            }
            $bee_real_estate_hide_notice = sanitize_text_field(wp_unslash($_GET['bee-real-estate-hide-notice']));
            update_option('bee_real_estate_admin_notice_' . $bee_real_estate_hide_notice, 1);
        }
    }
}
new SaleCraft_Ecommerce_Welcome_Notice();