<?php
if ( ! defined( 'ABSPATH' ) ) exit;

$def  = SftcyWordaiFrontendAgentChat::wordai_chat_default_settings();
$chat = wp_parse_args( (array) get_option( 'sc_wordai_agent_chat_settings', [] ), $def );
$is_basic_tier = class_exists( 'SftcyWordaiFeatures' ) && SftcyWordaiFeatures::wordai_is_basic_tier();
$upgrade_url   = class_exists( 'SftcyWordaiFeatures' ) ? SftcyWordaiFeatures::wordai_get_upgrade_url() : 'https://softcoy.com/products/wordai-pro/checkout';
$retention_days = $is_basic_tier ? 7 : max( 7, min( 365, absint( $chat['retention_days'] ?? $def['retention_days'] ) ) );

global $wpdb;
$embeddings_table     = $wpdb->prefix . 'wordai_agent_embeddings';
$status_provider = sanitize_key( $chat[ $current_provider . '_embedding_source' ] ?? $current_provider );
if ( ! in_array( $status_provider, [ 'anthropic', 'openai', 'google', 'xai' ], true ) ) {
  $status_provider = $current_provider;
}
$status_provider_label = SftcyWordaiBase::sc_wordai_get_provider_label( $status_provider );
$has_indexed = (int) $wpdb->get_var(
  $wpdb->prepare(
    "SELECT COUNT(DISTINCT source_id) FROM {$embeddings_table} WHERE provider = %s",
    $status_provider
  )
);
?>
<div id="wordai-chat-settings-wrapper">
  <div class="wordai-card">    
     <!-- <h3 style="padding-left: 15px;"><?php //esc_html_e('🤖 AI Chat Settings','wordai-pro'); ?></h3>   -->
    <h3 style="padding-left: 15px; display:flex; align-items:center; gap:10px;">
      <img 
        src="<?php echo esc_url( SFTCY_WORDAI_PLUGIN_URL . 'admin/css/images/avatar-female-chatsettings.svg' ); ?>" 
        alt="<?php esc_attr_e('AI Chat Settings','wordai-pro'); ?>" 
        width="30" height="30"
        style="
          border-radius:50%;
          background:#fef6ff; /* soft lilac background tint for distinct look */
          box-shadow:0 1px 3px rgba(0,0,0,0.2);
          border:1px solid #e0d3f8;
        "
      />
      <?php esc_html_e('AI Chat Settings','wordai-pro'); ?>
    </h3>


    <table class="form-table wa-col-600" role="presentation">
      <tbody>
        <tr>
          <th scope="row"><?php esc_html_e('Enable Chat','wordai-pro'); ?></th>
          <td>
            <label class="wa-fieldset"><input type="checkbox" id="wa-chat-enabled" <?php checked( $chat['enabled'], 1 ); ?>> <?php esc_html_e('Enable site-wide chat bubble','wordai-pro'); ?></label>


              <!-- =======================================================
                  🧠 AI Chat Embedding Status Notice
                  -------------------------------------------------------
                  - Shows a yellow info if no content embedded
                  - Shows a green success message when embedding done
                  - No AJAX or polling for performance
                  ======================================================= -->
              <div id="wa-embedding-status-wrapper">
                <?php if ( $has_indexed > 0 ) : ?>
                  <div class="wordai-embedding-success">
                    <p>                      
                      <?php
                        printf(
                          esc_html__( '✅ Embedded contents are available for %s. Your AI Chat should respond using that website data.', 'wordai-pro' ),
                          esc_html( $status_provider_label )
                        );
                      ?>
                    </p>
                  </div>
                <?php else : ?>
                  <div class="wordai-embedding-warning">
                    <p>
                      <span class="dashicons dashicons-info"></span>
                      <?php
                        printf(
                          esc_html__( 'Your AI Chat is active, but no embedded content is available yet for %s.', 'wordai-pro' ),
                          esc_html( $status_provider_label )
                        );
                      ?>
                      <?php esc_html_e( 'To make the assistant respond accurately using your website data, please run', 'wordai-pro' ); ?>
                      <strong><?php esc_html_e( 'Start Indexing', 'wordai-pro' ); ?></strong>
                      <?php esc_html_e( 'from the', 'wordai-pro' ); ?>
                      <a href="#" id="wa-go-to-embedding"><?php esc_html_e( 'Contents Embedding', 'wordai-pro' ); ?></a>
                      <?php esc_html_e( 'tab.', 'wordai-pro' ); ?>
                    </p>
                  </div>
                <?php endif; ?>
              </div>

          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Assistant Name','wordai-pro'); ?></th>
          <td><input type="text" id="wa-chat-name" class="regular-text" value="<?php echo esc_attr($chat['assistant_name']); ?>"></td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Bubble Label Text','wordai-pro'); ?></th>
          <td><input type="text" id="wa-chat-label" class="regular-text" value="<?php echo esc_attr($chat['bubble_label']); ?>" placeholder="<?php esc_attr_e('Chat','wordai-pro'); ?>"></td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Welcome Message','wordai-pro'); ?></th>
          <td><textarea id="wa-chat-welcome" rows="3"><?php echo esc_textarea($chat['welcome_message']); ?></textarea></td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('System Prompt Prefix','wordai-pro'); ?></th>
          <td><textarea id="wa-chat-prefix" rows="3"><?php echo esc_textarea($chat['prompt_prefix']); ?></textarea></td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('When info not found', 'wordai-pro'); ?></th>
          <td>
            <?php if ( $current_provider === 'google' ) : ?>
              <textarea id="wa-chat-fallback-google" rows="3"><?php echo esc_textarea($chat['fallback_google'] ?? $def['fallback_google']); ?></textarea>
              <p class="description">
                <?php esc_html_e('💡 Shown when the answer is not found in embedded website content (Google Gemini). You may add support email/contact URL.', 'wordai-pro'); ?>
              </p>
            <?php elseif ( $current_provider === 'anthropic' ) : ?>
              <textarea id="wa-chat-fallback-anthropic" rows="3"><?php echo esc_textarea($chat['fallback_anthropic'] ?? $def['fallback_anthropic']); ?></textarea>
              <p class="description">
                <?php esc_html_e('💡 Shown when the answer is not found in embedded website content (Anthropic Claude). You may add support email/contact URL.', 'wordai-pro'); ?>
              </p>
            <?php elseif ( $current_provider === 'xai' ) : ?>
              <textarea id="wa-chat-fallback-xai" rows="3"><?php echo esc_textarea($chat['fallback_xai'] ?? $def['fallback_xai']); ?></textarea>
              <p class="description">
                <?php esc_html_e('💡 Shown when the answer is not found in embedded website content (xAI Grok). You may add support email/contact URL.', 'wordai-pro'); ?>
              </p>
            <?php else : ?>
              <textarea id="wa-chat-fallback-openai" rows="3"><?php echo esc_textarea($chat['fallback_openai'] ?? $def['fallback_openai']); ?></textarea>
              <p class="description">
                <?php esc_html_e('💡 Shown when the answer is not found in embedded website content (OpenAI). You may add support email/contact URL.', 'wordai-pro'); ?>
              </p>
            <?php endif; ?>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Embedded Content Source', 'wordai-pro'); ?></th>
          <td>
            <?php $source_key = $current_provider . '_embedding_source'; ?>
            <fieldset class="wa-fieldset wa-chat-embedding-source-fieldset">
              <label class="wa-chat-embedding-source-option"><input type="radio" name="wa-chat-embedding-source" value="<?php echo esc_attr( $current_provider ); ?>" <?php checked( $chat[ $source_key ] ?? $current_provider, $current_provider ); ?>> <?php echo esc_html( sprintf( __( 'Use %s embedded contents', 'wordai-pro' ), SftcyWordaiBase::sc_wordai_get_provider_label( $current_provider ) ) ); ?></label>
              <?php if ( $current_provider !== 'openai' ) : ?>
                <label class="wa-chat-embedding-source-option"><input type="radio" name="wa-chat-embedding-source" value="openai" <?php checked( $chat[ $source_key ] ?? $current_provider, 'openai' ); ?>> <?php esc_html_e('Use OpenAI embedded contents', 'wordai-pro'); ?></label>
              <?php endif; ?>
              <?php if ( $current_provider !== 'google' ) : ?>
                <label class="wa-chat-embedding-source-option"><input type="radio" name="wa-chat-embedding-source" value="google" <?php checked( $chat[ $source_key ] ?? $current_provider, 'google' ); ?>> <?php esc_html_e('Use Google Gemini embedded contents', 'wordai-pro'); ?></label>
              <?php endif; ?>
              <?php if ( $current_provider !== 'anthropic' ) : ?>
                <label class="wa-chat-embedding-source-option"><input type="radio" name="wa-chat-embedding-source" value="anthropic" <?php checked( $chat[ $source_key ] ?? $current_provider, 'anthropic' ); ?>> <?php esc_html_e('Use Anthropic embedded contents', 'wordai-pro'); ?></label>
              <?php endif; ?>
              <?php if ( $current_provider !== 'xai' ) : ?>
                <label class="wa-chat-embedding-source-option"><input type="radio" name="wa-chat-embedding-source" value="xai" <?php checked( $chat[ $source_key ] ?? $current_provider, 'xai' ); ?>> <?php esc_html_e('Use xAI Grok embedded contents', 'wordai-pro'); ?></label>
              <?php endif; ?>
            </fieldset>
            <p class="description"><?php esc_html_e('Reuse already embedded contents from another AI provider to avoid indexing the same site content again and reduce embedding costs. The selected source provider still needs its embedding/API setup available for chat retrieval.', 'wordai-pro'); ?></p>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Analytics & Feedback', 'wordai-pro'); ?></th>
          <td>
            <fieldset class="wa-fieldset wordai-chat-advanced-fieldset">
              <label><input type="checkbox" id="wa-chat-analytics-enabled" <?php checked( $chat['analytics_enabled'], 1 ); ?>> <?php esc_html_e( 'Record chat analytics and conversation history', 'wordai-pro' ); ?></label>
              <label><input type="checkbox" id="wa-chat-feedback-enabled" <?php checked( $chat['feedback_enabled'], 1 ); ?>> <?php esc_html_e( 'Show helpful / not helpful feedback buttons after AI replies', 'wordai-pro' ); ?></label>
              <label><input type="checkbox" id="wa-chat-source-citations" <?php checked( $chat['show_source_citations'], 1 ); ?> <?php disabled( $is_basic_tier ); ?>> <?php esc_html_e( 'Show source links in front-end chat replies', 'wordai-pro' ); ?></label>
              <label class="wordai-inline-number-field">
                <span><?php esc_html_e( 'Keep analytics logs for', 'wordai-pro' ); ?></span>
                <input type="number" id="wa-chat-retention-days" min="7" max="365" value="<?php echo esc_attr( $retention_days ); ?>" <?php disabled( $is_basic_tier ); ?>>
                <span><?php esc_html_e( 'days', 'wordai-pro' ); ?></span>
                <?php if ( $is_basic_tier ) : ?>
                  <span class="wordai-basic-limit-note"><?php esc_html_e( 'Free preview keeps 7 days', 'wordai-pro' ); ?></span>
                <?php endif; ?>
              </label>
            </fieldset>
            <?php if ( $is_basic_tier ) : ?>
              <p class="description wordai-pro-inline-upsell">
                <?php esc_html_e( 'Free records basic analytics, feedback totals, and admin-only source usage. Upgrade to Pro to show citations to visitors and unlock full analytics history.', 'wordai-pro' ); ?>
                <a href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Upgrade to Pro', 'wordai-pro' ); ?></a>
              </p>
            <?php endif; ?>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Chat Conversation Font Size', 'wordai-pro'); ?></th>
          <td>
            <select id="wa-chat-font-size">
              <?php
                $font_size = max( 13, min( 18, absint( $chat['chat_font_size'] ?? $def['chat_font_size'] ?? 16 ) ) );
                foreach ( [ 13, 14, 15, 16, 17, 18 ] as $size ) {
                  printf(
                    '<option value="%1$d" %2$s>%1$dpx</option>',
                    (int) $size,
                    selected( $font_size, $size, false )
                  );
                }
              ?>
            </select>
            <p class="description"><?php esc_html_e('Controls the visitor question and AI answer text size inside the front-end chat window.', 'wordai-pro'); ?></p>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Human Handoff', 'wordai-pro'); ?></th>
          <td>
            <fieldset class="wa-fieldset wordai-chat-advanced-fieldset <?php echo $is_basic_tier ? esc_attr( 'wordai-pro-locked-fieldset' ) : ''; ?>">
              <label><input type="checkbox" id="wa-chat-handoff-enabled" <?php checked( $chat['handoff_enabled'], 1 ); ?> <?php disabled( $is_basic_tier ); ?>> <?php esc_html_e( 'Enable contact handoff when AI cannot answer', 'wordai-pro' ); ?></label>
              <div class="wordai-chat-handoff-grid">
                <label>
                  <span><?php esc_html_e( 'Handoff email', 'wordai-pro' ); ?></span>
                  <input type="email" id="wa-chat-handoff-email" class="regular-text" value="<?php echo esc_attr( $chat['handoff_email'] ); ?>" <?php disabled( $is_basic_tier ); ?>>
                </label>
                <label>
                  <span><?php esc_html_e( 'Button label', 'wordai-pro' ); ?></span>
                  <input type="text" id="wa-chat-handoff-label" class="regular-text" value="<?php echo esc_attr( $chat['handoff_label'] ); ?>" <?php disabled( $is_basic_tier ); ?>>
                </label>
              </div>
            </fieldset>
            <?php if ( $is_basic_tier ) : ?>
              <p class="description wordai-pro-inline-upsell">
                <?php esc_html_e( 'Human handoff is a Pro feature for sites that want AI chat to become a real support or lead channel.', 'wordai-pro' ); ?>
                <a href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Unlock Handoff', 'wordai-pro' ); ?></a>
              </p>
            <?php else : ?>
              <p class="description"><?php esc_html_e( 'The first release stores the handoff preference for the front-end workflow. Ticket creation and WordForm integration can be connected on top of this setting.', 'wordai-pro' ); ?></p>
            <?php endif; ?>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Visitor Lead Capture', 'wordai-pro'); ?></th>
          <td>
            <div class="wordai-chat-lead-capture-panel">
              <div class="wordai-chat-lead-mode-row">
                <label>
                  <span><?php esc_html_e( 'Capture mode', 'wordai-pro' ); ?></span>
                  <select id="wa-chat-lead-mode">
                    <option value="off" <?php selected( $chat['lead_capture_mode'], 'off' ); ?>><?php esc_html_e( 'Off', 'wordai-pro' ); ?></option>
                    <option value="optional_before_chat" <?php selected( $chat['lead_capture_mode'], 'optional_before_chat' ); ?>><?php esc_html_e( 'Optional form before chat', 'wordai-pro' ); ?></option>
                    <option value="required_before_chat" <?php selected( $chat['lead_capture_mode'], 'required_before_chat' ); ?>><?php esc_html_e( 'Required form before chat', 'wordai-pro' ); ?></option>
                    <option value="after_first_reply" <?php selected( $chat['lead_capture_mode'], 'after_first_reply' ); ?>><?php esc_html_e( 'Ask after first AI reply', 'wordai-pro' ); ?></option>
                    <option value="fallback_handoff" <?php selected( $chat['lead_capture_mode'], 'fallback_handoff' ); ?>><?php esc_html_e( 'Ask on fallback / handoff', 'wordai-pro' ); ?></option>
                  </select>
                </label>
                <p class="description"><?php esc_html_e( 'Best default is Off or Ask after first AI reply. Required mode is available when a site needs contact details before answering.', 'wordai-pro' ); ?></p>
              </div>

              <div class="wordai-chat-lead-copy-grid">
                <label>
                  <span><?php esc_html_e( 'Lead form intro text', 'wordai-pro' ); ?></span>
                  <textarea id="wa-chat-lead-intro" rows="2"><?php echo esc_textarea( $chat['lead_intro_text'] ); ?></textarea>
                </label>
                <label>
                  <span><?php esc_html_e( 'Consent text', 'wordai-pro' ); ?></span>
                  <input type="text" id="wa-chat-lead-consent-text" class="regular-text" value="<?php echo esc_attr( $chat['lead_consent_text'] ); ?>">
                </label>
                <label>
                  <span><?php esc_html_e( 'Privacy policy URL', 'wordai-pro' ); ?></span>
                  <input type="url" id="wa-chat-lead-privacy-url" class="regular-text" value="<?php echo esc_attr( $chat['lead_privacy_url'] ); ?>">
                </label>
              </div>

              <div class="wordai-chat-lead-fields">
                <?php
                $lead_fields = array(
                  'name'    => __( 'Name', 'wordai-pro' ),
                  'email'   => __( 'Email', 'wordai-pro' ),
                  'phone'   => __( 'Phone', 'wordai-pro' ),
                  'company' => __( 'Company / Website', 'wordai-pro' ),
                  'message' => __( 'Interest Message', 'wordai-pro' ),
                );
                foreach ( $lead_fields as $field_key => $field_label ) :
                  $is_pro_only_field = $is_basic_tier && 'name' !== $field_key;
                  ?>
                  <div class="wordai-chat-lead-field-row <?php echo $is_pro_only_field ? esc_attr( 'is-locked' ) : ''; ?>">
                    <strong><?php echo esc_html( $field_label ); ?></strong>
                    <label>
                      <input type="checkbox" id="wa-chat-lead-<?php echo esc_attr( $field_key ); ?>-enabled" <?php checked( $chat[ 'lead_' . $field_key . '_enabled' ], 1 ); ?> <?php disabled( $is_pro_only_field ); ?>>
                      <?php esc_html_e( 'Show', 'wordai-pro' ); ?>
                    </label>
                    <label>
                      <input type="checkbox" id="wa-chat-lead-<?php echo esc_attr( $field_key ); ?>-required" <?php checked( $chat[ 'lead_' . $field_key . '_required' ], 1 ); ?> <?php disabled( $is_pro_only_field ); ?>>
                      <?php esc_html_e( 'Required', 'wordai-pro' ); ?>
                    </label>
                    <?php if ( $is_pro_only_field ) : ?>
                      <span class="wordai-chat-lead-lock"><?php esc_html_e( 'Pro', 'wordai-pro' ); ?></span>
                    <?php endif; ?>
                  </div>
                <?php endforeach; ?>
              </div>

              <fieldset class="wa-fieldset wordai-chat-lead-extra-options">
                <label><input type="checkbox" id="wa-chat-lead-consent-required" <?php checked( $chat['lead_consent_required'], 1 ); ?>> <?php esc_html_e( 'Require consent checkbox when the form is submitted', 'wordai-pro' ); ?></label>
                <label><input type="checkbox" id="wa-chat-lead-notify-admin" <?php checked( $chat['lead_notify_admin'], 1 ); ?> <?php disabled( $is_basic_tier ); ?>> <?php esc_html_e( 'Email site admin when a new lead is captured', 'wordai-pro' ); ?></label>
              </fieldset>

              <?php if ( $is_basic_tier ) : ?>
                <p class="description wordai-pro-inline-upsell">
                  <span class="dashicons dashicons-lock"></span>
                  <?php esc_html_e( 'Free/basic lead capture can collect visitor names. Upgrade to Pro to capture email, phone, company, interest message, notifications, lead status, and CSV export.', 'wordai-pro' ); ?>
                  <a href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Upgrade to Pro', 'wordai-pro' ); ?></a>
                </p>
              <?php endif; ?>
            </div>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Theme','wordai-pro'); ?></th>
          <td>
            <fieldset class="wa-fieldset">
              <label><input type="radio" name="wa-chat-theme" value="light" <?php checked($chat['theme'],'light'); ?>> <?php esc_html_e('Light','wordai-pro'); ?></label>
              <label><input type="radio" name="wa-chat-theme" value="dark"  <?php checked($chat['theme'],'dark'); ?>>  <?php esc_html_e('Dark','wordai-pro'); ?></label>
              <label><input type="radio" name="wa-chat-theme" value="auto"  <?php checked($chat['theme'],'auto'); ?>>  <?php esc_html_e('Auto','wordai-pro'); ?></label>
            </fieldset>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Bubble Color','wordai-pro'); ?></th>
          <td>
              <input type="text" id="wa-chat-color" class="wa-color-field" value="<?php echo esc_attr($chat['bubble_color']); ?>" data-default-color="#0073aa">
              <p class="description">
                  <?php esc_html_e('💡 Main chat accent color used for the bubble, header and buttons.','wordai-pro'); ?>
              </p>
          </td>
        </tr>

        <tr>
          <th scope="row"><?php esc_html_e('Avatar','wordai-pro'); ?></th>
          <td>
            <fieldset class="wa-fieldset">
              <label style="display:inline-flex;align-items:center;gap:6px;margin-right:16px;">
                <input type="radio" name="wa-chat-avatar" value="none" <?php checked($chat['avatar_style'],'none'); ?>>
                <?php esc_html_e('None','wordai-pro'); ?>
              </label>

              <label style="display:inline-flex;align-items:center;gap:6px;margin-right:16px;">
                <input type="radio" name="wa-chat-avatar" value="male" <?php checked($chat['avatar_style'],'male'); ?>>
                <svg width="24" height="24" viewBox="0 0 64 64"><circle cx="32" cy="32" r="32" fill="#d7f0f8"/><circle cx="32" cy="24" r="10" fill="#0e7490"/><path d="M16 50c4-8 12-12 16-12s12 4 16 12" fill="#155e75"/></svg>
                <?php esc_html_e('Male','wordai-pro'); ?>
              </label>

              <label style="display:inline-flex;align-items:center;gap:6px;margin-right:16px;">
                <input type="radio" name="wa-chat-avatar" value="female" <?php checked($chat['avatar_style'],'female'); ?>>
                <svg width="24" height="24" viewBox="0 0 64 64"><circle cx="32" cy="32" r="32" fill="#f3e0ff"/><circle cx="32" cy="24" r="9" fill="#a21caf"/><path d="M14 50c5-9 12-12 18-12s13 3 18 12" fill="#86198f"/><path d="M23 18c3 3 18 3 21 0" stroke="#6b21a8" stroke-width="3" fill="none"/></svg>
                <?php esc_html_e('Female','wordai-pro'); ?>
              </label>

              <label style="display:inline-flex;align-items:center;gap:6px;">
                <input type="radio" name="wa-chat-avatar" value="bot" <?php checked($chat['avatar_style'],'bot'); ?>>
                <svg width="24" height="24" viewBox="0 0 64 64"><rect x="8" y="14" width="48" height="36" rx="10" fill="#d9f3ff"/><circle cx="24" cy="32" r="6" fill="#0f172a"/><circle cx="40" cy="32" r="6" fill="#0f172a"/><rect x="20" y="42" width="24" height="4" rx="2" fill="#0f172a"/><rect x="30" y="6" width="4" height="8" rx="2" fill="#0f172a"/></svg>
                <?php esc_html_e('AI Bot','wordai-pro'); ?>
              </label>
            </fieldset>

            <div style="margin-top:8px;">
              <label for="wa-chat-avatar-color" style="display:inline-block;margin-right:8px;">
                <?php esc_html_e('Avatar Color','wordai-pro'); ?>:
              </label>
              <input type="text" id="wa-chat-avatar-color" class="wa-color-field"
                    value="<?php echo esc_attr($chat['avatar_color']); ?>"
                    data-default-color="#ffffff">
              <p class="description">
                <?php esc_html_e('💡 Pick a contrasting color so the avatar remains visible. If both colors are similar, the system will auto-adjust it.','wordai-pro'); ?>
              </p>
            </div>
          </td>
        </tr>


        <tr>
          <th scope="row"><?php esc_html_e('Model & Temperature','wordai-pro'); ?></th>
          <td>
            <?php
              // $models = SftcyWordaiOpenai::openai_models();
              if ( $current_provider === 'google' && class_exists('SftcyWordaiGoogleGeminiSettings') ) {
                  $models = SftcyWordaiGoogleGeminiSettings::gemini_models(); 
                  $selected_val = SftcyWordaiGoogleGeminiSettings::normalize_gemini_model_slug( $chat['google_model'] ?? '' );
                  $select_id = 'wa-chat-google-model';
              } elseif ( $current_provider === 'anthropic' && class_exists('SftcyWordaiAnthropicSettings') ) {
                  $models = SftcyWordaiAnthropicSettings::anthropic_models();
                  $selected_val = SftcyWordaiAnthropicSettings::normalize_anthropic_model_slug( $chat['anthropic_model'] ?? '' );
                  $select_id = 'wa-chat-anthropic-model';
              } elseif ( $current_provider === 'xai' && class_exists('SftcyWordaiXaiSettings') ) {
                  $models = SftcyWordaiXaiSettings::xai_models();
                  $selected_val = SftcyWordaiXaiSettings::normalize_xai_model_slug( $chat['xai_model'] ?? '' );
                  $select_id = 'wa-chat-xai-model';
              } else {
                  $models = SftcyWordaiOpenai::openai_models();
                  $selected_val = SftcyWordaiOpenai::normalize_openai_model( $chat['model'] ?? '' );
                  $select_id = 'wa-chat-model';
              }
              $model_hints = [];
              if ( $current_provider === 'openai' && class_exists( 'SftcyWordaiOpenai' ) ) {
                  $model_hints = SftcyWordaiOpenai::openai_model_hints();
              } elseif ( $current_provider === 'xai' && class_exists( 'SftcyWordaiXaiSettings' ) ) {
                  $model_hints = SftcyWordaiXaiSettings::xai_model_hints();
              }
            ?>
            <select id="<?php echo esc_attr( $select_id ); ?>">
              <?php
                foreach ( $models as $slug => $label ) {
                  if ( is_array( $label ) ) {
                    echo '<optgroup label="' . esc_attr( $slug ) . '">';
                    foreach ( $label as $group_slug => $group_label ) {
                      printf(
                        '<option value="%1$s" data-model-hint="%4$s" %2$s>%3$s</option>',
                        esc_attr( $group_slug ),
                        selected( $selected_val, $group_slug, false ),
                        esc_html( $group_label ),
                        esc_attr( $model_hints[ $group_slug ] ?? '' )
                      );
                    }
                    echo '</optgroup>';
                    continue;
                  }

                  printf(
                    '<option value="%1$s" data-model-hint="%4$s" %2$s>%3$s</option>',
                    esc_attr( $slug ),
                    selected( $selected_val, $slug, false ),
                    esc_html( $label ),
                    esc_attr( $model_hints[ $slug ] ?? '' )
                  );
                }
              ?>
            </select>
            &nbsp;
            <label><?php esc_html_e('Temperature','wordai-pro'); ?>:
              <input type="number" id="wa-chat-temp" min="0" max="1" step="0.1" value="<?php echo esc_attr($chat['temperature']); ?>" style="width:80px;">
            </label>
            <?php if ( $current_provider === 'openai' && class_exists( 'SftcyWordaiOpenai' ) ) : ?>
              <p id="wa-chat-openai-model-hint" class="description sc-wordai-settings-text-hints">
                <?php echo esc_html( SftcyWordaiOpenai::openai_model_hint( $selected_val ) ); ?>
              </p>
            <?php elseif ( $current_provider === 'xai' && class_exists( 'SftcyWordaiXaiSettings' ) ) : ?>
              <p id="wa-chat-xai-model-hint" class="description sc-wordai-settings-text-hints">
                <?php echo esc_html( SftcyWordaiXaiSettings::xai_model_hint( $selected_val ) ); ?>
              </p>
            <?php endif; ?>
          </td>
        </tr>

        <!--<tr>
          <th scope="row"><?php //esc_html_e('Auto Display','wordai-pro'); ?></th>
          <td>
            <label class="wa-fieldset"><input type="checkbox" id="wa-chat-auto" <?php //checked( $chat['auto_display'], 1 ); ?>> <?php //esc_html_e('Show on all public pages (shortcode also available: [wordai_chatbot])','wordai-pro'); ?></label>
          </td>
        </tr>-->

        <tr>
          <th scope="row"><?php esc_html_e('Position','wordai-pro'); ?></th>
          <td>
            <fieldset class="wa-fieldset">
              <label><input type="radio" name="wa-chat-position" value="bottom-right" <?php checked($chat['position'],'bottom-right'); ?>> <?php esc_html_e('Bottom-Right','wordai-pro'); ?></label>
              <label><input type="radio" name="wa-chat-position" value="bottom-left"  <?php checked($chat['position'],'bottom-left'); ?>>  <?php esc_html_e('Bottom-Left','wordai-pro'); ?></label>
            </fieldset>
          </td>
        </tr>

        <input type="hidden" name="wa-chat-reply" value="nonstream">

        <tr>
          <th scope="row"><?php esc_html_e('Chat Widget Display','wordai-pro'); ?></th>
          <td>
            <fieldset class="wa-fieldset">
              <label><input type="radio" name="wa-chat-visibility" value="public"   <?php checked($chat['visibility'],'public'); ?>>   <?php esc_html_e('Public (all visitors)','wordai-pro'); ?></label>
              <label><input type="radio" name="wa-chat-visibility" value="loggedin" <?php checked($chat['visibility'],'loggedin'); ?>> <?php esc_html_e('Logged-in users only','wordai-pro'); ?></label>
            </fieldset>
          </td>
        </tr>
      </tbody>
    </table>

    <p class="wa-chat-actions">
      <?php wp_nonce_field( 'scwordai_wpnonce', 'scwordai_wpnonce_field' ); ?>
      <button id="wa-chat-save" class="button button-primary">
        <span class="dashicons dashicons-yes-alt"></span>
        <?php esc_html_e('Save Chat Settings','wordai-pro'); ?>
      </button>
      <button id="wa-chat-reset" class="button button-secondary">
        <span class="dashicons dashicons-image-rotate"></span>
        <?php esc_html_e('Reset to Defaults','wordai-pro'); ?>
      </button>
    </p>
  </div><!-- .wordai-card -->

  <input type="hidden" id="wa-active-provider" value="<?php echo esc_attr($current_provider); ?>">

</div>
