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

class Party_Corp_Welcome_Notice {
    private $party_corp_pro_theme_url = 'https://www.themebee.net/products/ecommerce-wordpress-theme/';
    private $party_corp_doc_url = 'https://preview.themebee.net/guide/party-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('party_corp_admin_notice_welcome', 0);
    }

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

    public function render_welcome_notice() {
        $party_corp_dismiss_url = wp_nonce_url(
            remove_query_arg(['activated'], add_query_arg('party-corp-hide-notice', 'welcome')),
            'party_corp_hide_notices_nonce',
            '_party_corp_notice_nonce'
        );

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