<?php
/**
 * Activation Wizard Handler.
 *
 * @package PrimeLink
 */

namespace PrimeLink\Core\Admin;

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

final class ActivationWizard {

    /**
     * Initialize activation wizard.
     */
    public static function init() {
        if ( ! is_admin() ) {
            return;
        }

        // Check if wizard should be shown
        add_action( 'admin_init', array( __CLASS__, 'maybe_show_wizard' ) );
        
        // Enqueue wizard assets
        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
    }

    /**
     * Check if activation wizard should be displayed.
     */
    public static function maybe_show_wizard() {
        // Only show to administrators
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Check if wizard was already completed
        $wizard_completed = get_option( 'mt_primelink_wizard_completed', false );
        if ( $wizard_completed ) {
            // Clean up the show flag if it still exists
            delete_option( 'mt_primelink_show_activation_wizard' );
            return;
        }

        // Check if wizard was skipped
        $wizard_skipped = get_option( 'mt_primelink_wizard_skipped', false );
        if ( $wizard_skipped ) {
            // Don't show wizard if user skipped it - only show after restart
            return;
        }

        // Check if wizard should be shown
        $show_wizard = get_option( 'mt_primelink_show_activation_wizard', false );
        
        if ( ! $show_wizard ) {
            return;
        }

        // Don't show on AJAX requests
        if ( wp_doing_ajax() ) {
            return;
        }

        // Don't show on REST API requests
        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
            return;
        }

        // Show wizard as modal overlay on all admin pages until it is skipped or completed.
        add_action( 'admin_footer', array( __CLASS__, 'render_wizard' ) );
    }

    /**
     * Enqueue wizard assets.
     */
    public static function enqueue_assets( $hook ) {
        // Only enqueue if wizard should be shown
        $show_wizard = get_option( 'mt_primelink_show_activation_wizard', false );
        
        if ( ! $show_wizard ) {
            return;
        }

        // Enqueue wizard CSS (minified when available).
        $css_relative = 'css/activation-wizard.css';
        wp_enqueue_style(
            'mt-primelink-activation-wizard',
            Assets::get_asset_url( $css_relative ),
            array(),
            Assets::get_asset_version( $css_relative )
        );

        // Enqueue wizard JS (minified when available).
        $js_relative = 'js/activation-wizard.js';
        wp_enqueue_script(
            'mt-primelink-activation-wizard',
            Assets::get_asset_url( $js_relative ),
            array( 'jquery' ),
            Assets::get_asset_version( $js_relative ),
            true
        );

        // Localize script
        wp_localize_script(
            'mt-primelink-activation-wizard',
            'mtPrimeLinkWizard',
            array(
                'restUrl'   => esc_url_raw( rest_url( 'primelink/v1' ) ),
                'restNonce' => wp_create_nonce( 'wp_rest' ),
                'adminUrl'  => admin_url(),
            )
        );
    }

    /**
     * Render activation wizard.
     */
    public static function render_wizard() {
        include MT_PRIMELINK_PATH . 'templates/admin/activation-wizard/wizard.php';
    }

    /**
     * Mark wizard as completed.
     */
    public static function complete_wizard() {
        delete_option( 'mt_primelink_show_activation_wizard' );
        update_option( 'mt_primelink_wizard_completed', true, false );
        update_option( 'mt_primelink_wizard_completed_time', current_time( 'mysql' ), false );
    }
}

