                <section class="eep-settings-panel" id="eep-tab-chatbot" data-tab="chatbot" data-subtab="chatbot">
                    <h2><?php esc_html_e('AI Listing Chatbot', 'nexdirectory'); ?></h2>
                    <p class="eep-settings-subheading"><?php esc_html_e('AI assistant powered by GPT-5.2 + embeddings over published listings. It re-indexes on publish/verification and on a recurring schedule.', 'nexdirectory'); ?></p>
                    <?php if (!$premium_unlocked) : ?>
                        <div class="eep-settings-field eep-full">
                            <p><strong><?php esc_html_e('Premium feature', 'nexdirectory'); ?></strong></p>
                            <p class="description"><?php esc_html_e('AI Listing Chatbot is available on the premium plan. Activate a valid license (or start trial) to enable chatbot settings and indexing tools.', 'nexdirectory'); ?></p>
                        </div>
                    <?php else : ?>
                    <div class="eep-settings-grid">
                        <div class="eep-settings-field eep-full">
                            <input type="hidden" name="eep_chatbot_enabled" value="0">
                            <label><input type="checkbox" name="eep_chatbot_enabled" value="1" <?php checked((string) get_option('eep_chatbot_enabled', '0'), '1'); ?>> <?php esc_html_e('Enable AI Listing Chatbot', 'nexdirectory'); ?></label>
                            <p class="description"><?php esc_html_e('Public users can ask natural-language questions like “AI listings in the next 2 weeks”.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field eep-full">
                            <input type="hidden" name="eep_chatbot_debug_mode" value="0">
                            <label><input type="checkbox" name="eep_chatbot_debug_mode" value="1" <?php checked((string) get_option('eep_chatbot_debug_mode', '0'), '1'); ?>> <?php esc_html_e('Show chatbot debug metadata (intent/mode/search gate)', 'nexdirectory'); ?></label>
                            <p class="description"><?php esc_html_e('Temporary troubleshooting mode. Adds an internal debug line under assistant replies for admins and testers.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_openai_api_key"><?php esc_html_e('OpenAI API Key', 'nexdirectory'); ?></label>
                            <input type="password" id="eep_chatbot_openai_api_key" name="eep_chatbot_openai_api_key" value="" autocomplete="off" placeholder="<?php esc_attr_e('Leave blank to keep existing API key', 'nexdirectory'); ?>">
                            <p class="description"><?php esc_html_e('Used for embeddings and answer generation.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_model"><?php esc_html_e('Text Model', 'nexdirectory'); ?></label>
                            <input type="text" id="eep_chatbot_model" name="eep_chatbot_model" value="<?php echo esc_attr((string) get_option('eep_chatbot_model', 'gpt-5.2')); ?>" placeholder="gpt-5.2">
                            <p class="description"><?php esc_html_e('Recommended: gpt-5.2', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_embedding_model"><?php esc_html_e('Embedding Model', 'nexdirectory'); ?></label>
                            <select id="eep_chatbot_embedding_model" name="eep_chatbot_embedding_model">
                                <option value="text-embedding-3-small" <?php selected((string) get_option('eep_chatbot_embedding_model', 'text-embedding-3-small'), 'text-embedding-3-small'); ?>>text-embedding-3-small</option>
                                <option value="text-embedding-3-large" <?php selected((string) get_option('eep_chatbot_embedding_model', 'text-embedding-3-small'), 'text-embedding-3-large'); ?>>text-embedding-3-large</option>
                            </select>
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_embedding_dimensions"><?php esc_html_e('Embedding Dimensions', 'nexdirectory'); ?></label>
                            <input type="number" min="128" max="3072" id="eep_chatbot_embedding_dimensions" name="eep_chatbot_embedding_dimensions" value="<?php echo esc_attr((string) get_option('eep_chatbot_embedding_dimensions', 512)); ?>">
                            <p class="description"><?php esc_html_e('Lower dimensions reduce storage and query cost.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field eep-full">
                            <?php
                            $chatbot_available_post_types = method_exists($this, 'get_chatbot_available_post_types')
                                ? (array) $this->get_chatbot_available_post_types()
                                : [self::POST_TYPE => __('Listings', 'nexdirectory')];
                            $chatbot_selected_post_types = method_exists($this, 'get_chatbot_selected_post_types')
                                ? (array) $this->get_chatbot_selected_post_types()
                                : [self::POST_TYPE];
                            ?>
                            <label for="eep_chatbot_index_post_types"><?php esc_html_e('Embedding Source Post Types', 'nexdirectory'); ?></label>
                            <select id="eep_chatbot_index_post_types" name="eep_chatbot_index_post_types[]" multiple size="7">
                                <?php foreach ($chatbot_available_post_types as $post_type_slug => $post_type_label) : ?>
                                    <option value="<?php echo esc_attr((string) $post_type_slug); ?>" <?php selected(in_array((string) $post_type_slug, $chatbot_selected_post_types, true)); ?>>
                                        <?php echo esc_html((string) $post_type_label . ' (' . (string) $post_type_slug . ')'); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <p class="description"><?php esc_html_e('Select one or more post types/CPTs to include in chatbot embeddings. Hold Ctrl/Cmd to select multiple options. After changes, click “Re-index Embeddings Now”.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field eep-full">
                            <input type="hidden" name="eep_chatbot_ai_catalog_enabled" value="0">
                            <label><input type="checkbox" name="eep_chatbot_ai_catalog_enabled" value="1" <?php checked((string) get_option('eep_chatbot_ai_catalog_enabled', '1'), '1'); ?>> <?php esc_html_e('Enable AI Knowledge Map Enrichment', 'nexdirectory'); ?></label>
                            <p class="description"><?php esc_html_e('During reindex, AI categorizes indexed content (types/topics/user ask patterns) and updates a knowledge map used for intent routing and better conversational context.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_name"><?php esc_html_e('Bot Name', 'nexdirectory'); ?></label>
                            <input type="text" id="eep_chatbot_name" name="eep_chatbot_name" value="<?php echo esc_attr((string) get_option('eep_chatbot_name', __('Listing Assistant', 'nexdirectory'))); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_welcome_text"><?php esc_html_e('Welcome Text', 'nexdirectory'); ?></label>
                            <input type="text" id="eep_chatbot_welcome_text" name="eep_chatbot_welcome_text" value="<?php echo esc_attr((string) get_option('eep_chatbot_welcome_text', __('Ask me about upcoming listings and I will recommend the best matches.', 'nexdirectory'))); ?>">
                        </div>
                        <div class="eep-settings-field eep-full">
                            <label for="eep_chatbot_personality"><?php esc_html_e('Personality / Instruction Prompt', 'nexdirectory'); ?></label>
                            <textarea id="eep_chatbot_personality" name="eep_chatbot_personality" rows="4" placeholder="<?php esc_attr_e('Example: Be concise, prioritize upcoming listings, and provide practical recommendations.', 'nexdirectory'); ?>"><?php echo esc_textarea((string) get_option('eep_chatbot_personality', '')); ?></textarea>
                        </div>
                        <div class="eep-settings-field">
                            <?php
                            $desktop_chatbot_position = sanitize_key((string) get_option('eep_chatbot_position_desktop', 'bottom-right'));
                            $desktop_allowed = $this->get_chatbot_position_options('desktop');
                            if (!in_array($desktop_chatbot_position, $desktop_allowed, true)) {
                                $desktop_chatbot_position = 'bottom-right';
                            }
                            ?>
                            <label for="eep_chatbot_position_desktop"><?php esc_html_e('Desktop Widget Position', 'nexdirectory'); ?></label>
                            <select id="eep_chatbot_position_desktop" name="eep_chatbot_position_desktop">
                                <option value="bottom-right" <?php selected($desktop_chatbot_position, 'bottom-right'); ?>><?php esc_html_e('Bottom Right (floating)', 'nexdirectory'); ?></option>
                                <option value="bottom-left" <?php selected($desktop_chatbot_position, 'bottom-left'); ?>><?php esc_html_e('Bottom Left (floating)', 'nexdirectory'); ?></option>
                                <option value="right-edge-center" <?php selected($desktop_chatbot_position, 'right-edge-center'); ?>><?php esc_html_e('Right Edge Center', 'nexdirectory'); ?></option>
                                <option value="left-edge-center" <?php selected($desktop_chatbot_position, 'left-edge-center'); ?>><?php esc_html_e('Left Edge Center', 'nexdirectory'); ?></option>
                                <option value="inline" <?php selected($desktop_chatbot_position, 'inline'); ?>><?php esc_html_e('Inline block', 'nexdirectory'); ?></option>
                            </select>
                        </div>
                        <div class="eep-settings-field">
                            <?php
                            $mobile_chatbot_position = sanitize_key((string) get_option('eep_chatbot_position_mobile', 'right-edge-center'));
                            $mobile_allowed = $this->get_chatbot_position_options('mobile');
                            if (!in_array($mobile_chatbot_position, $mobile_allowed, true)) {
                                $mobile_chatbot_position = 'right-edge-center';
                            }
                            ?>
                            <label for="eep_chatbot_position_mobile"><?php esc_html_e('Mobile Widget Position', 'nexdirectory'); ?></label>
                            <select id="eep_chatbot_position_mobile" name="eep_chatbot_position_mobile">
                                <option value="right-edge-center" <?php selected($mobile_chatbot_position, 'right-edge-center'); ?>><?php esc_html_e('Right Edge Center (launcher) + Centered Panel', 'nexdirectory'); ?></option>
                                <option value="left-edge-center" <?php selected($mobile_chatbot_position, 'left-edge-center'); ?>><?php esc_html_e('Left Edge Center (launcher) + Centered Panel', 'nexdirectory'); ?></option>
                                <option value="bottom-center" <?php selected($mobile_chatbot_position, 'bottom-center'); ?>><?php esc_html_e('Bottom Center', 'nexdirectory'); ?></option>
                                <option value="bottom-right" <?php selected($mobile_chatbot_position, 'bottom-right'); ?>><?php esc_html_e('Bottom Right', 'nexdirectory'); ?></option>
                                <option value="bottom-left" <?php selected($mobile_chatbot_position, 'bottom-left'); ?>><?php esc_html_e('Bottom Left', 'nexdirectory'); ?></option>
                                <option value="inline" <?php selected($mobile_chatbot_position, 'inline'); ?>><?php esc_html_e('Inline block', 'nexdirectory'); ?></option>
                            </select>
                            <p class="description"><?php esc_html_e('Use edge-center on mobile to avoid overlap with fixed bottom navigation bars.', 'nexdirectory'); ?></p>
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_theme_color"><?php esc_html_e('Theme Accent Color', 'nexdirectory'); ?></label>
                            <input type="color" id="eep_chatbot_theme_color" name="eep_chatbot_theme_color" value="<?php echo esc_attr((string) get_option('eep_chatbot_theme_color', '#FF5E14')); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_text_color"><?php esc_html_e('Text Color', 'nexdirectory'); ?></label>
                            <input type="color" id="eep_chatbot_text_color" name="eep_chatbot_text_color" value="<?php echo esc_attr((string) get_option('eep_chatbot_text_color', '#0B1221')); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_reindex_interval_hours"><?php esc_html_e('Holistic Reindex Interval (hours)', 'nexdirectory'); ?></label>
                            <input type="number" min="1" max="168" id="eep_chatbot_reindex_interval_hours" name="eep_chatbot_reindex_interval_hours" value="<?php echo esc_attr((string) get_option('eep_chatbot_reindex_interval_hours', 24)); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_top_k"><?php esc_html_e('Recommendations Per Answer', 'nexdirectory'); ?></label>
                            <input type="number" min="1" max="12" id="eep_chatbot_top_k" name="eep_chatbot_top_k" value="<?php echo esc_attr((string) get_option('eep_chatbot_top_k', 6)); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_max_candidates"><?php esc_html_e('Max Indexed Candidates Per Query', 'nexdirectory'); ?></label>
                            <input type="number" min="20" max="1200" id="eep_chatbot_max_candidates" name="eep_chatbot_max_candidates" value="<?php echo esc_attr((string) get_option('eep_chatbot_max_candidates', 250)); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_rate_limit_requests"><?php esc_html_e('Rate Limit Requests', 'nexdirectory'); ?></label>
                            <input type="number" min="3" max="120" id="eep_chatbot_rate_limit_requests" name="eep_chatbot_rate_limit_requests" value="<?php echo esc_attr((string) get_option('eep_chatbot_rate_limit_requests', 30)); ?>">
                        </div>
                        <div class="eep-settings-field">
                            <label for="eep_chatbot_rate_limit_window_seconds"><?php esc_html_e('Rate Limit Window (seconds)', 'nexdirectory'); ?></label>
                            <input type="number" min="60" max="3600" id="eep_chatbot_rate_limit_window_seconds" name="eep_chatbot_rate_limit_window_seconds" value="<?php echo esc_attr((string) get_option('eep_chatbot_rate_limit_window_seconds', 600)); ?>">
                        </div>
                        <div class="eep-settings-field eep-full">
                            <h3><?php esc_html_e('Index Stats', 'nexdirectory'); ?></h3>
                            <p class="description">
                                <?php
                                echo esc_html(
                                    sprintf(
                                        __('Indexed rows: %1$d | Latest vector update: %2$s | Last query: %3$s | Query count: %4$d', 'nexdirectory'),
                                        (int) ($chatbot_stats['rows'] ?? 0),
                                        (string) ($chatbot_stats['latest_update'] ?: __('Never', 'nexdirectory')),
                                        (string) ($chatbot_stats['last_query_at'] ?: __('Never', 'nexdirectory')),
                                        (int) ($chatbot_stats['query_count'] ?? 0)
                                    )
                                );
                                ?>
                            </p>
                            <?php if (!empty($chatbot_stats['last_summary'])) : ?>
                                <p class="description"><?php echo esc_html(sprintf(__('Last reindex summary: %s', 'nexdirectory'), (string) $chatbot_stats['last_summary'])); ?></p>
                            <?php endif; ?>
                            <p class="description">
                                <?php
                                echo esc_html(
                                    sprintf(
                                        __('Knowledge map: %1$d content types, %2$d topics, %3$d ask-patterns | Updated: %4$s', 'nexdirectory'),
                                        (int) ($chatbot_stats['catalog_types'] ?? 0),
                                        (int) ($chatbot_stats['catalog_topics'] ?? 0),
                                        (int) ($chatbot_stats['catalog_patterns'] ?? 0),
                                        (string) (($chatbot_stats['catalog_updated_at'] ?? '') !== '' ? (string) $chatbot_stats['catalog_updated_at'] : __('Never', 'nexdirectory'))
                                    )
                                );
                                ?>
                            </p>
                            <?php if (!empty($chatbot_stats['catalog_type_labels']) && is_array($chatbot_stats['catalog_type_labels'])) : ?>
                                <p class="description"><?php echo esc_html(sprintf(__('Top indexed content types: %s', 'nexdirectory'), implode(', ', array_map('eep_sanitize_text', (array) $chatbot_stats['catalog_type_labels'])))); ?></p>
                            <?php endif; ?>
                            <?php if (!empty($chatbot_stats['catalog_summary'])) : ?>
                                <p class="description"><?php echo esc_html(sprintf(__('Knowledge summary: %s', 'nexdirectory'), (string) $chatbot_stats['catalog_summary'])); ?></p>
                            <?php endif; ?>
                            <div class="eep-settings-inline" style="margin-top:8px;">
                                <button type="submit" class="button button-secondary" form="eep-chatbot-test-form"><?php esc_html_e('Test OpenAI Connection', 'nexdirectory'); ?></button>
                                <button type="submit" class="button button-secondary" form="eep-chatbot-reindex-form"><?php esc_html_e('Re-index Embeddings Now', 'nexdirectory'); ?></button>
                                <button type="submit" class="button button-secondary" form="eep-chatbot-clear-form" onclick="return confirm('<?php echo esc_js(__('Clear all chatbot embeddings?', 'nexdirectory')); ?>');"><?php esc_html_e('Clear Embedding Index', 'nexdirectory'); ?></button>
                            </div>
                            <p class="description" style="margin-top:8px;"><?php esc_html_e('Shortcode: [eep_event_chatbot] (also available as an Elementor widget).', 'nexdirectory'); ?></p>
                        </div>
                    </div>
                    <?php endif; ?>
                </section>
