<?php
// Protection from direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

include( 'ai-content-wizard-tooltips.php' );

function enqueue_settings_page_script() {
    if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'ai_content_wizard_settings' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        return;
    }

    wp_enqueue_media(); // Добавляем медиатеку
    wp_enqueue_script( 'ai-content-wizard-settings-script', AI_CONTENT_WIZARD_URL . 'js/settings-page-script.js', array( 'jquery' ), AI_CONTENT_WIZARD_VERSION, true );
    wp_localize_script( 'ai-content-wizard-settings-script', 'ajax_object', array( 
        'ajax_url' => admin_url( 'admin-ajax.php' )
    ) );
    wp_localize_script( 'ai-content-wizard-settings-script', 'ai_content_wizard_settings', array(
        'selectWatermarkImageTitle' => __( 'Select Watermark Image', 'ai-content-wizard' ),
        'selectWatermarkButtonText' => __( 'Select Image', 'ai-content-wizard' )
    ) );
    
    // Подключаем стили вкладок и новый облик настроек
    wp_enqueue_style(
        'ai-content-wizard-content-plan-style',
        AI_CONTENT_WIZARD_URL . 'css/content-plan-styles.css',
        array(),
        AI_CONTENT_WIZARD_VERSION
    );

    $settings_lite_style = AI_CONTENT_WIZARD_PATH . 'css/settings-page-lite.css';
    $settings_lite_version = AI_CONTENT_WIZARD_VERSION;
    if ( file_exists( $settings_lite_style ) ) {
        $settings_lite_version .= '-' . filemtime( $settings_lite_style );
    }

    wp_enqueue_style(
        'ai-content-wizard-settings-lite-style',
        AI_CONTENT_WIZARD_URL . 'css/settings-page-lite.css',
        array( 'ai-content-wizard-content-plan-style' ),
        $settings_lite_version
    );
}

add_action( 'admin_enqueue_scripts', 'enqueue_settings_page_script' );

