<?php
/**
 * Файл: api-settings-blocks.php
 *
 * Этот файл содержит функции для вывода отдельных блоков настроек API:
 * – для текстовых моделей,
 * – для изображений,
 * – для дополнительных (misc) настроек.
 *
 * Файл можно подключать в мастере установки или в других частях плагина.
 */

// Защита от прямого доступа.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Вывод настроек для текстовых моделей.
 */
function render_text_models_settings() {
    // Получаем настройки.
    $api_key            = ai_content_wizard_get_setting( 'ai_content_wizard_api_key', '' );
    $anthropic_api_key  = ai_content_wizard_get_setting( 'ai_content_wizard_anthropic_api_key', '' );
    $deepseek_api_key   = ai_content_wizard_get_setting( 'deepseek_api_key', '' );
    $gemini_api_key     = ai_content_wizard_get_setting( 'ai_content_wizard_gemini_api_key', '' );
    $grok_api_key       = ai_content_wizard_get_setting( 'ai_content_wizard_grok_api_key', '' );
    $text_model         = ai_content_wizard_get_setting( 'ai_content_wizard_text_model', 'gpt-4o-mini' );
    $available_models   = ai_content_wizard_get_setting( 'ai_content_wizard_available_models', array() );
    $fine_tuned_models  = ai_content_wizard_get_setting( 'ai_content_wizard_fine_tuned_models', array() );

    // Lite capabilities — everything else stays visible but locked with CTA.
    $lite_text_providers = array( 'openai' );
    $lite_text_models    = array( 'gpt-4o-mini' );

    $pro_badge = sprintf(
        '<span class="aiw-lock-badge"><span class="dashicons dashicons-lock"></span>%s</span>',
        esc_html__( 'Pro', 'ai-content-wizard' )
    );
    $pro_note = sprintf(
        '<span class="aiw-provider-note">%s</span>',
        esc_html__( 'Available in AI Content Wizard Pro', 'ai-content-wizard' )
    );
    $upgrade_cta = sprintf(
        '<a class="aiw-upgrade-cta" href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
        esc_url( 'https://aiwizard.buyreadysite.com/' ),
        esc_html__( 'Unlock in Pro', 'ai-content-wizard' )
    );

    $text_provider_locked = array(
        'openai'    => ! in_array( 'openai', $lite_text_providers, true ),
        'anthropic' => ! in_array( 'anthropic', $lite_text_providers, true ),
        'deepseek'  => ! in_array( 'deepseek', $lite_text_providers, true ),
        'gemini'    => ! in_array( 'gemini', $lite_text_providers, true ),
        'grok'      => ! in_array( 'grok', $lite_text_providers, true ),
    );

    $text_provider_labels = array(
        'openai'    => __( 'OpenAI', 'ai-content-wizard' ),
        'anthropic' => __( 'Claude (Anthropic)', 'ai-content-wizard' ),
        'deepseek'  => __( 'DeepSeek', 'ai-content-wizard' ),
        'gemini'    => __( 'Google Gemini', 'ai-content-wizard' ),
        'grok'      => __( 'xAI Grok', 'ai-content-wizard' ),
    );

    $openai_regular_models = array(
        'gpt-5.1'        => 'GPT-5.1',
        'gpt-5-mini'     => 'GPT-5 Mini',
        'gpt-4.5-preview'=> 'GPT-4.5-preview',
        'gpt-4.1'        => 'GPT‑4.1',
        'gpt-4.1-mini'   => 'GPT‑4.1 Mini',
        'gpt-4.1-nano'   => 'GPT‑4.1 Nano',
        'gpt-4o'         => 'GPT‑4o',
        'gpt-4o-mini'    => 'GPT‑4o Mini'
    );

    $openai_reasoning_models = array(
        'o1'      => 'O1',
        'o1-pro'  => 'O1 Pro',
        'o1-mini' => 'O1 Mini',
        'o3'      => 'O3',
        'o3-mini' => 'O3 Mini',
        'o4-mini' => 'O4 Mini',
        'gpt-5'   => 'GPT-5',
    );

    $anthropic_default = array(
        'claude-4.5-sonnet-latest' => 'Claude Sonnet 4.5',
        'claude-4-sonnet-latest'   => 'Claude Sonnet 4',
        'claude-4.5-haiku-latest'  => 'Claude Haiku 4.5',
        'claude-4.1-opus-latest'   => 'Claude Opus 4.1',
        'claude-4-opus-latest'     => 'Claude Opus 4',
        'claude-3-7-sonnet-20250219' => 'Claude 3.7 Sonnet',
        'claude-3-5-haiku-20241022'  => 'Claude 3.5 Haiku',
        'claude-3-5-sonnet-20241022' => 'Claude 3.5 Sonnet',
        'claude-3-opus-20240229'     => 'Claude 3 Opus',
        'claude-3-haiku-20240307'    => 'Claude 3 Haiku'
    );

    $deepseek_default_models = array(
        'deepseek-chat'     => 'DeepSeek V3 (Chat)',
        'deepseek-reasoner' => 'DeepSeek R1 (Reasoner)'
    );

    $gemini_all_models = array(
           'gemini-3-pro-preview' => 'Gemini 3 Pro Preview',
        'gemini-2.5-pro'       => 'Gemini 2.5 Pro',
        'gemini-2.5-flash'     => 'Gemini 2.5 Flash',
        'gemini-2.0-flash'     => 'Gemini 2.0 Flash',
        'gemini-2.0-flash-lite' => 'Gemini 2.0 Flash-Lite'
    );

    $grok4_models = array(
        'grok-4-fast-reasoning'     => 'Grok 4 Fast Reasoning',
        'grok-4-fast-non-reasoning' => 'Grok 4 Fast Non-Reasoning',
        'grok-code-fast-1'          => 'Grok Code Fast 1',
        'grok-4'                    => 'Grok 4'
    );

    $grok3_models = array(
        'grok-3'      => 'Grok 3',
        'grok-3-mini' => 'Grok 3 Mini'
    );

    $text_model_labels = array_merge(
        $openai_regular_models,
        $openai_reasoning_models,
        $anthropic_default,
        $deepseek_default_models,
        $gemini_all_models,
        $grok4_models,
        $grok3_models
    );

    if ( ! in_array( $text_model, $lite_text_models, true ) ) {
        $text_model = 'gpt-4o-mini';
        ai_content_wizard_update_setting( 'ai_content_wizard_text_model', $text_model );
    }
    
    // Определяем текущего провайдера для текстовых моделей.
    $current_provider = 'openai';
    if ( strpos( strtolower( $text_model ), 'gpt' ) !== false ) {
        $current_provider = 'openai';
    } elseif ( strpos( strtolower( $text_model ), 'claude' ) !== false ) {
        $current_provider = 'anthropic';
    } elseif ( strpos( strtolower( $text_model ), 'deepseek' ) !== false ) {
        $current_provider = 'deepseek';
    } elseif ( strpos( strtolower( $text_model ), 'gemini' ) !== false ) {
        $current_provider = 'gemini';
    } elseif ( strpos( strtolower( $text_model ), 'grok' ) !== false ) {
        $current_provider = 'grok';
    }

    $active_text_model_label    = $text_model_labels[ $text_model ] ?? strtoupper( $text_model );
    $active_text_provider_label = $text_provider_labels[ $current_provider ] ?? ucfirst( $current_provider );

    // --- Web search capable models ---
    $web_search_capable = ( $current_provider === 'openai' ) && in_array(
        $text_model,
        array(
            'gpt-4.1',
            'gpt-4o',
            'gpt-4o-search-preview',
            'gpt-4.1-mini',
            'gpt-4o-mini',
            'gpt-4o-mini-search-preview',
            'gpt-5',
            'gpt-5-mini',
            'gpt-5.1'
        ),
        true
    );

    // Массив стоимости из calculate_text_generation_cost
    $costs = [
        // --- GPT-5 lineup ---
        'gpt-5.1'   => ['input' => 1.25 / 1000000, 'output' => 10.00 / 1000000],
        'gpt-5'      => ['input' => 1.25 / 1000000, 'output' => 10.00 / 1000000],
        'gpt-5-mini' => ['input' => 0.25 / 1000000, 'output' => 2.00  / 1000000],
        // --- OpenAI GPT-4/4o/4.1 ---
        'gpt-4.5-preview'      => ['input' => 75.00 / 1000000, 'output' => 150.00 / 1000000],
        'gpt-4o'               => ['input' => 2.50 / 1000000, 'output' => 10.00 / 1000000],
        'gpt-4o-mini'          => ['input' => 0.15 / 1000000, 'output' => 0.60 / 1000000],
        // --- NEW GPT‑4.1 LINEUP ---
        'gpt-4.1'              => ['input' => 2.00 / 1000000,  'output' => 8.00 / 1000000],
        'gpt-4.1-mini'         => ['input' => 0.40 / 1000000, 'output' => 1.60 / 1000000],
        'gpt-4.1-nano'         => ['input' => 0.10 / 1000000, 'output' => 0.40 / 1000000],
        'gpt-4o1-preview'      => ['input' => 15 / 1000000, 'output' => 60 / 1000000],
        'gpt-4o1-mini'         => ['input' => 3 / 1000000, 'output' => 12 / 1000000],
        'gpt-4o-audio-preview' => ['input' => 2.50 / 1000000, 'output' => 10.00 / 1000000],
        'gpt-4o-realtime-preview' => ['input' => 5.00 / 1000000, 'output' => 20.00 / 1000000],
        'gpt-4o-mini-audio-preview' => ['input' => 0.15 / 1000000, 'output' => 0.60 / 1000000],
        'gpt-4o-mini-realtime-preview' => ['input' => 0.60 / 1000000, 'output' => 2.40 / 1000000],
        // --- Reasoning Models ---
        'o1' => ['input' => 15.00 / 1000000, 'output' => 60.00 / 1000000],
        'o1-preview' => ['input' => 15.00 / 1000000, 'output' => 60.00 / 1000000],
        'o3'      => ['input' => 2.00 / 1000000, 'output' => 8.00 / 1000000],
        'o3-mini' => ['input' => 1.10 / 1000000, 'output' => 4.40 / 1000000],
        'o1-mini' => ['input' => 1.10 / 1000000, 'output' => 4.40 / 1000000],
        'o4-mini' => ['input' => 1.10 / 1000000, 'output' => 4.40 / 1000000],
        // --- Anthropic ---
        // New 4.x/4.5 lineup (latest aliases)
        'claude-4.5-sonnet-latest' => ['input' => 3.00 / 1000000,  'output' => 15.00 / 1000000],
        'claude-4-sonnet-latest'   => ['input' => 3.00 / 1000000,  'output' => 15.00 / 1000000],
        'claude-4.5-haiku-latest'  => ['input' => 1.00 / 1000000,  'output' => 5.00  / 1000000],
        'claude-4.1-opus-latest'   => ['input' => 15.00 / 1000000, 'output' => 75.00 / 1000000],
        'claude-4-opus-latest'     => ['input' => 15.00 / 1000000, 'output' => 75.00 / 1000000],
        // Existing 3.x lineup
        'claude-3-7-sonnet-20250219' => ['input' => 3.00 / 1000000, 'output' => 15.00 / 1000000],
        'claude-3-5-sonnet-20241022' => ['input' => 3.00 / 1000000, 'output' => 15.00 / 1000000],
        'claude-3-5-haiku-20241022'  => ['input' => 0.80 / 1000000, 'output' => 4.00 / 1000000],
        'claude-3-opus-20240229'     => ['input' => 15.00 / 1000000, 'output' => 75.00 / 1000000],
        'claude-3-haiku-20240307'    => ['input' => 0.25 / 1000000, 'output' => 1.25 / 1000000],
        // --- DeepSeek ---
        // Conservative cache-miss input pricing; cache-hit можно учесть отдельно в калькуляторе
        'deepseek-chat' => ['input' => 0.28 / 1000000, 'output' => 0.42 / 1000000],
        'deepseek-reasoner' => ['input' => 0.28 / 1000000, 'output' => 0.42 / 1000000],
        // --- Google Gemini (Updated November 2025) ---
        // Gemini 3.0 (Preview - most advanced)
        'gemini-3-pro-preview' => ['input' => 2.00 / 1000000, 'output' => 12.00 / 1000000],
        // Gemini 2.5 (Advanced)
        'gemini-2.5-pro' => ['input' => 1.25 / 1000000, 'output' => 10.00 / 1000000],
        'gemini-2.5-flash' => ['input' => 0.30 / 1000000, 'output' => 2.50 / 1000000],
        // Gemini 2.0 (Stable)
        'gemini-2.0-flash' => ['input' => 0.10 / 1000000, 'output' => 0.40 / 1000000],
        'gemini-2.0-flash-lite' => ['input' => 0.075 / 1000000, 'output' => 0.30 / 1000000],
        // --- xAI Grok (Updated November 2025) ---
        'grok-4' => ['input' => 3.00 / 1000000, 'output' => 15.00 / 1000000],
        'grok-4-fast-reasoning' => ['input' => 0.20 / 1000000, 'output' => 0.50 / 1000000],
        'grok-4-fast-non-reasoning' => ['input' => 0.20 / 1000000, 'output' => 0.50 / 1000000],
        'grok-code-fast-1' => ['input' => 0.20 / 1000000, 'output' => 1.50 / 1000000],
        'grok-3' => ['input' => 3.00 / 1000000, 'output' => 15.00 / 1000000],
        'grok-3-mini' => ['input' => 0.30 / 1000000, 'output' => 0.50 / 1000000],
    ];

    // Функция форматирования стоимости для текстовых моделей
    function format_text_cost($model, $costs) {
        if (isset($costs[$model])) {
            $input_cost = $costs[$model]['input'] * 1000000;  // Стоимость за 1M входящих токенов
            $output_cost = $costs[$model]['output'] * 1000000; // Стоимость за 1M исходящих токенов
            $article_cost = calculate_article_cost($model, $costs); // Расчет стоимости статьи
            return '$' . number_format($input_cost, 2) . " per 1M input tokens\n$" . number_format($output_cost, 2) . " per 1M output tokens\n**Estimated cost for a 2000-word article: $" . number_format($article_cost, 3) . "**";
        }
        return "Cost information not available";
    }

    // Расчет стоимости статьи на 2000 слов
    function calculate_article_cost($model, $costs) {
        $words = 2000;
        $tokens_per_word = 1.5; // Средний коэффициент
        $total_tokens = $words * $tokens_per_word; // 3000 токенов
        $input_tokens = $total_tokens / 2; // 1500 токенов
        $output_tokens = $total_tokens / 2; // 1500 токенов
        $input_cost = $costs[$model]['input'] * $input_tokens; // Стоимость входящих
        $output_cost = $costs[$model]['output'] * $output_tokens; // Стоимость исходящих
        return $input_cost + $output_cost;
    }
    ?>
    <!-- Блок настроек текстовых моделей -->
    <div id="api-settings-text-models">
        <fieldset class="api-legend">
            <legend>
                <?php _e( 'Select Text Model Provider', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpTextProviders')"></span>
            </legend>
            <div class="aiw-radio-group" style="display: flex; gap: 12px; justify-content: center; padding: 10px 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px 0; flex-wrap: wrap;">
                <input type="radio" id="provider_openai" name="text_provider" value="openai" data-label="<?php echo esc_attr( $text_provider_labels['openai'] ); ?>" <?php checked($current_provider, 'openai'); ?> style="display: none;" <?php echo $text_provider_locked['openai'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $text_provider_locked['openai'] ? 'aiw-locked' : ''; ?>" for="provider_openai" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #10a37f; border-radius: 8px; background: linear-gradient(135deg, #f0f8ff, #e6f0ff); color: #10a37f; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(16, 163, 127, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #10a37f;">
                        <path d="M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.872zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997Z"/>
                    </svg>
                    OpenAI
                    <?php echo $text_provider_locked['openai'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_anthropic" name="text_provider" value="anthropic" data-label="<?php echo esc_attr( $text_provider_labels['anthropic'] ); ?>" <?php checked($current_provider, 'anthropic'); ?> style="display: none;" <?php echo $text_provider_locked['anthropic'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $text_provider_locked['anthropic'] ? 'aiw-locked' : ''; ?>" for="provider_anthropic" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #9b30ff; border-radius: 8px; background: linear-gradient(135deg, #fff5f5, #ffe6e6); color: #9b30ff; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(155, 48, 255, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg width="20" height="20" viewBox="0 0 24 24" style="margin-right: 8px; fill: #9b30ff;">
                        <path d="M12 1.5a10.482 10.482 0 0 0-6.875 2.55 1.875 1.875 0 0 0 2.452 2.83A6.752 6.752 0 0 1 18.75 12a6.752 6.752 0 0 1-11.173 5.12 1.875 1.875 0 1 0-2.452 2.83A10.5 10.5 0 1 0 12 1.5Z"/>
                    </svg>
                    Claude (Anthropic)
                    <?php echo $text_provider_locked['anthropic'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_deepseek" name="text_provider" value="deepseek" data-label="<?php echo esc_attr( $text_provider_labels['deepseek'] ); ?>" <?php checked($current_provider, 'deepseek'); ?> style="display: none;" <?php echo $text_provider_locked['deepseek'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $text_provider_locked['deepseek'] ? 'aiw-locked' : ''; ?>" for="provider_deepseek" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #0066ff; border-radius: 8px; background: linear-gradient(135deg, #f5fff5, #e6ffe6); color: #0066ff; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(0, 102, 255, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #0066ff;">
                        <path d="M5.617 14.598 3.648 16.63l1.387 1.366 3.598-3.56a6.835 6.835 0 0 1-2.002-1.825l-1.014 1.987zm16.32-5.578-3.599 3.559a6.868 6.868 0 0 1-1.981 3.862l1.01-1.99 1.968-2.033-1.387-1.365-3.598 3.56a6.893 6.893 0 0 1-2.916 1.174v-2.838h-1.958v2.851a6.893 6.893 0 0 1-2.932-1.2l3.597-3.546a6.952 6.952 0 0 1 0-9.811 6.9 6.9 0 0 1 4.876-2.028 6.9 6.9 0 0 1 4.875 2.028 6.958 6.958 0 0 1 2.045 4.919 6.952 6.952 0 0 1 0 .868zm-7.475 9.895c4.25 0 7.697-3.453 7.697-7.71 0-4.257-3.447-7.71-7.697-7.71-4.25 0-7.697 3.453-7.697 7.71 0 4.257 3.447 7.71 7.697 7.71z"/>
                    </svg>
                    DeepSeek
                    <?php echo $text_provider_locked['deepseek'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_gemini" name="text_provider" value="gemini" data-label="<?php echo esc_attr( $text_provider_labels['gemini'] ); ?>" <?php checked($current_provider, 'gemini'); ?> style="display: none;" <?php echo $text_provider_locked['gemini'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $text_provider_locked['gemini'] ? 'aiw-locked' : ''; ?>" for="provider_gemini" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #4285f4; border-radius: 8px; background: linear-gradient(135deg, #e8f4ff, #d0e7ff); color: #4285f4; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(66, 133, 244, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #4285f4;">
                        <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"/>
                    </svg>
                    Google Gemini
                    <?php echo $text_provider_locked['gemini'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_grok" name="text_provider" value="grok" data-label="<?php echo esc_attr( $text_provider_labels['grok'] ); ?>" <?php checked($current_provider, 'grok'); ?> style="display: none;" <?php echo $text_provider_locked['grok'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $text_provider_locked['grok'] ? 'aiw-locked' : ''; ?>" for="provider_grok" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #1da1f2; border-radius: 8px; background: linear-gradient(135deg, #e6f9ff, #ccf2ff); color: #1da1f2; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(29, 161, 242, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #1da1f2;">
                        <path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/>
                    </svg>
                    xAI Grok
                    <?php echo $text_provider_locked['grok'] ? $pro_badge . $pro_note : ''; ?>
                </label>
            </div>
        </fieldset>

        <div class="aiw-active-selection aiw-active-text-model">
            <span class="aiw-active-selection__label"><?php esc_html_e( 'Active text model', 'ai-content-wizard' ); ?></span>
            <div class="aiw-active-selection__value" id="aiw-active-text-model-name"><?php echo esc_html( $active_text_model_label ); ?></div>
            <p class="aiw-active-selection__meta">
                <?php esc_html_e( 'Provider:', 'ai-content-wizard' ); ?>
                <span id="aiw-active-text-provider"><?php echo esc_html( $active_text_provider_label ); ?></span>
            </p>
        </div>

        <div class="aiw-upgrade-hint">
            <p><?php esc_html_e( 'Need the full list of AI providers and newest models? Upgrade to Pro to unlock everything inside Lite.', 'ai-content-wizard' ); ?></p>
            <?php echo $upgrade_cta; ?>
        </div>
        
        <!-- Поля API Key для текстовых провайдеров -->
        <fieldset class="api-legend" id="openai_key_block" style="display:none;">
            <legend><?php _e( 'OpenAI API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpOpenAIKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_api_key"><?php _e( 'OpenAI API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display:flex; align-items:center; width:100%;">
                            <div style="position:relative; flex-grow:1;">
                                <input type="password" id="ai_content_wizard_api_key" name="ai_content_wizard_api_key" value="<?php echo esc_attr( $api_key ); ?>" style="width:100%; padding-right:30px;">
                                <span class="dashicons dashicons-visibility" style="position:absolute; right:8px; top:50%; transform:translateY(-50%); cursor:pointer;" onclick="toggleVisibility('ai_content_wizard_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_api_key" name="reset_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left:10px;" onclick="resetField('ai_content_wizard_api_key','reset_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
            <div style="margin-top:15px;" id="openai_models_button_block">
                <button type="submit" name="aiw_update_openai_models" class="aiw-models-button"><?php _e( 'Show available models', 'ai-content-wizard' ); ?></button>
            </div>
        </fieldset>
        
        <fieldset class="api-legend" id="anthropic_key_block" style="display:none;">
            <legend><?php _e( 'Anthropic API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpAnthropicKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_anthropic_api_key"><?php _e( 'Anthropic API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display:flex; align-items:center; width:100%;">
                            <div style="position:relative; flex-grow:1;">
                                <input type="password" id="ai_content_wizard_anthropic_api_key" name="ai_content_wizard_anthropic_api_key" value="<?php echo esc_attr( $anthropic_api_key ); ?>" style="width:100%; padding-right:30px;">
                                <span class="dashicons dashicons-visibility" style="position:absolute; right:8px; top:50%; transform:translateY(-50%); cursor:pointer;" onclick="toggleVisibility('ai_content_wizard_anthropic_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_anthropic_api_key" name="reset_anthropic_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left:10px;" onclick="resetField('ai_content_wizard_anthropic_api_key','reset_anthropic_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
        
        <fieldset class="api-legend" id="deepseek_key_block" style="display:none;">
            <legend><?php _e( 'DeepSeek API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpDeepSeekKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="deepseek_api_key"><?php _e( 'DeepSeek API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display:flex; align-items:center; width:100%;">
                            <div style="position:relative; flex-grow:1;">
                                <input type="password" id="deepseek_api_key" name="deepseek_api_key" value="<?php echo esc_attr( $deepseek_api_key ); ?>" style="width:100%; padding-right:30px;">
                                <span class="dashicons dashicons-visibility" style="position:absolute; right:8px; top:50%; transform:translateY(-50%); cursor:pointer;" onclick="toggleVisibility('deepseek_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_deepseek_api_key" name="reset_deepseek_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left:10px;" onclick="resetField('deepseek_api_key','reset_deepseek_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
        
        <fieldset class="api-legend" id="gemini_key_block" style="display:none;">
            <legend><?php _e( 'Google Gemini API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpGeminiKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_gemini_api_key"><?php _e( 'Google Gemini API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display:flex; align-items:center; width:100%;">
                            <div style="position:relative; flex-grow:1;">
                                <input type="password" id="ai_content_wizard_gemini_api_key" name="ai_content_wizard_gemini_api_key" value="<?php echo esc_attr( $gemini_api_key ); ?>" style="width:100%; padding-right:30px;">
                                <span class="dashicons dashicons-visibility" style="position:absolute; right:8px; top:50%; transform:translateY(-50%); cursor:pointer;" onclick="toggleVisibility('ai_content_wizard_gemini_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_gemini_api_key" name="reset_gemini_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left:10px;" onclick="resetField('ai_content_wizard_gemini_api_key','reset_gemini_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
        
        <fieldset class="api-legend" id="grok_key_block" style="display:none;">
            <legend><?php _e( 'xAI Grok API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpGrokKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_grok_api_key"><?php _e( 'xAI Grok API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display:flex; align-items:center; width:100%;">
                            <div style="position:relative; flex-grow:1;">
                                <input type="password" id="ai_content_wizard_grok_api_key" name="ai_content_wizard_grok_api_key" value="<?php echo esc_attr( $grok_api_key ); ?>" style="width:100%; padding-right:30px;">
                                <span class="dashicons dashicons-visibility" style="position:absolute; right:8px; top:50%; transform:translateY(-50%); cursor:pointer;" onclick="toggleVisibility('ai_content_wizard_grok_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_grok_api_key" name="reset_grok_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left:10px;" onclick="resetField('ai_content_wizard_grok_api_key','reset_grok_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
        
        <!-- ------------------ Text Models Container ------------------ -->
        <fieldset class="api-legend">
            <legend>
                <?php _e( 'Select Text Model', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpTextModels')"></span>
            </legend>
            <div class="aiw-radio-group" id="text-models-radio-group">
                <!-- Контейнер моделей для OpenAI -->
                <div id="openai_models_container" style="display:none;">
                    <?php
                    echo '<div class="model-group">';
                    echo '<div class="model-group-title">' . __( 'Regular Models', 'ai-content-wizard' ) . '</div>';
                    foreach ( $openai_regular_models as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $class   = ! empty( $available_models ) ? ( in_array( $key, $available_models ) ? '' : 'unavailable' ) : '';
                        $locked  = ! in_array( $key, $lite_text_models, true );
                        $cost_info = format_text_cost($key, $costs);
                        $label_text = $label;
                        $label_markup = $label;
                        if ( $key === 'gpt-5-mini' ) {
                            $label_markup .= ' <span class="badge-recommended">' . __( 'Recommended', 'ai-content-wizard' ) . '</span>';
                        }
                        $label_markup .= $locked ? $pro_badge : '';
                        $label_classes = trim( $class . ' ' . ( $locked ? 'aiw-locked' : '' ) );
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label_text ) . '" ' . $checked . ( $locked ? ' disabled' : '' ) . '>';
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" class="' . esc_attr( $label_classes ) . '" title="' . esc_attr( $cost_info ) . '">' . wp_kses( $label_markup, array( 'span' => array( 'class' => array() ) ) ) . '</label>';
                    }
                    echo '</div>';
                    echo '<div class="model-group">';
                    echo '<div class="model-group-title">' . __( 'Reasoning Models', 'ai-content-wizard' ) . '</div>';
                    foreach ( $openai_reasoning_models as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $class   = ! empty( $available_models ) ? ( in_array( $key, $available_models ) ? '' : 'unavailable' ) : '';
                        $cost_info = format_text_cost($key, $costs);
                        $label_text = $label;
                        $label_markup = $label . $pro_badge;
                        $label_classes = trim( $class . ' aiw-locked' );
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label_text ) . '" ' . $checked . ' disabled>';
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" class="' . esc_attr( $label_classes ) . '" title="' . esc_attr( $cost_info ) . '">' . wp_kses( $label_markup, array( 'span' => array( 'class' => array() ) ) ) . '</label>';
                    }
                    echo '</div>';
                    // Insert the web search toggle as a model-group here
                    ?>
                    <div class="model-group" id="web_search_toggle" style="display:none;">
                        <div class="model-group-title"><?php _e( 'Web Search', 'ai-content-wizard' ); ?></div>
                        <label for="ai_content_wizard_web_search">
                            <input type="checkbox" id="ai_content_wizard_web_search" name="ai_content_wizard_web_search" value="1" <?php checked( ai_content_wizard_get_setting( 'ai_content_wizard_web_search', 0 ), 1 ); ?> />
                            <?php _e( 'Enable internet search for this model. Additional costs apply.', 'ai-content-wizard' ); ?>
                        </label>
                    </div>
                </div>
        
                <!-- Контейнер моделей для Anthropic -->
                <div id="anthropic_models_container" style="display:none;">
                    <?php
                    foreach ( $anthropic_default as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $cost_info = format_text_cost($key, $costs);
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . wp_kses( $label, array( "span" => array( "class" => array() ) ) ) . '</label>';
                    }
                    ?>
                </div>
        
                <!-- Контейнер моделей для DeepSeek -->
                <div id="deepseek_models_container" style="display:none;">
                    <?php
                    foreach ( $deepseek_default_models as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $cost_info = format_text_cost($key, $costs);
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    ?>
                </div>

                <!-- Контейнер моделей для Google Gemini -->
                <div id="gemini_models_container" style="display:none;">
                    <?php
                    echo '<div class="model-group">';
                    echo '<div class="model-group-title">' . __( 'Google Gemini Models', 'ai-content-wizard' ) . '</div>';
                    foreach ( $gemini_all_models as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $cost_info = format_text_cost($key, $costs);
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    echo '</div>';
                    ?>
                </div>

                <!-- Контейнер моделей для xAI Grok -->
                <div id="grok_models_container" style="display:none;">
                    <?php
                    echo '<div class="model-group">';
                    echo '<div class="model-group-title">' . __( 'Grok 4 Generation', 'ai-content-wizard' ) . '</div>';
                    foreach ( $grok4_models as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $cost_info = format_text_cost($key, $costs);
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        $label_output = $label;
                        if ( in_array( $key, ['grok-4-fast-reasoning', 'grok-4-fast-non-reasoning', 'grok-code-fast-1'] ) ) {
                            $label_output .= ' <span class="badge-new">' . __( 'New', 'ai-content-wizard' ) . '</span>';
                        }
                        if ( $key === 'grok-4-fast-reasoning' ) {
                            $label_output .= ' <span class="badge-recommended">' . __( 'Recommended', 'ai-content-wizard' ) . '</span>';
                        }
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . wp_kses( $label_output, array( "span" => array( "class" => array() ) ) ) . '</label>';
                    }
                    echo '</div>';
                    echo '<div class="model-group">';
                    echo '<div class="model-group-title">' . __( 'Grok 3 Generation', 'ai-content-wizard' ) . '</div>';
                    foreach ( $grok3_models as $key => $label ) {
                        $checked = ( $text_model === $key ) ? 'checked' : '';
                        $cost_info = format_text_cost($key, $costs);
                        echo '<input type="radio" id="txt_model_' . esc_attr( $key ) . '" name="text_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="txt_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    echo '</div>';
                    ?>
                </div>
            </div>
            <input type="hidden" id="ai_content_wizard_text_model" name="ai_content_wizard_text_model" value="<?php echo esc_attr( $text_model ); ?>">
        </fieldset>
        
        <!-- Fine-Tuned Models – only for OpenAI -->
        <div id="fine_tuned_container" style="display:none;">
            <fieldset class="api-legend">
                <legend><?php _e( 'Fine-Tuned Models', 'ai-content-wizard' ); ?></legend>
                <?php
                $has_ftjob_models = false;
                foreach ( $fine_tuned_models as $model ) {
                    if ( strpos( $model, 'ftjob-' ) === 0 ) {
                        $has_ftjob_models = true;
                        break;
                    }
                }
                ?>
                <table class="form-table">
                    <tr>
                        <th><label for="ai_content_wizard_fine_tuned_model"><?php _e( 'Fine-Tuned Model', 'ai-content-wizard' ); ?></label></th>
                        <td>
                            <select id="ai_content_wizard_fine_tuned_model" class="regular-text">
                                <?php if ( ! $has_ftjob_models ) : ?>
                                    <option value=""><?php _e( 'No fine-tuned models available', 'ai-content-wizard' ); ?></option>
                                <?php else : ?>
                                    <optgroup label="<?php _e( 'Custom Fine-Tuned Models', 'ai-content-wizard' ); ?>">
                                        <?php
                                        foreach ( $fine_tuned_models as $model ) {
                                            if ( strpos( $model, 'ftjob-' ) === 0 ) {
                                                echo '<option value="' . esc_attr( $model ) . '">' . wp_kses( $model, array( "span" => array( "class" => array() ) ) ) . '</option>';
                                            }
                                        }
                                        ?>
                                    </optgroup>
                                <?php endif; ?>
                            </select>
                            <?php if ( $has_ftjob_models ) : ?>
                                <input type="text" id="new_model_name" placeholder="<?php _e( 'Format: ft:gpt-3.5-turbo...', 'ai-content-wizard' ); ?>" class="regular-text" style="margin-top:10px; width:100%;">
                                <button id="rename_model" class="button" style="margin-top:10px;"><?php _e( 'Rename', 'ai-content-wizard' ); ?></button>
                            <?php endif; ?>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>

        <script type="text/javascript">
jQuery(function ($) {
    const searchModels = [
        'gpt-4.1',
        'gpt-4o',
        'gpt-4o-search-preview',
        'gpt-4.1-mini',
        'gpt-4o-mini',
        'gpt-4o-mini-search-preview',
        'gpt-5',
        'gpt-5-mini',
        'gpt-5.1'       
    ];

    function updateWebSearchToggle() {
        const provider = $('input[name="text_provider"]:checked').val();
        const model    = $('input[name="text_model_selector"]:checked').val();

        if (provider === 'openai' && searchModels.includes(model)) {
            $('#web_search_toggle').show();
        } else {
            $('#web_search_toggle').hide();
            $('#ai_content_wizard_web_search').prop('checked', false);
        }
    }

    $(document).on('change', 'input[name="text_provider"], input[name="text_model_selector"]', updateWebSearchToggle);
    updateWebSearchToggle(); // initial call
});
</script>
    </div>
    <?php
}


/**
 * Вывод настроек для моделей изображений.
 */
function render_image_models_settings() {
    // Получаем настройки.
    $deepai_api_key          = ai_content_wizard_get_setting( 'ai_content_wizard_deepai_api_key', '' );
    $stable_diffusion_api_key = ai_content_wizard_get_setting( 'ai_content_wizard_stable_diffusion_api_key', '' );
    $ideogram_api_key        = ai_content_wizard_get_setting( 'ideogram_api_key', '' );
    $gemini_api_key          = ai_content_wizard_get_setting( 'ai_content_wizard_gemini_api_key', '' );
    $image_model             = ai_content_wizard_get_setting( 'ai_content_wizard_image_model', 'dall-e-3' );

    $image_provider_labels = array(
        'dalle'           => __( 'DALL‑E (OpenAI)', 'ai-content-wizard' ),
        'deepai'          => __( 'DeepAI', 'ai-content-wizard' ),
        'stablediffusion' => __( 'Stable Diffusion', 'ai-content-wizard' ),
        'ideogram'        => __( 'Ideogram', 'ai-content-wizard' ),
        'gemini'          => __( 'Gemini / Imagen', 'ai-content-wizard' ),
    );

    $dalle_models = array(
        'dall-e-2'    => 'DALL‑E 2',
        'dall-e-3'    => 'DALL‑E 3',
        'gpt-image-1' => 'GPT‑Image‑1',
    );

    $deepai_models = array(
        'deepai-standard' => 'DeepAI Standard',
        'deepai-hd'       => 'DeepAI HD',
    );

    $stable_diffusion_models = array(
        'sdxl-1-0'             => 'SDXL 1.0',
        'stable-diffusion-3'   => 'Stable Diffusion 3',
        'stable-diffusion-ultra'=> 'Stable Diffusion Ultra',
    );

    $ideogram_models = array(
        'V_1'        => 'Ideogram V1',
        'V_1_TURBO'  => 'Ideogram V1 Turbo',
        'V_2'        => 'Ideogram V2',
        'V_2_TURBO'  => 'Ideogram V2 Turbo',
        'V_2A'       => 'Ideogram V2a',
        'V_2A_TURBO' => 'Ideogram V2a Turbo',
        'ideogram-upscale' => 'Ideogram Upscale',
        'ideogram-2.0-edit' => 'Ideogram 2.0 Edit',
        'ideogram-2.0-turbo-edit' => 'Ideogram 2.0 Turbo Edit',
    );

    $gemini_image_models = array(
        'gemini-3-pro-image-preview' => 'Gemini 3 Pro Image Preview 🍌',
        'gemini-2.5-flash-image'     => 'Gemini 2.5 Flash Image',
    );

    $image_model_labels = array_merge(
        $dalle_models,
        $deepai_models,
        $stable_diffusion_models,
        $ideogram_models,
        $gemini_image_models
    );

    $lite_image_providers = array( 'dalle' );
    $lite_image_models    = array( 'dall-e-2', 'dall-e-3' );
    $normalized_lite_image_models = array_map( 'strtolower', $lite_image_models );

    $image_provider_locked = array(
        'dalle'           => ! in_array( 'dalle', $lite_image_providers, true ),
        'deepai'          => ! in_array( 'deepai', $lite_image_providers, true ),
        'stablediffusion' => ! in_array( 'stablediffusion', $lite_image_providers, true ),
        'ideogram'        => ! in_array( 'ideogram', $lite_image_providers, true ),
        'gemini'          => ! in_array( 'gemini', $lite_image_providers, true ),
    );

    if ( ! in_array( strtolower( $image_model ), $normalized_lite_image_models, true ) ) {
        $image_model = 'dall-e-3';
        ai_content_wizard_update_setting( 'ai_content_wizard_image_model', $image_model );
    }

    $pro_badge = sprintf(
        '<span class="aiw-lock-badge"><span class="dashicons dashicons-lock"></span>%s</span>',
        esc_html__( 'Pro', 'ai-content-wizard' )
    );
    $pro_note = sprintf(
        '<span class="aiw-provider-note">%s</span>',
        esc_html__( 'Available in AI Content Wizard Pro', 'ai-content-wizard' )
    );
    $upgrade_cta = sprintf(
        '<a class="aiw-upgrade-cta" href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
        esc_url( 'https://aiwizard.buyreadysite.com/' ),
        esc_html__( 'Unlock in Pro', 'ai-content-wizard' )
    );
    
    // Определяем текущего провайдера для изображений.
    $current_image_provider = 'dalle';
    if ( stripos( $image_model, 'dall-e' ) !== false ) {
        $current_image_provider = 'dalle';
    } elseif ( stripos( $image_model, 'deepai' ) !== false ) {
        $current_image_provider = 'deepai';
    } elseif ( stripos( $image_model, 'stable' ) !== false || stripos( $image_model, 'sdxl' ) !== false ) {
        $current_image_provider = 'stablediffusion';
    } elseif ( stripos( $image_model, 'ideogram' ) !== false || stripos( $image_model, 'v_' ) !== false ) {
        $current_image_provider = 'ideogram';
    } elseif ( stripos( $image_model, 'gemini' ) !== false || stripos( $image_model, 'imagen' ) !== false ) {
        $current_image_provider = 'gemini';
    }

    $image_model_labels_lower = array_change_key_case( $image_model_labels, CASE_LOWER );
    $active_image_model_label = $image_model_labels[ $image_model ] ?? ( $image_model_labels_lower[ strtolower( $image_model ) ] ?? strtoupper( $image_model ) );
    $active_image_provider_label = $image_provider_labels[ $current_image_provider ] ?? ucfirst( $current_image_provider );

    // Функция форматирования стоимости для моделей изображений
    function format_image_cost($model) {
        $image_costs = [
            'dall-e-3'    => "$0.080\n(average cost,\ndepends on resolution and quality)",
            'gpt-image-1' => "$0.042\n(average cost for medium-quality images,\nLow \$0.011 • Medium \$0.042 • High \$0.17)",
            'dall-e-2'    => "$0.018\n(average cost,\ndepends on resolution)",
            'deepai-standard' => '$0.050 per image',
            'deepai-hd' => '$0.050 per image',
            'sdxl-1-0' => '$0.03 per image',
            'stable-diffusion-3' => '$0.04 per image',
            'stable-diffusion-ultra' => '$0.08 per image',
            // Ideogram (legacy tooltip costs)
            'v_1' => '$0.06 per image',
            'v_1_turbo' => '$0.02 per image',
            'v_2' => '$0.08 per image',
            'v_2_turbo' => '$0.05 per image',
            'v_2a' => '$0.04 per image',
            'v_2a_turbo' => '$0.025 per image',
            'ideogram-upscale' => '$0.06 per image',
            'ideogram-2.0-edit' => '$0.08 per image',
            'ideogram-2.0-turbo-edit' => '$0.05 per image',
            // Gemini Image Generation (as per November 2025 pricing)
            'gemini-3-pro-image-preview' => '$0.134 per image (1K-2K) • $0.24 per image (4K)',
            'gemini-2.5-flash-image' => '$0.039 per image',
        ];
        return $image_costs[strtolower($model)] ?? 'Cost information not available';
    }
    ?>
    <!-- Блок настроек моделей изображений -->
    <div id="api-settings-image-models">
        <fieldset class="api-legend">
            <legend>
                <?php _e( 'Select Image Model Provider', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpImageProviders')"></span>
            </legend>
            <div class="aiw-radio-group" style="display: flex; gap: 12px; justify-content: center; padding: 10px 0; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 20px 0;">
                <input type="radio" id="provider_dalle" name="image_provider" value="dalle" data-label="<?php echo esc_attr( $image_provider_labels['dalle'] ); ?>" <?php checked( $current_image_provider, 'dalle' ); ?> style="display: none;" <?php echo $image_provider_locked['dalle'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $image_provider_locked['dalle'] ? 'aiw-locked' : ''; ?>" for="provider_dalle" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #10a37f; border-radius: 8px; background: linear-gradient(135deg, #f0f8ff, #e6f0ff); color: #10a37f; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(16, 163, 127, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #10a37f;">
                        <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"></path>
                    </svg>
                    DALL‑E (OpenAI)
                    <?php echo $image_provider_locked['dalle'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_deepai" name="image_provider" value="deepai" data-label="<?php echo esc_attr( $image_provider_labels['deepai'] ); ?>" <?php checked( $current_image_provider, 'deepai' ); ?> style="display: none;" <?php echo $image_provider_locked['deepai'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $image_provider_locked['deepai'] ? 'aiw-locked' : ''; ?>" for="provider_deepai" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #0066ff; border-radius: 8px; background: linear-gradient(135deg, #f5ffff, #e6f2ff); color: #0066ff; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(0, 102, 255, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #0066ff;">
                        <path d="M12 2L2 22h20L12 2z"></path>
                        <path d="M12 2v20"></path>
                    </svg>
                    DeepAI
                    <?php echo $image_provider_locked['deepai'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_stablediffusion" name="image_provider" value="stablediffusion" data-label="<?php echo esc_attr( $image_provider_labels['stablediffusion'] ); ?>" <?php checked( $current_image_provider, 'stablediffusion' ); ?> style="display: none;" <?php echo $image_provider_locked['stablediffusion'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $image_provider_locked['stablediffusion'] ? 'aiw-locked' : ''; ?>" for="provider_stablediffusion" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #9b30ff; border-radius: 8px; background: linear-gradient(135deg, #fff5f5, #ffe6e6); color: #9b30ff; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(155, 48, 255, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #9b30ff;">
                        <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"></path>
                    </svg>
                    Stable Diffusion
                    <?php echo $image_provider_locked['stablediffusion'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_ideogram" name="image_provider" value="ideogram" data-label="<?php echo esc_attr( $image_provider_labels['ideogram'] ); ?>" <?php checked( $current_image_provider, 'ideogram' ); ?> style="display: none;" <?php echo $image_provider_locked['ideogram'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $image_provider_locked['ideogram'] ? 'aiw-locked' : ''; ?>" for="provider_ideogram" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #ff6b00; border-radius: 8px; background: linear-gradient(135deg, #fff5f0, #ffe6e0); color: #ff6b00; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(255, 107, 0, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #ff6b00;">
                        <path d="M12 2L2 22h20L12 2z"></path>
                        <path d="M12 2v20"></path>
                    </svg>
                    Ideogram
                    <?php echo $image_provider_locked['ideogram'] ? $pro_badge . $pro_note : ''; ?>
                </label>

                <input type="radio" id="provider_gemini_image" name="image_provider" value="gemini" data-label="<?php echo esc_attr( $image_provider_labels['gemini'] ); ?>" <?php checked( $current_image_provider, 'gemini' ); ?> style="display: none;" <?php echo $image_provider_locked['gemini'] ? 'disabled' : ''; ?>>
                <label class="aiw-provider-card <?php echo $image_provider_locked['gemini'] ? 'aiw-locked' : ''; ?>" for="provider_gemini_image" style="display: flex; align-items: center; padding: 12px 20px; border: 2px solid #0b74de; border-radius: 8px; background: linear-gradient(135deg, #f0f8ff, #e6f4ff); color: #0b74de; font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 4px rgba(11, 116, 222, 0.2); transition: all 0.3s ease; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" style="margin-right: 8px; fill: #0b74de;">
                        <circle cx="12" cy="12" r="10"></circle>
                        <path d="M12 6v6l4 4"></path>
                    </svg>
                    Gemini / Imagen
                    <?php echo $image_provider_locked['gemini'] ? $pro_badge . $pro_note : ''; ?>
                </label>
            </div>
        </fieldset>

        <div class="aiw-active-selection aiw-active-image-model">
            <span class="aiw-active-selection__label"><?php esc_html_e( 'Active image model', 'ai-content-wizard' ); ?></span>
            <div class="aiw-active-selection__value" id="aiw-active-image-model-name"><?php echo esc_html( $active_image_model_label ); ?></div>
            <p class="aiw-active-selection__meta">
                <?php esc_html_e( 'Provider:', 'ai-content-wizard' ); ?>
                <span id="aiw-active-image-provider"><?php echo esc_html( $active_image_provider_label ); ?></span>
            </p>
        </div>

        <div class="aiw-upgrade-hint">
            <p><?php esc_html_e( 'Need advanced image generators like Stable Diffusion or Ideogram? Upgrade to Pro to enable every provider inside Lite.', 'ai-content-wizard' ); ?></p>
            <?php echo $upgrade_cta; ?>
        </div>

        <!-- Поля API Key для провайдеров изображений -->
        <fieldset class="api-legend" id="dalle_key_block" style="display: none;">
            <legend>
                <?php _e( 'DALL‑E (OpenAI) API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpDalleKey')"></span>
            </legend>
            <p style="font-style: italic;"><?php _e( 'Uses the same OpenAI API key as text models.', 'ai-content-wizard' ); ?></p>
        </fieldset>

        <fieldset class="api-legend" id="deepai_key_block" style="display: none;">
            <legend>
                <?php _e( 'DeepAI API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpDeepAIKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_deepai_api_key"><?php _e( 'DeepAI API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display: flex; align-items: center; width: 100%;">
                            <div style="position: relative; flex-grow: 1;">
                                <input type="password" id="ai_content_wizard_deepai_api_key" name="ai_content_wizard_deepai_api_key" value="<?php echo esc_attr( $deepai_api_key ); ?>" style="width: 100%; padding-right: 30px;">
                                <span class="dashicons dashicons-visibility" style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); cursor: pointer;" onclick="toggleVisibility('ai_content_wizard_deepai_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_deepai_api_key" name="reset_deepai_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left: 10px;" onclick="resetField('ai_content_wizard_deepai_api_key', 'reset_deepai_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>

        <fieldset class="api-legend" id="stablediffusion_key_block" style="display: none;">
            <legend>
                <?php _e( 'Stable Diffusion API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpStableKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_stable_diffusion_api_key"><?php _e( 'Stable Diffusion API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display: flex; align-items: center; width: 100%;">
                            <div style="position: relative; flex-grow: 1;">
                                <input type="password" id="ai_content_wizard_stable_diffusion_api_key" name="ai_content_wizard_stable_diffusion_api_key" value="<?php echo esc_attr( $stable_diffusion_api_key ); ?>" style="width: 100%; padding-right: 30px;">
                                <span class="dashicons dashicons-visibility" style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); cursor: pointer;" onclick="toggleVisibility('ai_content_wizard_stable_diffusion_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_stable_diffusion_api_key" name="reset_stable_diffusion_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left: 10px;" onclick="resetField('ai_content_wizard_stable_diffusion_api_key', 'reset_stable_diffusion_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>

        <fieldset class="api-legend" id="ideogram_key_block" style="display: none;">
            <legend>
                <?php _e( 'Ideogram API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpIdeogramKey')"></span>
            </legend>
            <table class="form-table">
                <tr>
                    <th><label for="ideogram_api_key"><?php _e( 'Ideogram API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display: flex; align-items: center; width: 100%;">
                            <div style="position: relative; flex-grow: 1;">
                                <input type="password" id="ideogram_api_key" name="ideogram_api_key" value="<?php echo esc_attr( $ideogram_api_key ); ?>" style="width: 100%; padding-right: 30px;">
                                <span class="dashicons dashicons-visibility" style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); cursor: pointer;" onclick="toggleVisibility('ideogram_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_ideogram_api_key" name="reset_ideogram_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left: 10px;" onclick="resetField('ideogram_api_key', 'reset_ideogram_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>

        <fieldset class="api-legend" id="gemini_image_key_block" style="display: none;">
            <legend>
                <?php _e( 'Gemini / Imagen API Key', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpGeminiKey')"></span>
            </legend>
            <p class="description" style="margin:0;">
                <?php _e( 'Gemini / Imagen image models reuse the same Google AI Studio key saved in the "Google Gemini API Key" section above.', 'ai-content-wizard' ); ?>
            </p>
        </fieldset>

        <!-- Выбор модели изображений -->
        <fieldset class="api-legend">
            <legend>
                <?php _e( 'Select Image Model', 'ai-content-wizard' ); ?>
                <span class="dashicons dashicons-editor-help aiw-help-icon" onclick="openModal('helpImageModels')"></span>
            </legend>
            <div class="aiw-radio-group" id="image-models-radio-group">
                <div id="dalle_models_container" style="display: none;">
                    <?php
                    foreach ( $dalle_models as $key => $label ) {
                        $checked = ( $image_model === $key ) ? 'checked' : '';
                        $cost_info = format_image_cost($key);
                        echo '<input type="radio" id="img_model_' . esc_attr( $key ) . '" name="image_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="img_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                        // After DALL‑E 3, insert GPT-Image-1
                        if ( $key === 'dall-e-3' ) {
                            $checked_gpt_image_1 = ( $image_model === 'gpt-image-1' ) ? 'checked' : '';
                            $cost_info_gpt_image_1 = format_image_cost('gpt-image-1');
                            echo '<input type="radio" id="img_model_gpt-image-1" name="image_model_selector" value="gpt-image-1" data-label="' . esc_attr( $dalle_models['gpt-image-1'] ) . '" ' . $checked_gpt_image_1 . '>';
                            echo '<label for="img_model_gpt-image-1" title="' . esc_attr( $cost_info_gpt_image_1 ) . '">GPT‑Image‑1</label>';
                        }
                    }
                    ?>
                    <div id="gpt_image_1_notice" class="api-description" style="display:none; margin:10px 0; padding:10px; background:#fffbea; border-left:4px solid #f1c40f;">
                        <?php _e( 'To ensure this model is used responsibly, you may need to complete the API Organization Verification from your developer console before using GPT‑Image‑1. ', 'ai-content-wizard' ); ?>
                        <a href="https://platform.openai.com/settings/organization/general" target="_blank" rel="noopener noreferrer">platform.openai.com/settings/organization/general</a>
                    </div>
                </div>
                <div id="deepai_models_container" style="display: none;">
                    <?php
                    foreach ( $deepai_models as $key => $label ) {
                        $checked = ( $image_model === $key ) ? 'checked' : '';
                        $cost_info = format_image_cost($key);
                        echo '<input type="radio" id="img_model_' . esc_attr( $key ) . '" name="image_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="img_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    ?>
                </div>
                <div id="stablediffusion_models_container" style="display: none;">
                    <?php
                    foreach ( $stable_diffusion_models as $key => $label ) {
                        $checked = ( $image_model === $key ) ? 'checked' : '';
                        $cost_info = format_image_cost($key);
                        echo '<input type="radio" id="img_model_' . esc_attr( $key ) . '" name="image_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="img_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    ?>
                </div>
                <div id="ideogram_models_container" style="display: none;">
                    <?php
                    foreach ( $ideogram_models as $key => $label ) {
                        $checked = ( $image_model === $key ) ? 'checked' : '';
                        $cost_info = format_image_cost($key);
                        echo '<input type="radio" id="img_model_' . esc_attr( $key ) . '" name="image_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="img_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    ?>
                </div>
                <div id="gemini_image_models_container" style="display: none;">
                    <?php
                    foreach ( $gemini_image_models as $key => $label ) {
                        $checked = ( $image_model === $key ) ? 'checked' : '';
                        $cost_info = format_image_cost($key);
                        echo '<input type="radio" id="img_model_' . esc_attr( $key ) . '" name="image_model_selector" value="' . esc_attr( $key ) . '" data-label="' . esc_attr( $label ) . '" ' . $checked . '>';
                        echo '<label for="img_model_' . esc_attr( $key ) . '" title="' . esc_attr( $cost_info ) . '">' . esc_html( $label ) . '</label>';
                    }
                    ?>
                </div>
            </div>
            <input type="hidden" id="ai_content_wizard_image_model" name="ai_content_wizard_image_model" value="<?php echo esc_attr( $image_model ); ?>">
        </fieldset>
    <script type="text/javascript">
jQuery(function($){
    function updateGptImage1Notice() {
        const imgModel = $('input[name="image_model_selector"]:checked').val();
        if ( imgModel === 'gpt-image-1' ) {
            $('#gpt_image_1_notice').show();
        } else {
            $('#gpt_image_1_notice').hide();
        }
    }
    $(document).on('change', 'input[name="image_model_selector"]', updateGptImage1Notice);
    updateGptImage1Notice();
});
</script>
    </div>
    <?php
}


/**
 * Обновляет параметры изображений в зависимости от выбранной модели.
 *
 * @param string $model выбранная модель изображений.
 */
function ai_content_wizard_update_image_model_parameters( $model ) {
    switch ( $model ) {
        case 'dall-e-2':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '512x512' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'standard' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'vivid' );
            break;
        case 'dall-e-3':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '1792x1024' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'hd' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'vivid' );
            break;
        case 'deepai-standard':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '1024x576' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'standard' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'none' );
            break;
        case 'deepai-hd':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '1216x832' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'hd' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'stylized' );
            break;
        case 'sdxl-1-0':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '1344x768' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'ultra' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'digital-art' );
            break;
        case 'stable-diffusion-3':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '16:9' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'hd' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'photorealistic' );
            break;
        case 'stable-diffusion-ultra':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '16:9' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'ultra' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'photorealistic' );
            break;
        // Новые модели Ideogram
        case 'V_1':
        case 'V_1_TURBO':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', 'ASPECT_1_1' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', '' );
            break;
        case 'V_2':
        case 'V_2_TURBO':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', 'RESOLUTION_1024_1024' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'GENERAL' );
            break;
        case 'V_2A':
        case 'V_2A_TURBO':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', 'RESOLUTION_1024_1024' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'GENERAL' );
            break;

        // Gemini Image Generation
        case 'gemini-3-pro-image-preview':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '1024x1024' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'standard' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'general' );
            break;
        case 'gemini-2.5-flash-image':
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', '1024x1024' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', 'standard' );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', 'general' );
            break;
    }
}



/**
 * Вывод дополнительных настроек (misc).
 */

function render_misc_settings() {
    // Получаем настройки
    $bing_api_key    = ai_content_wizard_get_setting( 'ai_content_wizard_bing_api_key', '' );
    $google_api_key  = ai_content_wizard_get_setting( 'ai_content_wizard_google_api_key', '' );
    $google_cse_id   = ai_content_wizard_get_setting( 'ai_content_wizard_google_cse_id', '' );
    $rss_feed_url    = ai_content_wizard_get_setting( 'ai_content_wizard_rss_feed_url', '' );
    ?>
    <!-- Блок дополнительных настроек -->
    <div id="api-settings-misc">
        <fieldset class="api-legend">
            <legend><?php _e( 'Additional Settings', 'ai-content-wizard' ); ?></legend>
            <table class="form-table">
                <tr>
                    <th><label for="ai_content_wizard_bing_api_key"><?php _e( 'Bing API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display: flex; align-items: center; width: 100%;">
                            <div style="position: relative; flex-grow: 1;">
                                <input type="password" id="ai_content_wizard_bing_api_key" name="ai_content_wizard_bing_api_key" value="<?php echo esc_attr( $bing_api_key ); ?>" style="width: 100%; padding-right: 30px;">
                                <span class="dashicons dashicons-visibility" style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); cursor: pointer;" onclick="toggleVisibility('ai_content_wizard_bing_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_bing_api_key" name="reset_bing_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left: 10px;" onclick="resetField('ai_content_wizard_bing_api_key', 'reset_bing_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th><label for="ai_content_wizard_google_api_key"><?php _e( 'Google API Key', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display: flex; align-items: center; width: 100%;">
                            <div style="position: relative; flex-grow: 1;">
                                <input type="password" id="ai_content_wizard_google_api_key" name="ai_content_wizard_google_api_key" value="<?php echo esc_attr( $google_api_key ); ?>" style="width: 100%; padding-right: 30px;">
                                <span class="dashicons dashicons-visibility" style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); cursor: pointer;" onclick="toggleVisibility('ai_content_wizard_google_api_key')"></span>
                            </div>
                            <input type="hidden" id="reset_google_api_key" name="reset_google_api_key" value="0">
                            <button type="button" class="button button-secondary" style="margin-left: 10px;" onclick="resetField('ai_content_wizard_google_api_key', 'reset_google_api_key')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th><label for="ai_content_wizard_google_cse_id"><?php _e( 'Google CSE ID', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <div style="display: flex; align-items: center; width: 100%;">
                            <div style="position: relative; flex-grow: 1;">
                                <input type="password" id="ai_content_wizard_google_cse_id" name="ai_content_wizard_google_cse_id" value="<?php echo esc_attr( $google_cse_id ); ?>" style="width: 100%; padding-right: 30px;">
                                <span class="dashicons dashicons-visibility" style="position: absolute; right: 8px; top: 50%; transform: translateY(-50%); cursor: pointer;" onclick="toggleVisibility('ai_content_wizard_google_cse_id')"></span>
                            </div>
                            <input type="hidden" id="reset_google_cse_id" name="reset_google_cse_id" value="0">
                            <button type="button" class="button button-secondary" style="margin-left: 10px;" onclick="resetField('ai_content_wizard_google_cse_id', 'reset_google_cse_id')"><?php _e( 'Reset Key', 'ai-content-wizard' ); ?></button>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th><label for="ai_content_wizard_rss_feed_url"><?php _e( 'RSS Feed URL', 'ai-content-wizard' ); ?></label></th>
                    <td>
                        <input type="text" id="ai_content_wizard_rss_feed_url" name="ai_content_wizard_rss_feed_url" value="<?php echo esc_attr( $rss_feed_url ); ?>" class="regular-text">
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>

    <?php
}