<?php
/**
 * Onboarding state: tracks the "pending" flag that activator sets and the
 * dashboard notice respects.
 *
 * @package Brainwerk Security Suite
 */

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

class Shieldforge_Onboarding {

    const FLAG = 'shieldforge_onboarding_pending';

    public static function register( Shieldforge_Loader $loader ) {
        $loader->add_action( 'admin_notices', __CLASS__, 'maybe_render_notice' );
        $loader->add_action( 'admin_init', __CLASS__, 'maybe_skip' );
        $loader->add_action( 'admin_init', __CLASS__, 'maybe_save_language' );
    }

    /**
     * Shortcut to the wizard's i18n table.
     */
    public static function t( $key ) {
        return Shieldforge_Onboarding_I18n::t( $key );
    }

    public static function maybe_save_language() {
        if ( empty( $_POST['shieldforge_onboarding_action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
            return;
        }
        if ( 'save_language' !== sanitize_key( wp_unslash( $_POST['shieldforge_onboarding_action'] ) ) ) {
            return;
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        if ( ! check_admin_referer( 'shieldforge_onboarding_lang' ) ) {
            return;
        }
        $locale = isset( $_POST['shieldforge_locale'] ) ? sanitize_key( wp_unslash( $_POST['shieldforge_locale'] ) ) : '';
        if ( Shieldforge_Onboarding_I18n::is_supported( $locale ) ) {
            Shieldforge_Onboarding_I18n::set_locale( $locale );
        }
        wp_safe_redirect( self::step_url( 'welcome' ) );
        exit;
    }

    public static function is_pending() {
        return (bool) get_site_option( self::FLAG, 0 );
    }

    public static function mark_pending() {
        update_site_option( self::FLAG, 1 );
    }

    public static function mark_complete() {
        delete_site_option( self::FLAG );
    }

    public static function maybe_skip() {
        if ( empty( $_GET['shieldforge_skip_onboarding'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
            return;
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        if ( ! check_admin_referer( 'shieldforge_skip_onboarding' ) ) {
            return;
        }
        self::mark_complete();
        wp_safe_redirect( admin_url( 'admin.php?page=' . Shieldforge_Admin::MENU_SLUG ) );
        exit;
    }

    public static function maybe_render_notice() {
        if ( ! self::is_pending() ) {
            return;
        }
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        if ( $screen && false !== strpos( (string) $screen->id, 'brainwerk-security-suite' ) ) {
            return;
        }
        $start_url = admin_url( 'admin.php?page=' . Shieldforge_Admin::MENU_SLUG . '&tab=onboarding' );
        $skip_url  = wp_nonce_url(
            admin_url( 'admin.php?shieldforge_skip_onboarding=1' ),
            'shieldforge_skip_onboarding'
        );
        ?>
        <div class="notice notice-info shieldforge-notice">
            <p>
                <strong><?php esc_html_e( 'Welcome to Brainwerk Security Suite.', 'brainwerk-security-suite' ); ?></strong>
                <?php esc_html_e( 'A short setup wizard will help you tune the privacy, login, and notification defaults.', 'brainwerk-security-suite' ); ?>
            </p>
            <p>
                <a class="button button-primary" href="<?php echo esc_url( $start_url ); ?>"><?php esc_html_e( 'Start setup', 'brainwerk-security-suite' ); ?></a>
                <a class="button button-link" href="<?php echo esc_url( $skip_url ); ?>"><?php esc_html_e( 'Skip', 'brainwerk-security-suite' ); ?></a>
            </p>
        </div>
        <?php
    }

    public static function steps() {
        return array(
            'language'      => self::t( 'step_language' ),
            'welcome'       => self::t( 'step_welcome' ),
            'privacy'       => self::t( 'step_privacy' ),
            'login'         => self::t( 'step_login' ),
            'notifications' => self::t( 'step_notifications' ),
            'done'          => self::t( 'step_done' ),
        );
    }

    public static function current_step() {
        $steps = array_keys( self::steps() );
        // First-run default: language picker. After it's been set, default
        // to welcome so re-entering the wizard doesn't keep nagging about
        // language.
        $default = is_multisite()
            ? get_site_option( Shieldforge_Onboarding_I18n::OPTION_KEY, '' )
            : get_option( Shieldforge_Onboarding_I18n::OPTION_KEY, '' );
        $fallback = $default ? 'welcome' : 'language';
        $step = isset( $_GET['step'] ) ? sanitize_key( wp_unslash( $_GET['step'] ) ) : $fallback; // phpcs:ignore WordPress.Security.NonceVerification
        return in_array( $step, $steps, true ) ? $step : $fallback;
    }

    public static function step_url( $step ) {
        return add_query_arg(
            array( 'page' => Shieldforge_Admin::MENU_SLUG, 'tab' => 'onboarding', 'step' => $step ),
            is_network_admin() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' )
        );
    }
}
