<?php
/**
 * Activation, welcome/onboarding, and ethical review notice.
 *
 * @package EyewearVirtualTryOn
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Activation side effects (called from main plugin file hook).
 *
 * @return void
 */
function eyewvitr_activate() {
    if (!get_option('eyewvitr_activated_at')) {
        update_option('eyewvitr_activated_at', time(), false);
    }

    set_transient('eyewvitr_show_welcome', 1, 60);
}

/**
 * Redirect to welcome after activation.
 *
 * @return void
 */
function eyewvitr_maybe_redirect_welcome() {
    if (!get_transient('eyewvitr_show_welcome')) {
        return;
    }

    delete_transient('eyewvitr_show_welcome');

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

    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    if (isset($_GET['activate-multi'])) {
        return;
    }

    wp_safe_redirect(admin_url('admin.php?page=eyewvitr-welcome'));
    exit;
}
add_action('admin_init', 'eyewvitr_maybe_redirect_welcome');

/**
 * Welcome / setup guide page.
 *
 * @return void
 */
function eyewvitr_welcome_page() {
    if (!current_user_can('manage_options')) {
        return;
    }

    $settings  = eyewvitr_get_settings();
    $connected = eyewvitr_is_connected();
    ?>
    <div class="eyewvitr-admin wrap">
        <div class="eyewvitr-welcome">
            <div class="eyewvitr-logo eyewvitr-logo--lg" aria-hidden="true">T</div>
            <p class="eyewvitr-eyebrow"><?php esc_html_e('Welcome', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
            <h1><?php esc_html_e('Welcome to Tryonixs', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h1>
            <p class="eyewvitr-hero__sub"><?php esc_html_e('Connect your account, enable products, and launch AI shopping in minutes.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>

            <div class="eyewvitr-welcome-steps">
                <div class="eyewvitr-welcome-step">
                    <span class="eyewvitr-welcome-num">1</span>
                    <h3><?php esc_html_e('Create Tryonixs Account', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h3>
                    <p><?php esc_html_e('Open tryonixs.com and create your merchant account.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                    <a class="eyewvitr-btn eyewvitr-btn--primary" href="https://tryonixs.com" target="_blank" rel="noopener noreferrer">
                        <?php esc_html_e('Create Tryonixs Account', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </a>
                </div>

                <div class="eyewvitr-welcome-step">
                    <span class="eyewvitr-welcome-num">2</span>
                    <h3><?php esc_html_e('Add API Key', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h3>
                    <p><?php esc_html_e('Paste your public API key (pk_live_…) to connect this store.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                    <?php if ($connected) : ?>
                        <p class="eyewvitr-status eyewvitr-status--ok"><?php esc_html_e('API key connected', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                    <?php else : ?>
                        <p class="eyewvitr-muted"><?php echo esc_html(sprintf(/* translators: %s: masked placeholder */ __('Current key: %s', 'tryonixs-virtual-try-on-for-eyewear-stores'), $settings['api_key'] !== '' ? '••••••••' : '—')); ?></p>
                    <?php endif; ?>
                    <a class="eyewvitr-btn eyewvitr-btn--secondary" href="<?php echo esc_url(admin_url('admin.php?page=eyewvitr-settings')); ?>">
                        <?php esc_html_e('Connect Account', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </a>
                </div>

                <div class="eyewvitr-welcome-step">
                    <span class="eyewvitr-welcome-num">3</span>
                    <h3><?php esc_html_e('Enable Try-On', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h3>
                    <p><?php esc_html_e('Open an eyewear product, keep Virtual Try-On enabled, and publish with a featured image.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                    <?php if (eyewvitr_is_woocommerce_active()) : ?>
                        <a class="eyewvitr-btn eyewvitr-btn--secondary" href="<?php echo esc_url(admin_url('edit.php?post_type=product')); ?>">
                            <?php esc_html_e('Open Products', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                        </a>
                    <?php endif; ?>
                </div>

                <div class="eyewvitr-welcome-step is-final">
                    <span class="eyewvitr-welcome-num">4</span>
                    <h3><?php esc_html_e('You are ready', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></h3>
                    <p><?php esc_html_e('Your store is ready for AI shopping. Visit a product page and try the button.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
                    <a class="eyewvitr-btn eyewvitr-btn--primary" href="<?php echo esc_url(admin_url('admin.php?page=eyewvitr-settings')); ?>">
                        <?php esc_html_e('Go to Dashboard', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <?php
}

/**
 * Whether review notice should appear.
 *
 * @return bool
 */
function eyewvitr_should_show_review_notice() {
    if (!current_user_can('manage_options')) {
        return false;
    }

    if (get_option('eyewvitr_review_dismissed') === 'yes') {
        return false;
    }

    if (!eyewvitr_is_connected()) {
        return false;
    }

    $activated = (int) get_option('eyewvitr_activated_at', 0);
    if ($activated <= 0 || (time() - $activated) < (14 * DAY_IN_SECONDS)) {
        return false;
    }

    if ((int) get_option('eyewvitr_tryon_opens', 0) < 1) {
        return false;
    }

    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

    return strpos($page, 'eyewvitr') === 0;
}

/**
 * Render ethical review request.
 *
 * @return void
 */
function eyewvitr_review_notice() {
    if (!eyewvitr_should_show_review_notice()) {
        return;
    }

    $dismiss_url = wp_nonce_url(
        add_query_arg('eyewvitr_dismiss_review', '1'),
        'eyewvitr_dismiss_review'
    );

    $review_url = 'https://wordpress.org/support/plugin/tryonixs-virtual-try-on-for-eyewear-stores/reviews/#new-post';
    ?>
    <div class="notice eyewvitr-review-notice">
        <p><strong><?php esc_html_e('Enjoying Tryonixs Virtual Try-On?', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></strong></p>
        <p><?php esc_html_e('Help other WooCommerce stores discover AI shopping.', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></p>
        <p>
            <a class="button button-primary" href="<?php echo esc_url($review_url); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e('★★★★★ Leave a Review', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></a>
            <a class="button" href="<?php echo esc_url($dismiss_url); ?>"><?php esc_html_e('Maybe later', 'tryonixs-virtual-try-on-for-eyewear-stores'); ?></a>
        </p>
    </div>
    <?php
}
add_action('admin_notices', 'eyewvitr_review_notice');

/**
 * Dismiss review notice.
 *
 * @return void
 */
function eyewvitr_handle_review_dismiss() {
    if (!isset($_GET['eyewvitr_dismiss_review'])) {
        return;
    }

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

    check_admin_referer('eyewvitr_dismiss_review');
    update_option('eyewvitr_review_dismissed', 'yes', false);

    wp_safe_redirect(remove_query_arg(['eyewvitr_dismiss_review', '_wpnonce']));
    exit;
}
add_action('admin_init', 'eyewvitr_handle_review_dismiss');
