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

class Bee_Corp_Welcome_Notice {
    private $bee_corp_pro_theme_url = 'https://www.themebee.net/products/ecommerce-wordpress-theme/';
    private $bee_corp_doc_url = 'https://preview.themebee.net/guide/bee-corp-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_corp_admin_notice_welcome', 0);
    }

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

    public function render_welcome_notice() {
        $bee_corp_dismiss_url = wp_nonce_url(
            remove_query_arg(['activated'], add_query_arg('bee-corp-hide-notice', 'welcome')),
            'bee_corp_hide_notices_nonce',
            '_bee_corp_notice_nonce'
        );

        $bee_corp_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-corp-notice">
            <a class="bee-corp-message-close notice-dismiss" href="<?php echo esc_url($bee_corp_dismiss_url); ?>"></a>
            <div class="bee-corp-notice-wrapper">
                <div class="bee-corp-notice-welcome">
                    <div class="bee-corp-notice-left">
                        <div class="bee-corp-notice-subheading">
                            <?php printf(esc_html__('Welcome!', 'bee-corp'), $bee_corp_current_user->user_login); ?>
                        </div>
                        <h2 class="bee-corp-notice-heading">
                            <?php esc_html_e('Thank you for choosing Bee Corp.', 'bee-corp'); ?>
                        </h2>
                    </div>
                </div>
                <div class="bee-corp-notice-content">
                    <div class="bee-corp-notice-item bee-corp-notice-theme-dashboard">
                        <h3><?php esc_html_e('Getting Started ', 'bee-corp'); ?></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-corp'); ?></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-corp'); ?>
                            </a>
                            <a href="<?php echo esc_url($this->bee_corp_doc_url); ?>" class="button button-secondary dashboard-button dashboard-button-secondary" target="_blank">
                                <?php esc_html_e('User Guide', 'bee-corp'); ?>
                            </a>
                            <a href="<?php echo esc_url($this->bee_corp_pro_theme_url); ?>" class="button button-secondary dashboard-button dashboard-button-primary" target="_blank">
                                <?php esc_html_e('Go Pro', 'bee-corp'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }
    public function handle_hide_notice() {
        if (isset($_GET['bee-corp-hide-notice']) && isset($_GET['_bee_corp_notice_nonce'])) {
            if (!wp_verify_nonce(wp_unslash($_GET['_bee_corp_notice_nonce']), 'bee_corp_hide_notices_nonce')) {
                wp_die(__('Action failed. Please refresh the page and retry.', 'bee-corp'));
            }
            if (!current_user_can('manage_options')) {
                wp_die(__('Cheatin&#8217; huh?', 'bee-corp'));
            }
            $bee_corp_hide_notice = sanitize_text_field(wp_unslash($_GET['bee-corp-hide-notice']));
            update_option('bee_corp_admin_notice_' . $bee_corp_hide_notice, 1);
        }
    }
}
new Bee_Corp_Welcome_Notice();