<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Template-scope variables and single-post admin meta reads, not globals or slow queries.
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Main wizard shell - renders the tab bar and includes each tab view.
 *
 * Variables available (set by TNC_Flipbook_3D_Admin::render_metabox()):
 *   $post     WP_Post object
 *   $meta     Array of all meta values for this post (keyed by meta key without prefix)
 *   $schema   TNC_Flipbook_3D_Meta_Schema::get() result
 *   $global   Global settings option array
 */

$is_pro = function_exists( 'tnc_flipbook_3d_is_pro' ) && tnc_flipbook_3d_is_pro();

$tabs = array(
    'content'     => __( '1. Content', 'tnc-flipbook-3d-lite' ),
    'general'     => __( '2. General', 'tnc-flipbook-3d-lite' ),
    'appearance'  => __( '3. Display', 'tnc-flipbook-3d-lite' ),
    'toolbar'     => __( '4. Toolbar', 'tnc-flipbook-3d-lite' ),

    'seo'         => __( '6. SEO', 'tnc-flipbook-3d-lite' ),
);

$tabs = apply_filters( 'tncfb3d_metabox_tabs', $tabs );

// Auto-number tabs
$i = 1;
foreach ( $tabs as $key => $label ) {
    $tabs[ $key ] = $i . '. ' . preg_replace( '/^\d+\.\s*/', '', $label );
    $i++;
}

