<?php
/**
 * Settings Page
 *
 * @package AIContentGenerator
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Include License Manager
if ( ! class_exists( 'AICG\License_Manager' ) ) {
    require_once ROIDAICO_PLUGIN_DIR . 'includes/class-license-manager.php';
}

// Check Pro status
$is_pro = \AICG\License_Manager::is_pro();
$upgrade_url = \AICG\License_Manager::get_upgrade_url();

// Get current tab
$current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'api';

$tabs = array(
    'api'     => __( 'API Settings', 'roidnet-ai-content-generator' ),
    'content' => __( 'Content Settings', 'roidnet-ai-content-generator' ),
    'queue'   => __( 'Queue Settings', 'roidnet-ai-content-generator' ),
);

// Always add license tab as an internal tab (not a redirect)
if ( function_exists( 'roidaico_fs' ) ) {
    $tabs['license'] = __( 'License', 'roidnet-ai-content-generator' );
}
?>

<div class="wrap roidaico-wrap">
    <h1><?php esc_html_e( 'Settings', 'roidnet-ai-content-generator' ); ?></h1>
    
    <nav class="nav-tab-wrapper">
        <?php foreach ( $tabs as $tab_id => $tab_name ) : ?>
            <a href="<?php echo esc_url( add_query_arg( 'tab', $tab_id ) ); ?>" 
               class="nav-tab <?php echo $current_tab === $tab_id ? 'nav-tab-active' : ''; ?>">
                <?php echo esc_html( $tab_name ); ?>
            </a>
        <?php endforeach; ?>
    </nav>
    
    <div class="roidaico-settings-content">
        <?php if ( $current_tab === 'api' ) : ?>
        <!-- API Settings -->
        <form method="post" action="options.php" id="roidaico-api-settings-form">
            <?php settings_fields( 'roidaico_api_settings' ); ?>
            
            <div class="roidaico-settings-section">
                <h2><?php esc_html_e( 'AI Service Provider', 'roidnet-ai-content-generator' ); ?></h2>
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_ai_provider"><?php esc_html_e( 'Select Provider', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <select id="roidaico_ai_provider" name="roidaico_ai_provider" class="regular-text">
                                <option value="openrouter" <?php selected( get_option( 'roidaico_ai_provider', 'openrouter' ), 'openrouter' ); ?>>
                                    <?php esc_html_e( 'OpenRouter API (Default)', 'roidnet-ai-content-generator' ); ?>
                                </option>
                                <option value="wp_ai_client" <?php selected( get_option( 'roidaico_ai_provider', 'openrouter' ), 'wp_ai_client' ); ?> <?php disabled( ! function_exists( 'wp_ai_client_prompt' ) ); ?>>
                                    <?php 
                                    if ( function_exists( 'wp_ai_client_prompt' ) ) {
                                        esc_html_e( 'WordPress 7.0 AI Client (Native)', 'roidnet-ai-content-generator' );
                                    } else {
                                        esc_html_e( 'WordPress 7.0 AI Client (Requires WP 7.0+)', 'roidnet-ai-content-generator' );
                                    }
                                    ?>
                                </option>
                            </select>
                            <p class="description">
                                <?php esc_html_e( 'Choose which AI service to use for content generation. WordPress 7.0 AI Client is native and provider-agnostic.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_image_source">
                                <?php esc_html_e( 'Image Source', 'roidnet-ai-content-generator' ); ?>
                                <?php if ( ! $is_pro ) : ?>
                                    <span class="roidaico-pro-badge">PRO</span>
                                <?php endif; ?>
                            </label>
                        </th>
                        <td>
                             <select id="roidaico_image_source" name="roidaico_image_source" class="regular-text">
                                 <option value="pexels" <?php selected( get_option( 'roidaico_image_source', 'pexels' ), 'pexels' ); ?>>
                                     <?php esc_html_e( 'Pexels (Stock Photos)', 'roidnet-ai-content-generator' ); ?>
                                 </option>
                                 <option value="ai" <?php selected( get_option( 'roidaico_image_source', 'pexels' ), 'ai' ); ?> <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                                     <?php 
                                     if ( ! $is_pro ) {
                                         esc_html_e( 'AI Image Generation [PRO]', 'roidnet-ai-content-generator' );
                                     } else {
                                         esc_html_e( 'AI Image Generation', 'roidnet-ai-content-generator' );
                                     }
                                     ?>
                                 </option>
                             </select>
                             <p class="description">
                                 <?php esc_html_e( 'Select the source for images included in the articles. AI Image Generation is a premium feature.', 'roidnet-ai-content-generator' ); ?>
                             </p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="roidaico-settings-section roidaico-provider-section-openrouter" style="<?php echo get_option( 'roidaico_ai_provider', 'openrouter' ) === 'wp_ai_client' ? 'display:none;' : ''; ?>">
                <h2><?php esc_html_e( 'OpenRouter API Configuration', 'roidnet-ai-content-generator' ); ?></h2>
                <p class="description">
                    <?php 
                    echo wp_kses(
                        sprintf(
                            __( 'Get your API key from %s', 'roidnet-ai-content-generator' ),
                            '<a href="https://openrouter.ai/keys" target="_blank">OpenRouter</a>'
                        ),
                        array( 'a' => array( 'href' => array(), 'target' => array() ) )
                    );
                    ?>
                </p>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_openrouter_api_key"><?php esc_html_e( 'API Key', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <div class="roidaico-input-group">
                                <input type="password" 
                                       id="roidaico_openrouter_api_key" 
                                       name="roidaico_openrouter_api_key" 
                                       value="<?php echo esc_attr( get_option( 'roidaico_openrouter_api_key' ) ); ?>" 
                                       class="regular-text">
                                <button type="button" class="button roidaico-toggle-password">
                                    <span class="dashicons dashicons-visibility"></span>
                                </button>
                                <button type="button" class="button roidaico-test-api" data-api="openrouter">
                                    <?php esc_html_e( 'Test Connection', 'roidnet-ai-content-generator' ); ?>
                                </button>
                            </div>
                            <span class="roidaico-api-status" id="openrouter-status"></span>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_openrouter_model"><?php esc_html_e( 'AI Model', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <div class="roidaico-model-selector">
                                <select id="roidaico_openrouter_model" name="roidaico_openrouter_model" class="regular-text roidaico-select2" data-current="<?php echo esc_attr( get_option( 'roidaico_openrouter_model', 'openai/gpt-4o-mini' ) ); ?>">
                                    <option value=""><?php esc_html_e( 'Loading models...', 'roidnet-ai-content-generator' ); ?></option>
                                </select>
                            </div>
                            <button type="button" class="button" id="roidaico-refresh-models">
                                <span class="dashicons dashicons-update"></span>
                            </button>
                            <p class="description">
                                <?php esc_html_e( 'Select the AI model for content generation.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                    <tr class="roidaico-ai-image-model-row" style="<?php echo get_option( 'roidaico_image_source', 'pexels' ) !== 'ai' ? 'display:none;' : ''; ?>">
                        <th scope="row">
                            <label for="roidaico_ai_image_model">
                                <?php esc_html_e( 'AI Image Model', 'roidnet-ai-content-generator' ); ?>
                                <?php if ( ! $is_pro ) : ?>
                                    <span class="roidaico-pro-badge">PRO</span>
                                <?php endif; ?>
                            </label>
                        </th>
                        <td>
                            <select id="roidaico_ai_image_model" name="roidaico_ai_image_model" class="regular-text roidaico-select2" <?php echo ! $is_pro ? 'disabled' : ''; ?> style="width: 350px;">
                                <?php 
                                $current_image_model = get_option( 'roidaico_ai_image_model', 'google/gemini-2.5-flash-image' );
                                $image_models = $this->ai_client->get_image_models();
                                if ( is_wp_error( $image_models ) ) {
                                    $image_models = array(
                                        array(
                                            'id' => 'google/gemini-2.5-flash-image',
                                            'name' => 'Google: Gemini 2.5 Flash Image',
                                            'price' => '$0.03',
                                        ),
                                    );
                                }
                                foreach ( $image_models as $details ) :
                                ?>
                                    <option value="<?php echo esc_attr( $details['id'] ); ?>" <?php selected( $current_image_model, $details['id'] ); ?>>
                                        <?php 
                                        $label = $details['name'];
                                        if (!empty($details['price'])) {
                                            $label .= ' (' . $details['price'] . ' / image)';
                                        }
                                        echo esc_html($label); 
                                        ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <p class="description">
                                <?php esc_html_e( 'Select the AI model used to generate images. Models are sorted from cheapest to most expensive.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="roidaico-settings-section roidaico-provider-section-wp-ai-client" style="<?php echo get_option( 'roidaico_ai_provider', 'openrouter' ) === 'wp_ai_client' ? '' : 'display:none;'; ?>">
                <h2><?php esc_html_e( 'WordPress Native AI Configuration', 'roidnet-ai-content-generator' ); ?></h2>
                <p class="description">
                    <?php 
                    echo wp_kses(
                        sprintf(
                            __( 'Configure your AI connectors and API keys globally in the %s screen.', 'roidnet-ai-content-generator' ),
                            '<a href="' . esc_url( admin_url( 'options-general.php?page=connectors' ) ) . '" target="_blank">' . __( 'Settings > Connectors', 'roidnet-ai-content-generator' ) . '</a>'
                        ),
                        array( 'a' => array( 'href' => array(), 'target' => array() ) )
                    );
                    ?>
                </p>
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <?php esc_html_e( 'Integration Status', 'roidnet-ai-content-generator' ); ?>
                        </th>
                        <td>
                            <div class="roidaico-input-group">
                                <button type="button" class="button roidaico-test-api" data-api="wp_ai_client">
                                    <?php esc_html_e( 'Test Integration', 'roidnet-ai-content-generator' ); ?>
                                </button>
                            </div>
                            <span class="roidaico-api-status" id="wp_ai_client-status"></span>
                            <p class="description">
                                <?php esc_html_e( 'Verify if WordPress native AI client is correctly configured with at least one active provider/connector.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div class="roidaico-settings-section">
                <h2><?php esc_html_e( 'Pexels API', 'roidnet-ai-content-generator' ); ?></h2>
                <p class="description">
                    <?php 
                    echo wp_kses(
                        sprintf(
                            __( 'Get your API key from %s', 'roidnet-ai-content-generator' ),
                            '<a href="https://www.pexels.com/api/" target="_blank">Pexels</a>'
                        ),
                        array( 'a' => array( 'href' => array(), 'target' => array() ) )
                    );
                    ?>
                </p>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_pexels_api_key"><?php esc_html_e( 'API Key', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <div class="roidaico-input-group">
                                <input type="password" 
                                       id="roidaico_pexels_api_key" 
                                       name="roidaico_pexels_api_key" 
                                       value="<?php echo esc_attr( get_option( 'roidaico_pexels_api_key' ) ); ?>" 
                                       class="regular-text">
                                <button type="button" class="button roidaico-toggle-password">
                                    <span class="dashicons dashicons-visibility"></span>
                                </button>
                                <button type="button" class="button roidaico-test-api" data-api="pexels">
                                    <?php esc_html_e( 'Test Connection', 'roidnet-ai-content-generator' ); ?>
                                </button>
                            </div>
                            <span class="roidaico-api-status" id="pexels-status"></span>
                        </td>
                    </tr>
                </table>
            </div>
            
            <?php submit_button(); ?>
        </form>

        <?php 
        // Include API Setup Guide
        include ROIDAICO_PLUGIN_DIR . 'admin/views/api-guide.php'; 
        ?>

        
        <?php elseif ( $current_tab === 'content' ) : ?>
        <!-- Content Settings -->
        <form method="post" action="options.php">
            <?php settings_fields( 'roidaico_content_settings' ); ?>
            
            <div class="roidaico-settings-section">
                <h2><?php esc_html_e( 'Default Content Options', 'roidnet-ai-content-generator' ); ?></h2>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_default_tone"><?php esc_html_e( 'Writing Tone', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <select id="roidaico_default_tone" name="roidaico_default_tone">
                                <?php 
                                $current_tone = get_option( 'roidaico_default_tone', 'professional' );
                                $tones = array(
                                    'professional' => __( 'Professional', 'roidnet-ai-content-generator' ),
                                    'casual'       => __( 'Casual', 'roidnet-ai-content-generator' ),
                                    'friendly'     => __( 'Friendly', 'roidnet-ai-content-generator' ),
                                    'formal'       => __( 'Formal', 'roidnet-ai-content-generator' ),
                                    'humorous'     => __( 'Humorous', 'roidnet-ai-content-generator' ),
                                    'informative'  => __( 'Informative', 'roidnet-ai-content-generator' ),
                                );
                                foreach ( $tones as $value => $label ) :
                                ?>
                                    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current_tone, $value ); ?>>
                                        <?php echo esc_html( $label ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_default_length">
                                <?php esc_html_e( 'Content Length', 'roidnet-ai-content-generator' ); ?>
                                <?php if ( ! $is_pro ) : ?>
                                    <span class="roidaico-pro-badge">PRO</span>
                                <?php endif; ?>
                            </label>
                        </th>
                        <td>
                            <select id="roidaico_default_length" name="roidaico_default_length">
                                <?php 
                                $current_length = get_option( 'roidaico_default_length', 'short' );
                                $lengths = array(
                                    'short'  => __( 'Short (~500 words)', 'roidnet-ai-content-generator' ),
                                    'medium' => __( 'Medium (~1000 words)', 'roidnet-ai-content-generator' ),
                                    'long'   => __( 'Long (~2000+ words)', 'roidnet-ai-content-generator' ),
                                );
                                foreach ( $lengths as $value => $label ) :
                                    $disabled = ( ! $is_pro && $value !== 'short' ) ? 'disabled' : '';
                                    $pro_label = ( ! $is_pro && $value !== 'short' ) ? ' [PRO]' : '';
                                ?>
                                    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current_length, $value ); ?> <?php echo $disabled; ?>>
                                        <?php echo esc_html( $label . $pro_label ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <?php if ( ! $is_pro ) : ?>
                                <p class="description">
                                    <a href="<?php echo esc_url( $upgrade_url ); ?>"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                                    <?php esc_html_e( 'for Medium and Long articles.', 'roidnet-ai-content-generator' ); ?>
                                </p>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_default_language"><?php esc_html_e( 'Language', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <select id="roidaico_default_language" name="roidaico_default_language">
                                <?php 
                                $current_lang = get_option( 'roidaico_default_language', 'en' );
                                $languages = array(
                                    'en' => 'English',
                                    'ar' => 'Arabic',
                                    'es' => 'Spanish',
                                    'fr' => 'French',
                                    'de' => 'German',
                                    'it' => 'Italian',
                                    'pt' => 'Portuguese',
                                    'ru' => 'Russian',
                                    'zh' => 'Chinese',
                                    'ja' => 'Japanese',
                                    'ko' => 'Korean',
                                    'tr' => 'Turkish',
                                );
                                foreach ( $languages as $value => $label ) :
                                ?>
                                    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current_lang, $value ); ?>>
                                        <?php echo esc_html( $label ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div class="roidaico-settings-section <?php echo ! $is_pro ? 'roidaico-pro-section' : ''; ?>">
                <h2>
                    <?php esc_html_e( 'Internal Linking', 'roidnet-ai-content-generator' ); ?>
                    <?php if ( ! $is_pro ) : ?>
                        <span class="roidaico-pro-badge">PRO</span>
                    <?php endif; ?>
                </h2>
                
                <?php if ( ! $is_pro ) : ?>
                <div class="roidaico-pro-overlay">
                    <span class="dashicons dashicons-lock"></span>
                    <h3><?php esc_html_e( 'Pro Feature', 'roidnet-ai-content-generator' ); ?></h3>
                    <p><?php esc_html_e( 'Automatically add internal links to your articles.', 'roidnet-ai-content-generator' ); ?></p>
                    <a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                </div>
                <?php endif; ?>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <?php esc_html_e( 'Enable Internal Links', 'roidnet-ai-content-generator' ); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox" 
                                       name="roidaico_enable_internal_links" 
                                       value="1" 
                                       <?php checked( get_option( 'roidaico_enable_internal_links', true ) ); ?>
                                       <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                                <?php esc_html_e( 'Automatically add internal links to generated content', 'roidnet-ai-content-generator' ); ?>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_max_internal_links"><?php esc_html_e( 'Max Internal Links', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <input type="number" 
                                   id="roidaico_max_internal_links" 
                                   name="roidaico_max_internal_links" 
                                   value="<?php echo esc_attr( get_option( 'roidaico_max_internal_links', 3 ) ); ?>" 
                                   min="1" 
                                   max="10" 
                                   class="small-text"
                                   <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div class="roidaico-settings-section <?php echo ! $is_pro ? 'roidaico-pro-section' : ''; ?>">
                <h2>
                    <?php esc_html_e( 'Article Generation', 'roidnet-ai-content-generator' ); ?>
                    <?php if ( ! $is_pro ) : ?>
                        <span class="roidaico-pro-badge">PRO</span>
                    <?php endif; ?>
                </h2>
                
                <?php if ( ! $is_pro ) : ?>
                <div class="roidaico-pro-overlay">
                    <span class="dashicons dashicons-lock"></span>
                    <h3><?php esc_html_e( 'Pro Feature', 'roidnet-ai-content-generator' ); ?></h3>
                    <p><?php esc_html_e( 'Custom instructions and multiple images per article.', 'roidnet-ai-content-generator' ); ?></p>
                    <a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                </div>
                <?php endif; ?>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_custom_instructions"><?php esc_html_e( 'Custom Instructions', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <textarea id="roidaico_custom_instructions" 
                                      name="roidaico_custom_instructions" 
                                      rows="5" 
                                      class="large-text"
                                      <?php echo ! $is_pro ? 'disabled' : ''; ?>><?php echo esc_textarea( get_option( 'roidaico_custom_instructions', '' ) ); ?></textarea>
                            <p class="description">
                                <?php esc_html_e( 'Custom instructions to include when generating articles. These will be added to the AI prompt for every article.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                            <p class="description">
                                <strong><?php esc_html_e( 'Example:', 'roidnet-ai-content-generator' ); ?></strong> 
                                <?php esc_html_e( 'Always include a call-to-action at the end. Use bullet points for lists. Avoid using the word "very".', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_images_count">
                                <?php esc_html_e( 'Number of Images', 'roidnet-ai-content-generator' ); ?>
                                <?php if ( ! $is_pro ) : ?>
                                    <span class="roidaico-pro-badge">PRO</span>
                                <?php endif; ?>
                            </label>
                        </th>
                        <td>
                            <?php 
                            $max_images = $is_pro ? 10 : 1;
                            $current_images = min( (int) get_option( 'roidaico_images_count', 1 ), $max_images );
                            ?>
                            <input type="number" 
                                   id="roidaico_images_count" 
                                   name="roidaico_images_count" 
                                   value="<?php echo esc_attr( $current_images ); ?>" 
                                   min="0" 
                                   max="<?php echo esc_attr( $max_images ); ?>" 
                                   class="small-text"
                                   <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                            <p class="description">
                                <?php if ( $is_pro ) : ?>
                                    <?php esc_html_e( 'Number of images to include in each article (0 = no images, max 10).', 'roidnet-ai-content-generator' ); ?>
                                <?php else : ?>
                                    <?php esc_html_e( 'Free version: 1 image per article.', 'roidnet-ai-content-generator' ); ?>
                                    <a href="<?php echo esc_url( $upgrade_url ); ?>"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                                    <?php esc_html_e( 'for up to 10 images.', 'roidnet-ai-content-generator' ); ?>
                                <?php endif; ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div class="roidaico-settings-section <?php echo ! $is_pro ? 'roidaico-pro-section' : ''; ?>">
                <h2>
                    <?php esc_html_e( 'SEO Integration', 'roidnet-ai-content-generator' ); ?>
                    <?php if ( ! $is_pro ) : ?>
                        <span class="roidaico-pro-badge">PRO</span>
                    <?php endif; ?>
                </h2>
                
                <?php if ( ! $is_pro ) : ?>
                <div class="roidaico-pro-overlay">
                    <span class="dashicons dashicons-lock"></span>
                    <h3><?php esc_html_e( 'Pro Feature', 'roidnet-ai-content-generator' ); ?></h3>
                    <p><?php esc_html_e( 'Integration with Yoast SEO and Rank Math.', 'roidnet-ai-content-generator' ); ?></p>
                    <a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                </div>
                <?php endif; ?>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_seo_plugin"><?php esc_html_e( 'SEO Plugin', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <select id="roidaico_seo_plugin" name="roidaico_seo_plugin" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                                <?php 
                                $current_seo = get_option( 'roidaico_seo_plugin', 'none' );
                                $seo_plugins = array(
                                    'auto'     => __( 'Auto-detect', 'roidnet-ai-content-generator' ),
                                    'yoast'    => __( 'Yoast SEO', 'roidnet-ai-content-generator' ),
                                    'rankmath' => __( 'Rank Math', 'roidnet-ai-content-generator' ),
                                    'none'     => __( 'None', 'roidnet-ai-content-generator' ),
                                );
                                foreach ( $seo_plugins as $value => $label ) :
                                ?>
                                    <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current_seo, $value ); ?>>
                                        <?php echo esc_html( $label ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <p class="description">
                                <?php esc_html_e( 'Select your SEO plugin for meta data integration.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>
            
            <?php submit_button(); ?>
        </form>
        
        <?php elseif ( $current_tab === 'queue' ) : ?>
        <!-- Queue Settings -->
        <form method="post" action="options.php">
            <?php settings_fields( 'roidaico_queue_settings' ); ?>
            
            <div class="roidaico-settings-section">
                <h2><?php esc_html_e( 'Queue Management', 'roidnet-ai-content-generator' ); ?></h2>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_max_ready_articles">
                                <?php esc_html_e( 'Max Ready Articles', 'roidnet-ai-content-generator' ); ?>
                                <?php if ( ! $is_pro ) : ?>
                                    <span class="roidaico-pro-badge">PRO</span>
                                <?php endif; ?>
                            </label>
                        </th>
                        <td>
                            <?php 
                            $max_allowed = $is_pro ? 50 : 2;
                            $current_max = min( (int) get_option( 'roidaico_max_ready_articles', 2 ), $max_allowed );
                            ?>
                            <input type="number" 
                                   id="roidaico_max_ready_articles" 
                                   name="roidaico_max_ready_articles" 
                                   value="<?php echo esc_attr( $current_max ); ?>" 
                                   min="1" 
                                   max="<?php echo esc_attr( $max_allowed ); ?>" 
                                   class="small-text"
                                   <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                            <p class="description">
                                <?php if ( $is_pro ) : ?>
                                    <?php esc_html_e( 'Maximum number of draft articles to keep ready for publishing.', 'roidnet-ai-content-generator' ); ?>
                                <?php else : ?>
                                    <?php esc_html_e( 'Free version: max 2 articles.', 'roidnet-ai-content-generator' ); ?>
                                    <a href="<?php echo esc_url( $upgrade_url ); ?>"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                                    <?php esc_html_e( 'for up to 50.', 'roidnet-ai-content-generator' ); ?>
                                <?php endif; ?>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_min_interval_minutes"><?php esc_html_e( 'Minimum Interval', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <input type="number" 
                                   id="roidaico_min_interval_minutes" 
                                   name="roidaico_min_interval_minutes" 
                                   value="<?php echo esc_attr( get_option( 'roidaico_min_interval_minutes', 15 ) ); ?>" 
                                   min="15" 
                                   max="1440" 
                                   class="small-text">
                            <span><?php esc_html_e( 'minutes', 'roidnet-ai-content-generator' ); ?></span>
                            <p class="description">
                                <?php esc_html_e( 'Minimum time between publishing articles. Default: 15 minutes.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>
            
            <div class="roidaico-settings-section <?php echo ! $is_pro ? 'roidaico-pro-section' : ''; ?>">
                <h2>
                    <?php esc_html_e( 'Auto-Generate Keywords', 'roidnet-ai-content-generator' ); ?>
                    <?php if ( ! $is_pro ) : ?>
                        <span class="roidaico-pro-badge">PRO</span>
                    <?php endif; ?>
                </h2>
                
                <?php if ( ! $is_pro ) : ?>
                <div class="roidaico-pro-overlay">
                    <span class="dashicons dashicons-lock"></span>
                    <h3><?php esc_html_e( 'Pro Feature', 'roidnet-ai-content-generator' ); ?></h3>
                    <p><?php esc_html_e( 'Automatically generate keywords when queue is empty.', 'roidnet-ai-content-generator' ); ?></p>
                    <a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?></a>
                </div>
                <?php endif; ?>
                
                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <?php esc_html_e( 'Enable Auto-Generation', 'roidnet-ai-content-generator' ); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox" 
                                       name="roidaico_auto_generate_keywords" 
                                       value="1" 
                                       <?php checked( get_option( 'roidaico_auto_generate_keywords', false ) ); ?>
                                       <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                                <?php esc_html_e( 'Automatically generate keywords when queue is empty', 'roidnet-ai-content-generator' ); ?>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <?php esc_html_e( 'Auto-Translate Keywords', 'roidnet-ai-content-generator' ); ?>
                        </th>
                        <td>
                            <label>
                                <input type="checkbox" 
                                       name="roidaico_auto_translate_keywords" 
                                       value="1" 
                                       <?php checked( get_option( 'roidaico_auto_translate_keywords', false ) ); ?>
                                       <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                                <?php esc_html_e( 'Automatically translate auto-generated keywords to all other active languages', 'roidnet-ai-content-generator' ); ?>
                            </label>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_auto_generate_category"><?php esc_html_e( 'Default Category', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <select id="roidaico_auto_generate_category" name="roidaico_auto_generate_category" <?php echo ! $is_pro ? 'disabled' : ''; ?>>
                                <option value=""><?php esc_html_e( '— Select Category —', 'roidnet-ai-content-generator' ); ?></option>
                                <?php 
                                $current_category = get_option( 'roidaico_auto_generate_category', '' );
                                $categories = get_categories( array(
                                    'hide_empty' => false,
                                    'orderby'    => 'name',
                                    'order'      => 'ASC',
                                ) );
                                foreach ( $categories as $category ) :
                                ?>
                                    <option value="<?php echo esc_attr( $category->term_id ); ?>" <?php selected( $current_category, $category->term_id ); ?>>
                                        <?php echo esc_html( $category->name ); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <p class="description">
                                <?php esc_html_e( 'Select the category for auto-generated keywords. If empty, the category name will be used for topic context.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">
                            <label for="roidaico_auto_generate_instructions"><?php esc_html_e( 'Generation Instructions', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <textarea id="roidaico_auto_generate_instructions" 
                                      name="roidaico_auto_generate_instructions" 
                                      rows="4" 
                                      class="large-text"
                                      <?php echo ! $is_pro ? 'disabled' : ''; ?>><?php echo esc_textarea( get_option( 'roidaico_auto_generate_instructions', '' ) ); ?></textarea>
                            <p class="description">
                                <?php esc_html_e( 'Topic or instructions for automatic keyword generation. Leave empty to use category name.', 'roidnet-ai-content-generator' ); ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>
            
            <?php submit_button(); ?>
        </form>

        <?php elseif ( $current_tab === 'license' ) : ?>
        <!-- License Management Tab -->
        <?php
        $is_pro_user     = \AICG\License_Manager::is_pro();
        $is_trial_user   = \AICG\License_Manager::is_trial();
        $fs_registered   = function_exists( 'roidaico_fs' ) && roidaico_fs()->is_registered();
        ?>

        <div class="roidaico-settings-section">
            <h2><?php esc_html_e( 'License Management', 'roidnet-ai-content-generator' ); ?></h2>

            <?php if ( $is_pro_user && ! $is_trial_user ) : ?>
            <!-- Pro License Active -->
            <div class="notice notice-success inline" style="margin: 0 0 20px 0; padding: 15px 20px; border-left-color: #46b450;">
                <p style="display: flex; align-items: center; gap: 8px; font-size: 14px; margin: 0;">
                    <span class="dashicons dashicons-yes-alt" style="color: #46b450; font-size: 22px;"></span>
                    <strong style="color: #46b450;"><?php esc_html_e( 'Pro License Active', 'roidnet-ai-content-generator' ); ?></strong>
                    <?php esc_html_e( '&mdash; All premium features are unlocked.', 'roidnet-ai-content-generator' ); ?>
                </p>
            </div>

            <table class="form-table">
                <tr>
                    <th><?php esc_html_e( 'Status', 'roidnet-ai-content-generator' ); ?></th>
                    <td>
                        <span style="display: inline-flex; align-items: center; gap: 5px; color: #46b450; font-weight: 600;">
                            <span class="dashicons dashicons-yes-alt"></span>
                            <?php esc_html_e( 'Active', 'roidnet-ai-content-generator' ); ?>
                        </span>
                    </td>
                </tr>
            </table>

            <?php if ( $fs_registered ) : ?>
            <p>
                <a href="<?php echo esc_url( roidaico_fs()->get_account_url() ); ?>" class="button">
                    <span class="dashicons dashicons-admin-users" style="vertical-align: middle; margin-right: 4px;"></span>
                    <?php esc_html_e( 'Manage License & Account', 'roidnet-ai-content-generator' ); ?>
                </a>
            </p>
            <?php endif; ?>

            <?php elseif ( $is_trial_user ) : ?>
            <!-- Trial Active -->
            <div class="notice notice-info inline" style="margin: 0 0 20px 0; padding: 15px 20px;">
                <p style="display: flex; align-items: center; gap: 8px; font-size: 14px; margin: 0;">
                    <span class="dashicons dashicons-clock" style="color: #0073aa; font-size: 22px;"></span>
                    <strong style="color: #0073aa;"><?php esc_html_e( 'Trial Active', 'roidnet-ai-content-generator' ); ?></strong>
                    <?php esc_html_e( '&mdash; All premium features are available during your trial period.', 'roidnet-ai-content-generator' ); ?>
                </p>
            </div>

            <p>
                <a href="<?php echo esc_url( roidaico_fs()->get_upgrade_url() ); ?>" class="button button-primary">
                    <?php esc_html_e( 'Upgrade to Pro', 'roidnet-ai-content-generator' ); ?>
                </a>
                <?php if ( $fs_registered ) : ?>
                <a href="<?php echo esc_url( roidaico_fs()->get_account_url() ); ?>" class="button" style="margin-left: 8px;">
                    <?php esc_html_e( 'Manage Account', 'roidnet-ai-content-generator' ); ?>
                </a>
                <?php endif; ?>
            </p>

            <?php else : ?>
            <!-- Free version: License activation available for all users (registered, anonymous, or unregistered).
                 opt_in() in ajax_activate_license handles all states transparently - best practice per Freemius docs. -->
            <div class="notice notice-info inline" style="margin: 0 0 20px 0; padding: 15px 20px;">
                <p style="display: flex; align-items: center; gap: 8px; font-size: 14px; margin: 0;">
                    <span class="dashicons dashicons-info" style="color: #0073aa; font-size: 22px;"></span>
                    <strong><?php esc_html_e( 'Free Version Active', 'roidnet-ai-content-generator' ); ?></strong>
                    <?php esc_html_e( '&mdash; Enter your license key below to unlock all Pro features.', 'roidnet-ai-content-generator' ); ?>
                </p>
            </div>


            <!-- License Activation Form -->
            <div id="roidaico-license-activate-section" style="border: 1px solid #c3c4c7; border-radius: 4px; padding: 20px; background: #fff; margin-bottom: 25px;">
                <h3 style="margin-top: 0; padding-top: 0;">
                    <?php esc_html_e( 'Activate License', 'roidnet-ai-content-generator' ); ?>
                </h3>
                <p style="color: #646970; margin-bottom: 4px;">
                    <?php esc_html_e( 'Enter your license key below to instantly unlock all Pro features. No separate download or reinstall needed.', 'roidnet-ai-content-generator' ); ?>
                </p>

                <table class="form-table">
                    <tr>
                        <th scope="row">
                            <label for="roidaico_license_key"><?php esc_html_e( 'License Key', 'roidnet-ai-content-generator' ); ?></label>
                        </th>
                        <td>
                            <div style="display: flex; align-items: center; gap: 8px; flex-wrap: wrap;">
                                <input type="text" 
                                       id="roidaico_license_key" 
                                       name="license_key" 
                                       class="regular-text" 
                                       placeholder="<?php esc_attr_e( 'e.g. sk_xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx', 'roidnet-ai-content-generator' ); ?>"
                                       style="width: 360px; font-family: monospace;"
                                       autocomplete="off"
                                       spellcheck="false">
                                <button type="button" id="roidaico-activate-license-btn" class="button button-primary">
                                    <span class="dashicons dashicons-unlock" style="vertical-align: middle; margin-right: 3px;"></span>
                                    <?php esc_html_e( 'Activate License', 'roidnet-ai-content-generator' ); ?>
                                </button>
                            </div>
                            <p class="description" id="roidaico-license-message" style="display: none; margin-top: 8px;"></p>
                        </td>
                    </tr>
                </table>

                <p class="description">
                    <?php
                    echo wp_kses(
                        sprintf(
                            /* translators: %s: License recovery URL */
                            __( 'Can\'t find your license key? <a href="%s" target="_blank">Retrieve it here</a>.', 'roidnet-ai-content-generator' ),
                            'https://dashboard.freemius.com/license-recovery/23018/roidnet-ai-content-generator/'
                        ),
                        array( 'a' => array( 'href' => array(), 'target' => array() ) )
                    );
                    ?>
                </p>
            </div>

            <hr style="margin: 25px 0; border-top: 1px solid #e0e0e0;">

            <p style="color: #646970; font-size: 13px; margin-bottom: 10px;">
                <strong><?php esc_html_e( 'Don\'t have a license yet?', 'roidnet-ai-content-generator' ); ?></strong>
            </p>
            <p>
                <a href="<?php echo esc_url( roidaico_fs()->get_trial_url() ); ?>" class="button button-primary" style="margin-right: 8px;">
                    <span class="dashicons dashicons-controls-play" style="vertical-align: middle; margin-right: 3px;"></span>
                    <?php esc_html_e( 'Start Free Trial', 'roidnet-ai-content-generator' ); ?>
                </a>
                <a href="<?php echo esc_url( roidaico_fs()->get_upgrade_url() ); ?>" class="button">
                    <span class="dashicons dashicons-cart" style="vertical-align: middle; margin-right: 3px;"></span>
                    <?php esc_html_e( 'Purchase License', 'roidnet-ai-content-generator' ); ?>
                </a>
            </p>

            <?php if ( $fs_registered ) : ?>
            <hr style="margin: 25px 0; border-top: 1px solid #e0e0e0;">
            <p class="description">
                <span class="dashicons dashicons-admin-users" style="vertical-align: middle;"></span>
                <a href="<?php echo esc_url( roidaico_fs()->get_account_url() ); ?>">
                    <?php esc_html_e( 'Manage your account via Freemius', 'roidnet-ai-content-generator' ); ?>
                </a>
            </p>
            <?php endif; ?>

            <?php endif; ?>
        </div>

        <!-- License Activation JavaScript -->
        <script type="text/javascript">
        jQuery(document).ready(function($) {
            var $licenseKey = $('#roidaico_license_key');
            var $activateBtn = $('#roidaico-activate-license-btn');
            var $message = $('#roidaico-license-message');

            $activateBtn.on('click', function() {
                var licenseKey = $licenseKey.val().trim();

                if (!licenseKey) {
                    showMessage('<?php echo esc_js( __( 'Please enter a license key.', 'roidnet-ai-content-generator' ) ); ?>', 'error');
                    $licenseKey.focus();
                    return;
                }

                // Disable button during activation
                $activateBtn.prop('disabled', true).html(
                    '<span class="dashicons dashicons-update dashicons-spin" style="vertical-align: middle; margin-right: 3px;"></span> ' +
                    '<?php echo esc_js( __( 'Activating...', 'roidnet-ai-content-generator' ) ); ?>'
                );
                $message.hide();

                // Use our custom license activation handler.
                // The SDK's built-in fs_activate_license_{id} action is NOT registered
                // in the free version without a license, so we use our own handler that
                // calls the SDK's public opt_in() method with the license key.
                $.post(aicgAdmin.ajaxUrl, {
                    action: 'roidaico_activate_license',
                    nonce: aicgAdmin.nonce,
                    license_key: licenseKey
                }, function(response) {
                    if (response.success) {
                        showMessage(response.data.message || '<?php echo esc_js( __( 'License activated successfully! Reloading...', 'roidnet-ai-content-generator' ) ); ?>', 'success');
                        // Reload page after short delay to reflect new license status
                        setTimeout(function() {
                            window.location.reload();
                        }, 1500);
                    } else {
                        var errorMsg = response.data || '<?php echo esc_js( __( 'Invalid license key or activation failed.', 'roidnet-ai-content-generator' ) ); ?>';
                        showMessage(errorMsg, 'error');
                        $activateBtn.prop('disabled', false).html(
                            '<span class="dashicons dashicons-unlock" style="vertical-align: middle; margin-right: 3px;"></span> ' +
                            '<?php echo esc_js( __( 'Activate License', 'roidnet-ai-content-generator' ) ); ?>'
                        );
                    }
                }).fail(function() {
                    showMessage('<?php echo esc_js( __( 'Connection error. Please try again.', 'roidnet-ai-content-generator' ) ); ?>', 'error');
                    $activateBtn.prop('disabled', false).html(
                        '<span class="dashicons dashicons-unlock" style="vertical-align: middle; margin-right: 3px;"></span> ' +
                        '<?php echo esc_js( __( 'Activate License', 'roidnet-ai-content-generator' ) ); ?>'
                    );
                });
            });

            // Allow Enter key to trigger activation
            $licenseKey.on('keypress', function(e) {
                if (e.which === 13) {
                    e.preventDefault();
                    $activateBtn.trigger('click');
                }
            });

            function showMessage(text, type) {
                var color = type === 'success' ? '#46b450' : '#dc3232';
                var icon = type === 'success' ? 'dashicons-yes-alt' : 'dashicons-dismiss';
                $message.html(
                    '<span class="dashicons ' + icon + '" style="color: ' + color + '; vertical-align: middle;"></span> ' +
                    '<span style="color: ' + color + ';">' + text + '</span>'
                ).show();
            }
        });
        </script>

        <?php endif; ?>
    </div>
</div>
