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

class AISB_Onboarding {

    public static function init() {
        add_action('wp_ajax_aisb_complete_onboarding', [__CLASS__, 'ajax_complete']);
    }

    public static function is_done() {
        return !empty(aisb_get_settings()['onboarding_done']);
    }

    public static function render_modal() {
        if (self::is_done()) {
            return;
        }
        ?>
        <div id="aisb-onboarding" class="aisb-onboarding-modal" role="dialog" aria-modal="true" aria-labelledby="aisb-onboarding-title">
            <div class="aisb-onboarding-backdrop"></div>
            <div class="aisb-onboarding-panel pewit-card">
                <h2 id="aisb-onboarding-title"><?php esc_html_e('Welcome to Pewit AI Schema Builder', 'pewit-ai-schema-builder'); ?></h2>
                <ol class="aisb-onboarding-steps">
                    <li><?php esc_html_e('Add your OpenAI API key in Settings and click Save & Test.', 'pewit-ai-schema-builder'); ?></li>
                    <li><?php esc_html_e('Enable post types and pick default schema types (Event, Product, FAQ…).', 'pewit-ai-schema-builder'); ?></li>
                    <li><?php esc_html_e('Open any post → Generate schema → Save to post.', 'pewit-ai-schema-builder'); ?></li>
                </ol>
                <p class="description"><?php esc_html_e('Pro unlocks bulk jobs, auto-generate on publish, and scheduled validation scans.', 'pewit-ai-schema-builder'); ?></p>
                <p class="aisb-onboarding-actions">
                    <a class="pewit-btn pewit-btn-primary" href="<?php echo esc_url(aisb_admin_url('settings')); ?>" id="aisb-onboarding-settings"><?php esc_html_e('Open Settings', 'pewit-ai-schema-builder'); ?></a>
                    <a class="pewit-btn pewit-btn-secondary" href="<?php echo esc_url(admin_url('edit.php')); ?>" id="aisb-onboarding-posts"><?php esc_html_e('Edit posts', 'pewit-ai-schema-builder'); ?></a>
                    <button type="button" class="button button-link" id="aisb-onboarding-dismiss"><?php esc_html_e('Skip for now', 'pewit-ai-schema-builder'); ?></button>
                </p>
            </div>
        </div>
        <?php
    }

    public static function ajax_complete() {
        check_ajax_referer('aisb_nonce', 'nonce');
        if (!current_user_can('manage_options')) {
            wp_send_json_error(['message' => __('Permission denied', 'pewit-ai-schema-builder')], 403);
        }
        $s = aisb_get_settings();
        $s['onboarding_done'] = 1;
        aisb_update_settings($s);
        wp_send_json_success(['message' => __('Onboarding dismissed.', 'pewit-ai-schema-builder')]);
    }
}
