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

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


/**
 * Show Admin Notice
 */
function thryvewp_business_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 Thryvewp FSE theme
    $theme = wp_get_theme();
    if ( 'thryvewp-fse' !== $theme->get_stylesheet() ) {
        return;
    }

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

    $pro_url = 'https://thryvewp.com/thryvewp-fse-pro';
    $doc_url = 'https://thryvewp.com/documentation/';
    ?>
    <div class="notice notice-info is-dismissible thryvewp-fse-pro-notice">
                  <h1>  <strong><?php echo esc_html__( 'Upgrade to Thryvewp FSE Pro!', 'thryvewp-fse' ); ?></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. Thryvewp FSE Pro helps you build faster, more flexible, and scalable websites with a premium design experience.', 'thryvewp-fse' ); ?>
        </p>
        <p>
            <a href="<?php echo esc_url( $pro_url ); ?>" target="_blank" class="button button-primary">
                <?php echo esc_html__( 'Upgrade to Pro', 'thryvewp-fse' ); ?>
            </a>
             <a href="<?php echo esc_url( $doc_url ); ?>" target="_blank" class="button button-secondary">
                <?php echo esc_html__( 'Documentation', 'thryvewp-fse' ); ?>
            </a>
        </p>
    </div>

    <script>
        jQuery(document).on('click', '.thryvewp-fse-pro-notice .notice-dismiss', function () {
            jQuery.post(ajaxurl, {
                action: 'thryvewp_business_dismiss_pro_notice'
            });
        });
    </script>
    <?php
}
add_action( 'admin_notices', 'thryvewp_business_pro_admin_notice' );


/**
 * Handle dismiss
 */
function thryvewp_business_dismiss_pro_notice() {
    update_user_meta( get_current_user_id(), 'thryvewp_business_pro_notice_dismissed', 1 );
    wp_die();
}
add_action( 'wp_ajax_thryvewp_business_dismiss_pro_notice', 'thryvewp_business_dismiss_pro_notice' );
