<?php
/**
 * File Path: includes/admin/class-mbs-setup-wizard-render.php
 *
 * MBS Setup Wizard v2 — Render Class
 *
 * Outputs the React mount point (#mbs-wizard-root) and enqueues
 * wizard assets via MBS_Setup_Wizard::enqueue_assets().
 *
 * The full UI is rendered client-side by assets/js/wizard.js using
 * wp.element (WordPress-bundled React). No PHP step rendering.
 *
 * @package MyBookShowroom
 * @since   1.2.0
 */

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

class MBS_Setup_Wizard_Render { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

    /**
     * Render the wizard page shell and mount the React app.
     *
     * Registered as the add_submenu_page() callback in MBS_Setup_Wizard::register_page().
     */
    public static function render(): void {
        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( esc_html__( 'You do not have permission to view this page.', 'my-book-showroom' ) );
        }

        // Redirect to dashboard if wizard is already done, skipped, or dismissed.
        $uid = get_current_user_id();
        if (
            get_user_meta( $uid, MBS_Setup_Wizard::META_DONE, true ) ||
            get_user_meta( $uid, MBS_Setup_Wizard::META_SKIP, true ) ||
            get_option( MBS_Setup_Wizard::DISMISSED_KEY )
        ) {
            $_mbs_redir = admin_url( 'admin.php?page=my-book-showroom' );
            if ( ! headers_sent() ) {
                wp_safe_redirect( $_mbs_redir );
                exit;
            }
            // Headers already sent (WP core early output) — use meta+JS fallback.
            echo '<meta http-equiv="refresh" content="0;url=' . esc_attr( $_mbs_redir ) . '">';
            echo '<script>window.location.replace("' . esc_js( $_mbs_redir ) . '");</script>';
            exit;
        }

        ?>
        <div id="mbs-wizard-root">
            <?php /* React mounts here. Styles apply to the inner .mbs-wizard-frame. */ ?>
        </div>
        <noscript>
            <div style="padding:40px;font-family:Arial,sans-serif;text-align:center;">
                <strong><?php esc_html_e( 'JavaScript is required to run the Setup Wizard.', 'my-book-showroom' ); ?></strong>
                <p>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=my-book-showroom' ) ); ?>">
                        <?php esc_html_e( 'Go to Dashboard instead', 'my-book-showroom' ); ?>
                    </a>
                </p>
            </div>
        </noscript>
        <?php
    }
}
