<?php
/**
 * Volade Onboarding — First-run tutorial
 *
 * @package Volade\SDK
 * @license GPL-2.0-or-later
 */

trait Volade_Onboarding {

    public function init_onboarding() {
        add_action( 'admin_footer', array( $this, 'maybe_render_onboarding' ), 5 );
        add_action( 'wp_ajax_volade_complete_onboarding', array( $this, 'ajax_complete_onboarding' ) );
        add_action( 'wp_ajax_volade_skip_onboarding', array( $this, 'ajax_skip_onboarding' ) );
    }

    public function has_completed_onboarding( $user_id = 0 ) {
        if ( ! $user_id ) {
            $user = $this->get_current_user();
            if ( ! $user ) {
                return true;
            }
            $user_id = $user['id'];
        }
        return (bool) get_option( 'volade_onboarding_' . $user_id, false );
    }

    public function complete_onboarding( $user_id = 0 ) {
        if ( ! $user_id ) {
            $user = $this->get_current_user();
            if ( ! $user ) {
                return false;
            }
            $user_id = $user['id'];
        }
        update_option( 'volade_onboarding_' . $user_id, 1, false );
        $this->add_xp( $user_id, 'onboarding_complete' );
        $this->log_event( $user_id, 'onboarding_complete', array() );
        return true;
    }

    public function ajax_complete_onboarding() {
        check_ajax_referer( 'volade_nonce', 'nonce' );
        if ( ! $this->is_user_logged_in() ) {
            wp_send_json_error( __( 'Non authentifié.', 'php-compatibility-checker-by-volade' ) );
        }
        $this->complete_onboarding();
        wp_send_json_success( array( 'redirect' => admin_url( 'admin.php?page=volade-discover' ) ) );
    }

    public function ajax_skip_onboarding() {
        check_ajax_referer( 'volade_nonce', 'nonce' );
        if ( ! $this->is_user_logged_in() ) {
            wp_send_json_error( __( 'Non authentifié.', 'php-compatibility-checker-by-volade' ) );
        }
        $user = $this->get_current_user();
        update_option( 'volade_onboarding_' . $user['id'], 1, false );
        wp_send_json_success();
    }

    public function maybe_render_onboarding() {
        if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
            return;
        }
        $screen = get_current_screen();
        if ( ! $screen || strpos( $screen->id, 'volade' ) === false ) {
            return;
        }
        if ( ! $this->is_user_logged_in() ) {
            return;
        }
        $user = $this->get_current_user();
        if ( ! $user || $this->has_completed_onboarding( $user['id'] ) ) {
            return;
        }
        include dirname( __FILE__ ) . '/views/admin-onboarding.php';
        if ( wp_script_is( 'volade-sdk-admin', 'enqueued' ) ) {
            wp_add_inline_script( 'volade-sdk-admin', 'if (window.voladeBindOnboarding) { window.voladeBindOnboarding(); }', 'after' );
        }
    }

    public function get_onboarding_steps() {
        return array(
            array(
                'icon'  => '⚡',
                'title' => __( 'Bienvenue sur Volade', 'php-compatibility-checker-by-volade' ),
                'desc'  => __( 'Chaque plugin fonctionne sans compte. Votre compte gratuit débloque plus de fonctionnalités et des limites plus élevées.', 'php-compatibility-checker-by-volade' ),
            ),
            array(
                'icon'  => '🔍',
                'title' => __( 'Découvrir les plugins', 'php-compatibility-checker-by-volade' ),
                'desc'  => __( 'Parcourez les forks, outils WooCommerce, plugins sectoriels français, et plus encore.', 'php-compatibility-checker-by-volade' ),
            ),
            array(
                'icon'  => '🎁',
                'title' => __( 'Gagner de l\'XP et des badges', 'php-compatibility-checker-by-volade' ),
                'desc'  => __( 'Installez des plugins, parrainez des amis et maintenez votre série pour monter de niveau.', 'php-compatibility-checker-by-volade' ),
            ),
            array(
                'icon'  => '⭐',
                'title' => __( 'Passer à V+', 'php-compatibility-checker-by-volade' ),
                'desc'  => __( 'V+ débloque toutes les fonctionnalités premium de chaque plugin Volade — plus votre licence multi-sites.', 'php-compatibility-checker-by-volade' ),
            ),
        );
    }
}