// Tabs locked in the Lite version
$locked_tabs = array();
if ( ! $is_pro ) {
    $locked_tabs = array();
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI tab selector; value is sanitized and validated against $tabs below.
$active_tab = isset( $_GET['tncfb3d_tab'] ) ? sanitize_key( wp_unslash( $_GET['tncfb3d_tab'] ) ) : 'content';
if ( ! array_key_exists( $active_tab, $tabs ) ) {
    $active_tab = 'content';
}

wp_nonce_field( 'tncfb3d_save_meta_' . $post->ID, 'tncfb3d_meta_nonce' );
?>
<div class="tnc-flipbook-admin tncfb-wizard-wrap">

    <!-- Tab navigation -->
    <nav class="tncfb-tab-nav" role="tablist">
        <?php foreach ( $tabs as $key => $title ) :
            $is_locked = in_array( $key, $locked_tabs, true );
            $classes   = 'tncfb-tab-btn';
            if ( $key === $active_tab ) { $classes .= ' is-active'; }
            if ( $is_locked )           { $classes .= ' tncfb-tab-locked'; }
        ?>
            <button
                type="button"
                role="tab"
                class="<?php echo esc_attr( $classes ); ?>"
                data-tab="<?php echo esc_attr( $key ); ?>"
                aria-selected="<?php echo $key === $active_tab ? 'true' : 'false'; ?>"
                aria-controls="tncfb-panel-<?php echo esc_attr( $key ); ?>">
                <?php echo esc_html( $title ); ?>
            </button>
        <?php endforeach; ?>
        <input type="hidden" name="tncfb3d_active_tab" id="tncfb3d_active_tab" value="<?php echo esc_attr( $active_tab ); ?>" />
    </nav>

    <!-- Tab panels -->
    <div class="tncfb-tab-panels">
        <?php foreach ( array_keys( $tabs ) as $key ) :
            $is_locked = in_array( $key, $locked_tabs, true );
        ?>
            <div
                id="tncfb-panel-<?php echo esc_attr( $key ); ?>"
                role="tabpanel"
                class="tncfb-tab-panel<?php echo $key === $active_tab ? ' is-active' : ''; ?>"
                aria-hidden="<?php echo $key === $active_tab ? 'false' : 'true'; ?>">
                <?php
                if ( $is_locked ) {
                    // Show Pro teaser for locked tabs
                    $teasers = array(
                        'appearance' => array(
                            'title'    => __( 'Appearance Settings', 'tnc-flipbook-3d-lite' ),
                            'icon'     => 'dashicons-art',
                            'features' => array(
                                __( '17 color schemes + custom scheme builder', 'tnc-flipbook-3d-lite' ),
                                __( '4 toolbar layout positions', 'tnc-flipbook-3d-lite' ),
                                __( 'Paper type, thickness, shininess controls', 'tnc-flipbook-3d-lite' ),
                                __( 'Camera distance, angle, FOV, book tilt', 'tnc-flipbook-3d-lite' ),
                                __( 'Custom flip duration & loading logo', 'tnc-flipbook-3d-lite' ),
                                __( 'Shadow, lighting, and render scale settings', 'tnc-flipbook-3d-lite' ),
                            ),
                        ),
                        'protection' => array(
                            'title'    => __( 'Content Protection / DRM', 'tnc-flipbook-3d-lite' ),
                            'icon'     => 'dashicons-shield',
                            'features' => array(
                                __( 'Text & image watermark system', 'tnc-flipbook-3d-lite' ),
                                __( 'Domain locking (restrict to allowed domains)', 'tnc-flipbook-3d-lite' ),
                                __( 'Disable right-click, keyboard shortcuts, image drag', 'tnc-flipbook-3d-lite' ),
                                __( 'DevTools detection with configurable actions', 'tnc-flipbook-3d-lite' ),
                                __( 'Print protection with watermark', 'tnc-flipbook-3d-lite' ),
                                __( 'Screenshot deterrent & blob URL conversion', 'tnc-flipbook-3d-lite' ),
                            ),
                        ),
                        'readaloud' => array(
                            'title'    => __( 'Read Aloud (Text-to-Speech)', 'tnc-flipbook-3d-lite' ),
                            'icon'     => 'dashicons-microphone',
                            'features' => array(
                                __( 'Browser-based text-to-speech (free voices)', 'tnc-flipbook-3d-lite' ),
                                __( 'Google Cloud TTS with 400+ voices', 'tnc-flipbook-3d-lite' ),
                                __( 'ElevenLabs AI voices with natural intonation', 'tnc-flipbook-3d-lite' ),
                                __( 'Azure Speech with 500+ neural voices', 'tnc-flipbook-3d-lite' ),
                                __( 'Amazon Polly with NTTS support', 'tnc-flipbook-3d-lite' ),
                                __( 'OpenAI TTS with HD voices', 'tnc-flipbook-3d-lite' ),
                                __( 'Pre-generate audio for instant playback', 'tnc-flipbook-3d-lite' ),
                                __( 'Playback speed control and auto-flip', 'tnc-flipbook-3d-lite' ),
                            ),
                        ),
                        'advanced' => array(
                            'title'    => __( 'Advanced Features', 'tnc-flipbook-3d-lite' ),
                            'icon'     => 'dashicons-admin-tools',
                            'features' => array(
                                __( 'Visual click-to-place hotspot editor', 'tnc-flipbook-3d-lite' ),
                                __( 'Page video overlays with positioning', 'tnc-flipbook-3d-lite' ),
                                __( 'Custom table of contents', 'tnc-flipbook-3d-lite' ),
                                __( 'Google Analytics 4 integration', 'tnc-flipbook-3d-lite' ),
                                __( 'Text-to-Speech with 6 AI providers', 'tnc-flipbook-3d-lite' ),
                            ),
                        ),
                        'woocommerce' => array(
                            'title'    => __( 'WooCommerce Integration', 'tnc-flipbook-3d-lite' ),
                            'icon'     => 'dashicons-cart',
                            'features' => array(
                                __( 'Link flipbooks to WooCommerce products', 'tnc-flipbook-3d-lite' ),
                                __( 'Gate pages behind purchase', 'tnc-flipbook-3d-lite' ),
                                __( 'Configurable preview page limit', 'tnc-flipbook-3d-lite' ),
                                __( 'Custom gating message & purchase redirect', 'tnc-flipbook-3d-lite' ),
                                __( 'Automatic product page preview embed', 'tnc-flipbook-3d-lite' ),
                            ),
                        ),
                    );
                    if ( isset( $teasers[ $key ] ) ) {
                        $pro_teaser = $teasers[ $key ];
                        include TNC_FLIPBOOK_3D_DIR . 'admin/views/pro-teaser.php';
                    }
                } else {
                    $view_file = TNC_FLIPBOOK_3D_DIR . 'admin/views/metabox-' . $key . '.php';
                    if ( file_exists( $view_file ) ) {
                        include $view_file;
                    }
                }
                do_action( "tncfb3d_metabox_tab_content_{$key}", $post, $meta, $schema );
                do_action( 'tncfb3d_metabox_after_tab_content', $key, $post, $meta );
                ?>
            </div>
        <?php endforeach; ?>
    </div>

</div>
