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

class KnowledgeHub_Welcome_Notice {
    private $knowledgehub_pro_theme_url = 'https://www.themebee.net/products/education-wordpress-theme/';
    private $knowledgehub_doc_url = 'https://preview.themebee.net/guide/knowledgehub-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('knowledgehub_admin_notice_welcome', 0);
    }

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

    public function render_welcome_notice() {
        $knowledgehub_dismiss_url = wp_nonce_url(
            remove_query_arg(['activated'], add_query_arg('knowledgehub-hide-notice', 'welcome')),
            'knowledgehub_hide_notices_nonce',
            '_knowledgehub_notice_nonce'
        );

        $knowledgehub_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 knowledgehub-notice">
            <a class="knowledgehub-message-close notice-dismiss" href="<?php echo esc_url($knowledgehub_dismiss_url); ?>"></a>
            <div class="knowledgehub-notice-wrapper">
                <div class="knowledgehub-notice-welcome">
                    <div class="knowledgehub-notice-left">
                        <div class="knowledgehub-notice-subheading">
                            <?php printf(esc_html__('Welcome!', 'knowledgehub'), $knowledgehub_current_user->user_login); ?>
                        </div>
                        <h2 class="knowledgehub-notice-heading">
                            <?php esc_html_e('Thank you for choosing KnowledgeHub.', 'knowledgehub'); ?>
                        </h2>
                    </div>
                </div>
                <div class="knowledgehub-notice-content">
                    <div class="knowledgehub-notice-item knowledgehub-notice-theme-dashboard">
                        <h3><?php esc_html_e('Getting Started ', 'knowledgehub'); ?></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.", 'knowledgehub'); ?></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', 'knowledgehub'); ?>
                            </a>
                            <a href="<?php echo esc_url($this->knowledgehub_doc_url); ?>" class="button button-secondary dashboard-button dashboard-button-secondary" target="_blank">
                                <?php esc_html_e('User Guide', 'knowledgehub'); ?>
                            </a>
                            <a href="<?php echo esc_url($this->knowledgehub_pro_theme_url); ?>" class="button button-secondary dashboard-button dashboard-button-primary" target="_blank">
                                <?php esc_html_e('Go Pro', 'knowledgehub'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }
    public function handle_hide_notice() {
        if (isset($_GET['knowledgehub-hide-notice']) && isset($_GET['_knowledgehub_notice_nonce'])) {
            if (!wp_verify_nonce(wp_unslash($_GET['_knowledgehub_notice_nonce']), 'knowledgehub_hide_notices_nonce')) {
                wp_die(__('Action failed. Please refresh the page and retry.', 'knowledgehub'));
            }
            if (!current_user_can('manage_options')) {
                wp_die(__('Cheatin&#8217; huh?', 'knowledgehub'));
            }
            $knowledgehub_hide_notice = sanitize_text_field(wp_unslash($_GET['knowledgehub-hide-notice']));
            update_option('knowledgehub_admin_notice_' . $knowledgehub_hide_notice, 1);
        }
    }
}
new KnowledgeHub_Welcome_Notice();