<?php
if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}
/**
 * Theme activation notice.
 *
 * @package Welding for Elementor
 */

defined('ABSPATH') || exit;

/**
 * Documentation URL constant.
 */
if (! defined('WELDING_PRO_FOR_ELEMENTOR')) {
    define('WELDING_PRO_FOR_ELEMENTOR', 'https://muhammadtarekreza.lemonsqueezy.com/checkout/buy/565b42aa-d6cb-4c9d-9e51-1aa2b1871ba9');
}
if (! defined('WELDING_PRO_FOR_ELEMENTOR_DEMO_URL')) {
    define('WELDING_PRO_FOR_ELEMENTOR_DEMO_URL', 'https://welding.muhammadtarekreza.com/');
}

/**
 * Set transient to display activation notice.
 *
 * @return void
 */
function welding_for_elementor_activation_notice()
{
    set_transient('welding_for_elementor_activation_notice', true, 30 * MINUTE_IN_SECONDS);
}
add_action('after_switch_theme', 'welding_for_elementor_activation_notice');

/**
 * Display activation notice with links to documentation.
 *
 * @return void
 */
function welding_for_elementor_display_activation_notice()
{
    if (! get_transient('welding_for_elementor_activation_notice')) {
        return;
    }
    $nonce = wp_create_nonce('welding_for_elementor_dismiss_notice');
?>
    <div class="notice notice-info is-dismissible" id="welding-for-elementor-activation-notice" data-nonce="<?php echo esc_attr($nonce); ?>">
        <h2>
            <?php esc_html_e('Welcome to Welding for Elementor Theme', 'welding-for-elementor'); ?>
        </h2>
        <p>
            <?php esc_html_e('Skip hours of setup — import a fully built welding workshops site instantly. Welding Pro for Elementor includes premium starter templates, and Elementor widgets.', 'welding-for-elementor'); ?>
        </p>
        <div id="btn">

            <a href="<?php echo esc_url(WELDING_PRO_FOR_ELEMENTOR); ?>" class="button button-primary" target="_blank" rel="noopener noreferrer">
                <?php esc_html_e('Get Welding Pro for Elementor', 'welding-for-elementor'); ?>
            </a>
            <a href="<?php echo esc_url(WELDING_PRO_FOR_ELEMENTOR_DEMO_URL); ?>" class="button" target="_blank" rel="noopener noreferrer">
                <?php esc_html_e('View Live Demo', 'welding-for-elementor'); ?>
            </a>
        </div>

    </div>
<?php
}
add_action('admin_notices', 'welding_for_elementor_display_activation_notice');

/**
 * Handle AJAX request to dismiss notice.
 *
 * @return void
 */
function welding_for_elementor_dismiss_notice_ajax()
{
    // Verify user has admin access
    if (! current_user_can('manage_options')) {
        wp_send_json_error('Unauthorized');
    }

    // Verify nonce for security
    check_ajax_referer('welding_for_elementor_dismiss_notice');

    // Delete the transient from server
    delete_transient('welding_for_elementor_activation_notice');

    // Send success response
    wp_send_json_success('Notice dismissed');
}
add_action('wp_ajax_welding_for_elementor_dismiss_notice', 'welding_for_elementor_dismiss_notice_ajax');
