<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Reset notice on theme activation
 */
function thryve_school_reset_pro_notice_on_theme_activation() {
    delete_user_meta( get_current_user_id(), 'thryve_school_pro_notice_dismissed' );
}
add_action( 'after_switch_theme', 'thryve_school_reset_pro_notice_on_theme_activation' );


/**
 * Show Admin Notice
 */
function thryve_school_pro_admin_notice() {

    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    // Prevent notice from breaking Theme Info page UI
    if ( function_exists( 'get_current_screen' ) ) {
        $screen = get_current_screen();
        if ( isset( $screen->id ) && $screen->id === 'appearance_page_thryvewp-business-info' ) {
            return;
        }
    }

    // Show only for thryve school theme
    $theme = wp_get_theme();
    if ( 'thryve-school' !== $theme->get_stylesheet() ) {
        return;
    }

    $dismissed = get_user_meta( get_current_user_id(), 'thryve_school_pro_notice_dismissed', true );
    if ( $dismissed ) {
        return;
    }

    $pro_url = 'https://thryvewp.com/thryve-school-pro';
    $doc_url = 'https://thryvewp.com/documentation/';
    ?>
    <div class="notice notice-info is-dismissible thryve-school-pro-notice">
                  <h1>  <strong><?php echo esc_html__( 'Upgrade to Thryve School Pro!', 'thryve-school' ); ?></strong></h1>

        <p>
            <?php echo esc_html__( '🚀 Unlock advanced features, premium blocks, enhanced customization options, priority support, and performance optimizations designed for professional business websites. Thryve School Pro helps you build faster, more flexible, and scalable websites with a premium design experience.', 'thryve-school' ); ?>
        </p>
        <p>
            <a href="<?php echo esc_url( $pro_url ); ?>" target="_blank" class="button button-primary">
                <?php echo esc_html__( 'Upgrade to Pro', 'thryve-school' ); ?>
            </a>
             <a href="<?php echo esc_url( $doc_url ); ?>" target="_blank" class="button button-secondary">
                <?php echo esc_html__( 'Documentation', 'thryve-school' ); ?>
            </a>
        </p>
    </div>

    <script>
        jQuery(document).on('click', '.thryve-school-pro-notice .notice-dismiss', function () {
            jQuery.post(ajaxurl, {
                action: 'thryve_school_dismiss_pro_notice'
            });
        });
    </script>
    <?php
}
add_action( 'admin_notices', 'thryve_school_pro_admin_notice' );


/**
 * Handle dismiss
 */
function thryve_school_dismiss_pro_notice() {
    update_user_meta( get_current_user_id(), 'thryve_school_pro_notice_dismissed', 1 );
    wp_die();
}
add_action( 'wp_ajax_thryve_school_dismiss_pro_notice', 'thryve_school_dismiss_pro_notice' );