function ai_content_wizard_settings_page() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    $latest_post = get_posts( array( 'numberposts' => 1 ) );
    if ( ! empty( $latest_post ) ) {
        $latest_post = $latest_post[0];
    } else {
        $latest_post = null;
    }

    if ( $latest_post ) {
        $latest_post_title = get_the_title( $latest_post );
        $latest_post_date  = get_the_date( get_option( 'date_format' ), $latest_post );
        $latest_status     = get_post_status( $latest_post );
        $status_object     = $latest_status ? get_post_status_object( $latest_status ) : null;
        $latest_post_status_label = $status_object ? $status_object->label : ( $latest_status ? ucfirst( $latest_status ) : __( 'Published', 'ai-content-wizard' ) );
    } else {
        $latest_post_title        = __( 'Ready to publish', 'ai-content-wizard' );
        $latest_post_date         = __( 'No posts yet', 'ai-content-wizard' );
        $latest_post_status_label = __( 'Not available', 'ai-content-wizard' );
    }
    
    $errors = array();
    
    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        if ( ! isset( $_POST['ai_content_wizard_nonce'] ) || ! wp_verify_nonce( $_POST['ai_content_wizard_nonce'], 'ai_content_wizard_save_settings' ) ) {
            $errors[] = __( 'Security error. Please try again.', 'ai-content-wizard' );
        } else {
            // Обработка основных настроек (без e-mail уведомлений, категорий и Telegram)
            ai_content_wizard_update_setting( 'ai_content_wizard_image_size', sanitize_text_field( $_POST['ai_content_wizard_image_size'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_quality', sanitize_text_field( $_POST['ai_content_wizard_image_quality'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_image_style', sanitize_text_field( $_POST['ai_content_wizard_image_style'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_prompt_specification', sanitize_text_field( $_POST['ai_content_wizard_prompt_specification'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_format', sanitize_text_field( $_POST['ai_content_wizard_format'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_text_length', sanitize_text_field( $_POST['ai_content_wizard_text_length'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_site_type', sanitize_text_field( $_POST['ai_content_wizard_site_type'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_writing_style', sanitize_text_field( $_POST['ai_content_wizard_writing_style'] ) );
            // Удалены поля Custom Theme и Additional Requests
            ai_content_wizard_update_setting( 'ai_content_wizard_target_audience', sanitize_text_field( $_POST['ai_content_wizard_target_audience'] ) );
            // Регионы заблокированы – их значение не меняется
            // ai_content_wizard_update_setting( 'ai_content_wizard_target_region', sanitize_text_field( $_POST['ai_content_wizard_target_region'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_post_status', sanitize_text_field( $_POST['ai_content_wizard_post_status'] ) );
            ai_content_wizard_update_setting( 'ai_content_wizard_enable_image_generation', isset( $_POST['ai_content_wizard_enable_image_generation'] ) ? 1 : 0 );
    
            // E-mail уведомления удалены в lite-версии
    
            // Footer Credit – всегда включён (locked)
            ai_content_wizard_update_setting( 'ai_content_wizard_enable_footer_credit', 1 );
    
            // Telegram настройки – в lite-версии не доступны, поэтому не сохраняем
    
            // Настройка генерации тегов
            ai_content_wizard_update_setting( 'ai_content_wizard_enable_tags_generation', isset( $_POST['ai_content_wizard_enable_tags_generation'] ) ? 1 : 0 );
    
            // Водяной знак – блокируется в lite-версии (сохраняем как выключено)
            ai_content_wizard_update_setting( 'ai_content_wizard_enable_watermark', 0 );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_type', 'text' );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_text', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_image_id', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_color', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_opacity', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_size', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_watermark_position', '' );
    
            // Дополнительные изображения и оптимизация – блокируются
            ai_content_wizard_update_setting( 'ai_content_wizard_enable_additional_images', 0 );
            ai_content_wizard_update_setting( 'ai_content_wizard_additional_images_count', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_enable_image_optimization', 0 );
    
            // Enhanced Generation Quality – сохраняем как выключено
            ai_content_wizard_update_setting( 'ai_content_wizard_enhanced_generation_quality', 0 );
    
            // Telegram формат сообщений – блокируются
            ai_content_wizard_update_setting( 'ai_content_wizard_custom_announcement_prompt', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_telegram_message_format', '' );
            ai_content_wizard_update_setting( 'ai_content_wizard_telegram_format_type', 'standard' );
    
            if ( empty( $errors ) ) {
                echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'ai-content-wizard' ) . '</p></div>';
            }
        }
    }
    
    // В lite-версии доступны только модели dall-e-2 и dall-e-3
    $model_names = array(
        'dall-e-2' => 'DALL-E 2',
        'dall-e-3' => 'DALL-E 3'
    );
    
    // Получаем выбранную модель. Если выбрана другая – принудительно возвращаем default (dall-e-3)
    $image_model = ai_content_wizard_get_setting( 'ai_content_wizard_image_model', 'dall-e-3' );
    if ( ! array_key_exists( $image_model, $model_names ) ) {
        $image_model = 'dall-e-3';
    }
    $model_display_name = $model_names[ $image_model ];
    
    $image_size         = ai_content_wizard_get_setting( 'ai_content_wizard_image_size', '' );
    $image_quality      = ai_content_wizard_get_setting( 'ai_content_wizard_image_quality', '' );
    $image_style        = ai_content_wizard_get_setting( 'ai_content_wizard_image_style', '' );
    $prompt_specification = ai_content_wizard_get_setting( 'ai_content_wizard_prompt_specification', '' );
    
    // Параметры только для dall-e моделей
    if ( $image_model === 'dall-e-2' ) {
        $size_options    = array( '256x256', '512x512', '1024x1024' );
        $quality_options = array( 'standard' );
        $style_options   = array( 'vivid' );
    } elseif ( $image_model === 'dall-e-3' ) {
        $size_options    = array( '1024x1024', '1792x1024', '1024x1792' );
        $quality_options = array( 'standard', 'hd' );
        $style_options   = array( 'natural', 'vivid' );
    } else {
        $size_options    = array();
        $quality_options = array();
        $style_options   = array();
    }
    
    // Массивы данных по текстовым моделям (без изменений)
    $text_model_names = array(
        'gpt-3.5-turbo'       => 'GPT-3.5-turbo',
        'gpt-3.5-turbo-16k'   => 'GPT-3.5-turbo 16k',
        'gpt-4'               => 'GPT-4',
        'gpt-4-turbo'         => 'GPT-4 Turbo',
        'gpt-4o'              => 'GPT-4o',
        'gpt-4o-mini'         => 'GPT-4o mini',
        'gpt-4o1-preview'     => 'GPT-4o1-preview',
        'gpt-4o1-mini'        => 'GPT-4o1-mini',
        'gpt-4.5-preview'     => 'GPT-4.5-preview',
        'gpt-5-mini'          => 'GPT-5 mini',
    );
    
    $text_model              = ai_content_wizard_get_setting( 'ai_content_wizard_text_model', 'gpt-4o-mini' );
    $text_model_display_name = array_key_exists( $text_model, $text_model_names ) ? $text_model_names[ $text_model ] : 'Unknown Model';
    
    echo '<div class="wrap ai-content-wizard-main-page">';

    if ( class_exists( 'AiContentWizard_PromoPilot' ) ) {
        AiContentWizard_PromoPilot::render_settings_banner();
    }

    echo '<div class="aiw-settings-page-wrapper">';
    echo '  <div class="acw-page-header">';
    echo '      <div>';
    echo '          <p class="aiw-eyebrow">' . esc_html__( 'Lite version overview', 'ai-content-wizard' ) . '</p>';
    echo '          <h1>' . esc_html__( 'AI Content Wizard Settings', 'ai-content-wizard' ) . '</h1>';
    echo '          <p>' . esc_html__( 'Fine-tune defaults for text, images, and publishing. Upgrade to Pro for automations, bulk scheduling, and multi-channel support.', 'ai-content-wizard' ) . '</p>';
    echo '      </div>';
    echo '      <a class="aiw-upgrade-link" href="https://aiwizard.buyreadysite.com/" target="_blank" rel="noopener noreferrer">';
    echo '          <span>' . esc_html__( 'Unlock In Pro', 'ai-content-wizard' ) . '</span>';
    echo '          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">';
    echo '              <line x1="5" y1="12" x2="19" y2="12"></line>';
    echo '              <polyline points="12 5 19 12 12 19"></polyline>';
    echo '          </svg>';
    echo '      </a>';
    echo '  </div>';

    echo '  <div class="aiw-settings-metrics">';
    echo '      <div class="aiw-metric-card">';
    echo '          <span class="aiw-metric-label">' . esc_html__( 'Text model', 'ai-content-wizard' ) . '</span>';
    echo '          <strong>' . esc_html( strtoupper( $text_model_display_name ) ) . '</strong>';
    echo '          <p>' . esc_html__( 'Switch providers and unlock premium prompts in the API settings.', 'ai-content-wizard' ) . '</p>';
    echo '          <a href="' . esc_url( admin_url( 'admin.php?page=ai_content_wizard_api_settings' ) ) . '" class="aiw-metric-pill is-accent">' . esc_html__( 'Manage text model', 'ai-content-wizard' ) . '</a>';
    echo '      </div>';
    echo '      <div class="aiw-metric-card">';
    echo '          <span class="aiw-metric-label">' . esc_html__( 'Image model', 'ai-content-wizard' ) . '</span>';
    echo '          <strong>' . esc_html( strtoupper( $model_display_name ) ) . '</strong>';
    echo '          <p>' . esc_html__( 'Lite users can work with DALL-E defaults. Pro unlocks more formats.', 'ai-content-wizard' ) . '</p>';
    echo '          <a href="' . esc_url( admin_url( 'admin.php?page=ai_content_wizard_api_settings' ) ) . '" class="aiw-metric-pill is-muted">' . esc_html__( 'Adjust image model', 'ai-content-wizard' ) . '</a>';
    echo '      </div>';
    echo '      <div class="aiw-metric-card">';
    echo '          <span class="aiw-metric-label">' . esc_html__( 'Latest publish state', 'ai-content-wizard' ) . '</span>';
    echo '          <strong>' . esc_html( $latest_post_status_label ) . '</strong>';
    echo '          <p>' . esc_html( $latest_post_title ) . '</p>';
    echo '          <span class="aiw-metric-pill is-accent">' . esc_html( $latest_post_date ) . '</span>';
    echo '      </div>';
    echo '  </div>';

    if ( ! empty( $errors ) ) {
        echo '<div class="notice notice-error is-dismissible aiw-inline-notice"><p>' . implode( '<br>', array_map( 'esc_html', $errors ) ) . '</p></div>';
    }
    ?>

    <div class="aiw-settings-main">
        <!-- Tabs navigation -->
        <div class="ai-content-wizard-tabs">
        <div class="tabs">
            <div class="tab-link active" data-tab="tab-main-settings">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tab-icon">
                    <circle cx="12" cy="12" r="3"></circle>
                    <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82V9a1.65 1.65 0 0 0 1.51-1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path>
                </svg>
                <?php esc_html_e( 'Main Settings', 'ai-content-wizard' ); ?>
            </div>
            <div class="tab-link" data-tab="tab-text-settings">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tab-icon">
                    <path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"></path>
                    <path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"></path>
                </svg>
                <?php esc_html_e( 'Text Settings', 'ai-content-wizard' ); ?>
            </div>
            <div class="tab-link" data-tab="tab-image-settings">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tab-icon">
                    <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                    <circle cx="8.5" cy="8.5" r="1.5"></circle>
                    <polyline points="21 15 16 10 5 21"></polyline>
                </svg>
                <?php esc_html_e( 'Image Settings', 'ai-content-wizard' ); ?>
            </div>
            <div class="tab-link" data-tab="tab-other-settings">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tab-icon">
                    <path d="M12 2v20M2 12h20"></path>
                </svg>
                <?php esc_html_e( 'Other Settings', 'ai-content-wizard' ); ?>
            </div>
            <div class="tab-link" data-tab="tab-categories-settings">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tab-icon">
                    <rect x="3" y="3" width="7" height="7"></rect>
                    <rect x="14" y="3" width="7" height="7"></rect>
                    <rect x="14" y="14" width="7" height="7"></rect>
                    <rect x="3" y="14" width="7" height="7"></rect>
                </svg>
                <?php esc_html_e( 'Category Settings', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span>
            </div>
            <div class="tab-link" data-tab="tab-telegram-channel-settings">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="tab-icon">
                    <path d="M21 2L2 11l7 2 3 7 4-10z"></path>
                </svg>
                <?php esc_html_e( 'Telegram Channel Settings', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span>
            </div>
        </div>
        </div>
        
        <form method="post" action="" class="ai-content-wizard-form" enctype="multipart/form-data">
        <?php wp_nonce_field( 'ai_content_wizard_save_settings', 'ai_content_wizard_nonce' ); ?>
    
        <!-- Tab: Main Settings -->
        <div id="tab-main-settings" class="tab-content ai-content-wizard-settings-container">
            <div class="ai-content-wizard-main-settings">
                <h2 class="aiw-settings-header"><?php esc_html_e( 'Main Settings', 'ai-content-wizard' ); ?></h2>
                <!-- В lite-версии e-mail уведомления убраны -->
                <!-- Режим эксперт (PRO – locked) -->
                <div class="ai-content-wizard-setting aiw-highlight-card aiw-locked-setting important-setting">
                    <h3 style="text-align: center;">
                        <?php esc_html_e( 'Important Setting:', 'ai-content-wizard' ); ?> <span class="pro-badge" style="font-size: 14px; background: #0073aa; color: #fff; padding: 2px 6px; border-radius: 3px; margin-left: 10px;">PRO</span>
                    </h3>
                    <p style="text-align: center;"><?php esc_html_e( 'Activate this feature to significantly improve the quality of generation at the expense of longer processing times and higher costs. This enables a dual generation process for superior quality.', 'ai-content-wizard' ); ?></p>
                    <div style="display: flex; justify-content: center; align-items: center; margin-top: 15px;">
                        <label for="ai_content_wizard_enhanced_generation_quality" class="switch" style="position: relative; display: inline-block; width: 60px; height: 34px;">
                            <input type="checkbox" id="ai_content_wizard_enhanced_generation_quality" name="ai_content_wizard_enhanced_generation_quality" value="1" <?php checked( ai_content_wizard_get_setting( 'ai_content_wizard_enhanced_generation_quality', 0 ) ); ?> disabled style="opacity: 0; width: 0; height: 0;">
                            <span class="slider round" style="position: absolute; cursor: not-allowed; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; border-color: red;"></span>
                        </label>
                        <label for="ai_content_wizard_enhanced_generation_quality" style="margin-left: 10px;"><?php esc_html_e( 'Enable Enhanced Generation Quality', 'ai-content-wizard' ); ?></label>
                    </div>
                </div>
    
            </div>
        </div>
    
        <!-- Tab: Text Settings -->
        <div id="tab-text-settings" class="tab-content">
            <div class="ai-content-wizard-text-settings">
                                <h2 class="aiw-settings-header">
                                    <?php esc_html_e( 'Text Settings', 'ai-content-wizard' ); ?>
                                    <span class="ai-content-wizard-model-name"><?php echo esc_html( strtoupper( $text_model_display_name ) ); ?> <?php esc_html_e( 'currently used', 'ai-content-wizard' ); ?></span>
                                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=ai_content_wizard_api_settings' ) ); ?>" class="ai-content-wizard-change-model-link">
                                        <?php esc_html_e( 'Change Text Model', 'ai-content-wizard' ); ?>
                                    </a>
                                </h2>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_format">
                        <?php esc_html_e( 'Format:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('formatInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_format" name="ai_content_wizard_format">
                        <?php $current_format = ai_content_wizard_get_setting( 'ai_content_wizard_format', 'post' ); ?>
                        <option value="post" <?php selected( $current_format, 'post' ); ?>><?php esc_html_e( 'Post', 'ai-content-wizard' ); ?></option>
                        <option value="recipe" <?php selected( $current_format, 'recipe' ); ?>><?php esc_html_e( 'Recipe', 'ai-content-wizard' ); ?></option>
                        <option value="review" <?php selected( $current_format, 'review' ); ?>><?php esc_html_e( 'Review', 'ai-content-wizard' ); ?></option>
                        <option value="guide" <?php selected( $current_format, 'guide' ); ?>><?php esc_html_e( 'Guide', 'ai-content-wizard' ); ?></option>
                        <option value="article" <?php selected( $current_format, 'article' ); ?>><?php esc_html_e( 'Article', 'ai-content-wizard' ); ?></option>
                        <option value="interview" <?php selected( $current_format, 'interview' ); ?>><?php esc_html_e( 'Interview', 'ai-content-wizard' ); ?></option>
                        <option value="news" <?php selected( $current_format, 'news' ); ?>><?php esc_html_e( 'News', 'ai-content-wizard' ); ?></option>
                        <option value="analysis" <?php selected( $current_format, 'analysis' ); ?>><?php esc_html_e( 'Analysis', 'ai-content-wizard' ); ?></option>
                    </select>
                </div>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_text_length">
                        <?php esc_html_e( 'Text Length:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('text_lengthInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_text_length" name="ai_content_wizard_text_length">
                        <?php
                        $current_text_length = ai_content_wizard_get_setting( 'ai_content_wizard_text_length', '500' );
                        $length_options = array(
                            '500'  => esc_html__( '500 characters', 'ai-content-wizard' ),
                            '1500' => esc_html__( '1500 characters', 'ai-content-wizard' ),
                            '2000' => esc_html__( '2000 characters', 'ai-content-wizard' ),
                            '3000' => esc_html__( '3000 characters (PRO)', 'ai-content-wizard' ),
                            '5000' => esc_html__( '5000 characters or more (PRO)', 'ai-content-wizard' )
                        );
                        foreach ( $length_options as $value => $label ) {
                            $disabled = in_array( $value, array( '3000', '5000' ) ) ? ' disabled' : '';
                            echo '<option value="' . esc_attr( $value ) . '" ' . selected( $current_text_length, $value, false ) . esc_attr( $disabled ) . '>' . esc_html( $label ) . '</option>';

                        }
                        ?>
                    </select>
                </div>
    
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_site_type">
                        <?php esc_html_e( 'Site Type:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('site_typeInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_site_type" name="ai_content_wizard_site_type">
                        <?php $current_site_type = ai_content_wizard_get_setting( 'ai_content_wizard_site_type', 'store' ); ?>
                        <option value="store" <?php selected( $current_site_type, 'store' ); ?>><?php esc_html_e( 'Store', 'ai-content-wizard' ); ?></option>
                        <option value="blog" <?php selected( $current_site_type, 'blog' ); ?>><?php esc_html_e( 'Blog', 'ai-content-wizard' ); ?></option>
                        <option value="personal" <?php selected( $current_site_type, 'personal' ); ?>><?php esc_html_e( 'Personal', 'ai-content-wizard' ); ?></option>
                        <option value="educational" <?php selected( $current_site_type, 'educational' ); ?>><?php esc_html_e( 'Educational', 'ai-content-wizard' ); ?></option>
                        <option value="news" <?php selected( $current_site_type, 'news' ); ?>><?php esc_html_e( 'News Portal', 'ai-content-wizard' ); ?></option>
                        <option value="community" <?php selected( $current_site_type, 'community' ); ?>><?php esc_html_e( 'Community', 'ai-content-wizard' ); ?></option>
                        <option value="portfolio" <?php selected( $current_site_type, 'portfolio' ); ?>><?php esc_html_e( 'Portfolio', 'ai-content-wizard' ); ?></option>
                    </select>
                </div>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_writing_style">
                        <?php esc_html_e( 'Writing Style:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('writing_styleInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_writing_style" name="ai_content_wizard_writing_style">
                        <?php $current_writing_style = ai_content_wizard_get_setting( 'ai_content_wizard_writing_style', 'formal' ); ?>
                        <option value="formal" <?php selected( $current_writing_style, 'formal' ); ?>><?php esc_html_e( 'Formal', 'ai-content-wizard' ); ?></option>
                        <option value="informal" <?php selected( $current_writing_style, 'informal' ); ?>><?php esc_html_e( 'Informal', 'ai-content-wizard' ); ?></option>
                        <option value="persuasive" <?php selected( $current_writing_style, 'persuasive' ); ?>><?php esc_html_e( 'Persuasive', 'ai-content-wizard' ); ?></option>
                        <option value="descriptive" <?php selected( $current_writing_style, 'descriptive' ); ?>><?php esc_html_e( 'Descriptive', 'ai-content-wizard' ); ?></option>
                        <option value="scientific" <?php selected( $current_writing_style, 'scientific' ); ?>><?php esc_html_e( 'Scientific', 'ai-content-wizard' ); ?></option>
                        <option value="narrative" <?php selected( $current_writing_style, 'narrative' ); ?>><?php esc_html_e( 'Narrative', 'ai-content-wizard' ); ?></option>
                    </select>
                </div>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_target_audience">
                        <?php esc_html_e( 'Target Audience:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('target_audienceInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_target_audience" name="ai_content_wizard_target_audience">
                        <?php $current_target_audience = ai_content_wizard_get_setting( 'ai_content_wizard_target_audience', 'general' ); ?>
                        <option value="general" <?php selected( $current_target_audience, 'general' ); ?>><?php esc_html_e( 'General Audience', 'ai-content-wizard' ); ?></option>
                        <option value="teenagers" <?php selected( $current_target_audience, 'teenagers' ); ?>><?php esc_html_e( 'Teenagers', 'ai-content-wizard' ); ?></option>
                        <option value="students" <?php selected( $current_target_audience, 'students' ); ?>><?php esc_html_e( 'Students', 'ai-content-wizard' ); ?></option>
                        <option value="professionals" <?php selected( $current_target_audience, 'professionals' ); ?>><?php esc_html_e( 'Professionals', 'ai-content-wizard' ); ?></option>
                        <option value="seniors" <?php selected( $current_target_audience, 'seniors' ); ?>><?php esc_html_e( 'Seniors', 'ai-content-wizard' ); ?></option>
                    </select>
                </div>
                <!-- Region поле заблокировано и дополнено бейджем PRO -->
                <div class="ai-content-wizard-setting aiw-locked-setting">
                    <label for="ai_content_wizard_target_region">
                        <?php esc_html_e( 'Region:', 'ai-content-wizard' ); ?> <span class="pro-badge" style="font-size:12px; background:#0073aa; color:#fff; padding:2px 4px; border-radius:3px; margin-left:5px;">PRO</span>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('target_regionInfo')"></span>
                    </label>
                    <input type="text" id="ai_content_wizard_target_region" name="ai_content_wizard_target_region" 
                           value="<?php echo esc_attr( ai_content_wizard_get_setting( 'ai_content_wizard_target_region', '' ) ); ?>" 
                           placeholder="<?php esc_attr_e( 'Enter target region (Pro version only)', 'ai-content-wizard' ); ?>" 
                           class="regular-text" disabled />
                </div>
    
                <!-- Блок с дополнительными пожеланиями убран -->
            </div>
        </div>
    
        <!-- Tab: Image Settings -->
        <div id="tab-image-settings" class="tab-content">
            <div class="ai-content-wizard-image-settings">
                <h2 class="aiw-settings-header">
                    <?php esc_html_e( 'Image Settings', 'ai-content-wizard' ); ?>
                    <span class="ai-content-wizard-model-name"><?php echo esc_html( strtoupper( $model_display_name ) ); ?> <?php esc_html_e( 'currently used', 'ai-content-wizard' ); ?></span>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=ai_content_wizard_api_settings' ) ); ?>" class="ai-content-wizard-change-model-link">
                        <?php esc_html_e( 'Change Image Model', 'ai-content-wizard' ); ?>
                    </a>
                </h2>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_image_size">
                        <?php esc_html_e( 'Image Size:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('image_sizeInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_image_size" name="ai_content_wizard_image_size">
                        <?php if ( ! empty( $size_options ) ) : ?>
                            <?php foreach ( $size_options as $size_option ) : ?>
                                <option value="<?php echo esc_attr( $size_option ); ?>" <?php selected( $image_size, $size_option ); ?>><?php echo esc_html( $size_option ); ?></option>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <option value=""><?php esc_html_e( 'No size options available', 'ai-content-wizard' ); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_image_quality">
                        <?php esc_html_e( 'Image Quality:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('image_qualityInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_image_quality" name="ai_content_wizard_image_quality">
                        <?php if ( ! empty( $quality_options ) ) : ?>
                            <?php foreach ( $quality_options as $quality_option ) : ?>
                                <option value="<?php echo esc_attr( $quality_option ); ?>" <?php selected( $image_quality, $quality_option ); ?>><?php echo esc_html( $quality_option ); ?></option>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <option value=""><?php esc_html_e( 'No quality options available', 'ai-content-wizard' ); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="ai-content-wizard-setting">
                    <label for="ai_content_wizard_image_style">
                        <?php esc_html_e( 'Image Style:', 'ai-content-wizard' ); ?>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('image_styleInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_image_style" name="ai_content_wizard_image_style">
                        <?php if ( ! empty( $style_options ) ) : ?>
                            <?php foreach ( $style_options as $style_option ) : ?>
                                <option value="<?php echo esc_attr( $style_option ); ?>" <?php selected( $image_style, $style_option ); ?>><?php echo esc_html( $style_option ); ?></option>
                            <?php endforeach; ?>
                        <?php else : ?>
                            <option value=""><?php esc_html_e( 'No style options available', 'ai-content-wizard' ); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="ai-content-wizard-setting aiw-locked-setting">
                    <label for="ai_content_wizard_prompt_specification">
                        <?php esc_html_e( 'Prompt Specification:', 'ai-content-wizard' ); ?> <span class="pro-badge" style="font-size:12px; background:#0073aa; color:#fff; padding:2px 4px; border-radius:3px; margin-left:5px;">PRO</span>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('prompt_specificationInfo')"></span>
                    </label>
                    <textarea id="ai_content_wizard_prompt_specification" name="ai_content_wizard_prompt_specification" placeholder="<?php esc_attr_e( 'e.g., A futuristic cityscape at sunset (Pro version only)', 'ai-content-wizard' ); ?>" rows="1" style="width: calc(100% - 30px); padding: 5px; margin: 0; box-sizing: border-box; font-size: 18px; border-radius: 4px; border: 1px solid #ddd; background-color: #fff; color: #333; box-shadow: 0 2px 4px rgba(0,0,0,0.1); height: 50px; margin-bottom: 15px; max-width: 100%; resize: vertical;" disabled><?php echo esc_textarea( $prompt_specification ); ?></textarea>
                </div>
    
                <div class="ai-content-wizard-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_image_generation" name="ai_content_wizard_enable_image_generation" value="1" <?php checked( ai_content_wizard_get_setting( 'ai_content_wizard_enable_image_generation', 1 ) ); ?>>
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_image_generation"><?php esc_html_e( 'Enable Image Generation:', 'ai-content-wizard' ); ?></label>
                </div>
                <!-- Watermark settings (PRO – locked) -->
                <div class="ai-content-wizard-setting aiw-locked-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_watermark" name="ai_content_wizard_enable_watermark" value="1">
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_watermark"><?php esc_html_e( 'Enable Watermark:', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span></label>
                </div>
                <!-- Additional Images (PRO – locked) -->
                <div class="ai-content-wizard-setting aiw-locked-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_additional_images" name="ai_content_wizard_enable_additional_images" value="1">
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_additional_images"><?php esc_html_e( 'Enable Additional Images Generation:', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span></label>
                </div>
                <!-- Image Optimization (PRO – locked) -->
                <div class="ai-content-wizard-setting aiw-locked-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_image_optimization" name="ai_content_wizard_enable_image_optimization" value="1">
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_image_optimization"><?php esc_html_e( 'Enable Image Optimization:', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span></label>
                </div>
            </div>
        </div>
    
        <!-- Tab: Other Settings -->
        <div id="tab-other-settings" class="tab-content">
            <div class="ai-content-wizard-other-settings">
                <h2 class="aiw-settings-header"><?php esc_html_e( 'Other Settings', 'ai-content-wizard' ); ?></h2>
                <div class="ai-content-wizard-setting aiw-locked-setting">
                    <label for="ai_content_wizard_post_status">
                        <?php esc_html_e( 'Default Post Status for Content Plan:', 'ai-content-wizard' ); ?>
                        <span class="pro-badge" style="font-size:12px; background:#0073aa; color:#fff; padding:2px 4px; border-radius:3px; margin-left:5px;">PRO</span>
                        <span class="info-icon dashicons dashicons-editor-help" onclick="showPopup('post_statusInfo')"></span>
                    </label>
                    <select id="ai_content_wizard_post_status" name="ai_content_wizard_post_status" disabled>
                        <?php
                        $current_post_status = ai_content_wizard_get_setting( 'ai_content_wizard_post_status', 'publish' );
                        $post_status_options = array(
                            'publish' => esc_html__( 'Published', 'ai-content-wizard' ),
                            'draft'   => esc_html__( 'Draft', 'ai-content-wizard' ),
                            'pending' => esc_html__( 'Pending Review', 'ai-content-wizard' ),
                            'private' => esc_html__( 'Private', 'ai-content-wizard' ),
                        );
                        foreach ( $post_status_options as $value => $label ) {
                            echo '<option value="' . esc_attr( $value ) . '" ' . selected( $current_post_status, $value, false ) . '>' . esc_html( $label ) . '</option>';
                        }
                        ?>
                    </select>
                </div>
    
                <div class="ai-content-wizard-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_toc_generation" name="ai_content_wizard_enable_toc_generation" value="1" <?php checked( ai_content_wizard_get_setting( 'ai_content_wizard_enable_toc_generation', 1 ) ); ?>>
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_toc_generation"><?php esc_html_e( 'Enable Table of Contents Generation', 'ai-content-wizard' ); ?></label>
                </div>
                <div class="ai-content-wizard-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_tags_generation" name="ai_content_wizard_enable_tags_generation" value="1" <?php checked( ai_content_wizard_get_setting( 'ai_content_wizard_enable_tags_generation', 0 ) ); ?>>
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_tags_generation"><?php esc_html_e( 'Enable Tags Generation for Posts', 'ai-content-wizard' ); ?></label>
                </div>
                <div class="ai-content-wizard-setting">
                    <label class="switch">
                        <input type="checkbox" id="ai_content_wizard_enable_more_tag" name="ai_content_wizard_enable_more_tag" value="1" <?php checked( ai_content_wizard_get_setting( 'ai_content_wizard_enable_more_tag', 0 ) ); ?>>
                        <span class="slider round"></span>
                    </label>
                    <label for="ai_content_wizard_enable_more_tag"><?php esc_html_e( 'Enable More Tag Insertion', 'ai-content-wizard' ); ?></label>
                </div>
            </div>
        </div>
    
        <!-- Tab: Category Settings (PRO – blocked) -->
        <div id="tab-categories-settings" class="tab-content">
            <div class="ai-content-wizard-categories-settings">
                <h2 class="aiw-settings-header"><?php esc_html_e( 'Category Specific Settings', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span></h2>
                <p><?php esc_html_e( 'Category specific settings are available in the Pro version.', 'ai-content-wizard' ); ?></p>
            </div>
        </div>
    
        <!-- Tab: Telegram Channel Settings (PRO – blocked) -->
        <div id="tab-telegram-channel-settings" class="tab-content">
            <div class="ai-content-wizard-telegram-channel-settings">
                <h2 class="aiw-settings-header"><?php esc_html_e( 'Telegram Channel Settings', 'ai-content-wizard' ); ?> <span class="pro-badge">PRO</span></h2>
                <p><?php esc_html_e( 'Telegram notifications settings are available in the Pro version.', 'ai-content-wizard' ); ?></p>
            </div>
        </div>
    
        <?php submit_button(); ?>
    </form>
    
    <?php
    echo '</div>';
    ai_content_wizard_display_tooltips();
    echo '</div>';
    echo '</div>';
}
    
add_action( 'wp_ajax_get_current_text_model', 'ai_content_wizard_get_current_text_model' );
function ai_content_wizard_get_current_text_model() {
    echo esc_html( ai_content_wizard_get_setting( 'ai_content_wizard_text_model', 'gpt-4o-mini' ) );
    wp_die();
}
    
add_action( 'wp_ajax_ai_content_wizard_reset_all_categories', 'ai_content_wizard_reset_all_categories' );
function ai_content_wizard_reset_all_categories() {
    // В lite-версии категорийные настройки отсутствуют, поэтому ничего не делаем.
    wp_die();
}
    
function ai_content_wizard_delete_category_setting( $category_id, $key ) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'ai_content_wizard_settings';
    $wpdb->delete( $table_name, array( 'setting_key' => $category_id . '_' . $key ) );
}

