<?php
/**
 * Admin settings page template
 *
 * @package ContentReply_AI
 */

if ( ! defined( 'ABSPATH' ) ) exit;

$wpsc_indexer = new WPSC_Indexer();
$wpsc_chunk_count = $wpsc_indexer->get_index_count();
$wpsc_last_indexed = get_option( 'wpsc_last_indexed', '' );
?>
<div class="wrap wpsc-wrap">
    <h1><?php esc_html_e( 'ContentReply AI - Settings', 'contentreply-ai' ); ?></h1>

    <?php settings_errors(); ?>

    <?php if ( empty( $wpsc_last_indexed ) || $wpsc_chunk_count === 0 ) : ?>
    <div class="notice notice-warning" style="padding:12px 16px;border-left-color:#f59e0b;">
        <strong><?php esc_html_e( 'Action Required:', 'contentreply-ai' ); ?></strong>
        <?php esc_html_e( 'Your site content has not been indexed yet. Click "Index Content Now" below to build the knowledge base for the chatbot.', 'contentreply-ai' ); ?>
    </div>
    <?php endif; ?>

    <div class="wpsc-status-bar">
        <span class="wpsc-status-item">
            <strong><?php esc_html_e( 'Indexed Chunks:', 'contentreply-ai' ); ?></strong>
            <span id="wpsc-chunk-count"><?php echo esc_html( $wpsc_chunk_count ); ?></span>
        </span>
        <span class="wpsc-status-item">
            <strong><?php esc_html_e( 'Last Indexed:', 'contentreply-ai' ); ?></strong>
            <?php echo esc_html( $wpsc_last_indexed ? $wpsc_last_indexed : 'Never' ); ?>
        </span>
        <button id="wpsc-reindex-btn" class="button button-primary">
            <?php echo esc_html( $wpsc_chunk_count > 0 ? 'Re-index Content' : 'Index Content Now' ); ?>
        </button>
        <span id="wpsc-reindex-status"></span>
    </div>

    <form method="post" action="options.php">
        <?php settings_fields( 'wpsc_settings' ); ?>

        <table class="form-table">
            <!-- API Configuration -->
            <tr>
                <th colspan="2"><h2><?php esc_html_e( 'AI Configuration', 'contentreply-ai' ); ?></h2></th>
            </tr>
            <tr>
                <th><label for="wpsc_ai_provider"><?php esc_html_e( 'AI Provider', 'contentreply-ai' ); ?></label></th>
                <td>
                    <select id="wpsc_ai_provider" name="wpsc_ai_provider">
                        <option value="groq" <?php selected( get_option( 'wpsc_ai_provider', 'openai' ), 'groq' ); ?>>Groq (Free - No credit card needed)</option>
                        <option value="gemini" <?php selected( get_option( 'wpsc_ai_provider', 'openai' ), 'gemini' ); ?>>Google Gemini (Free tier available)</option>
                        <option value="openai" <?php selected( get_option( 'wpsc_ai_provider', 'openai' ), 'openai' ); ?>>OpenAI (Paid)</option>
                    </select>
                    <p class="description"><?php esc_html_e( 'Groq is completely free with no credit card required. Recommended for testing.', 'contentreply-ai' ); ?></p>
                </td>
            </tr>
            <tr>
                <th><label for="wpsc_api_key"><?php esc_html_e( 'API Key', 'contentreply-ai' ); ?></label></th>
                <td>
                    <input type="password" id="wpsc_api_key" name="wpsc_api_key" value="<?php echo esc_attr( get_option( 'wpsc_api_key' ) ); ?>" class="regular-text" />
                    <p class="description" id="wpsc-api-key-help">
                        <span class="wpsc-help-groq" <?php echo get_option( 'wpsc_ai_provider', 'openai' ) !== 'groq' ? 'style="display:none;"' : ''; ?>>
                            <?php esc_html_e( 'Get your free API key from', 'contentreply-ai' ); ?> <a href="https://console.groq.com/keys" target="_blank">console.groq.com/keys</a>
                        </span>
                        <span class="wpsc-help-gemini" <?php echo get_option( 'wpsc_ai_provider', 'openai' ) !== 'gemini' ? 'style="display:none;"' : ''; ?>>
                            <?php esc_html_e( 'Get your free API key from', 'contentreply-ai' ); ?> <a href="https://aistudio.google.com/apikey" target="_blank">aistudio.google.com/apikey</a>
                        </span>
                        <span class="wpsc-help-openai" <?php echo get_option( 'wpsc_ai_provider', 'openai' ) !== 'openai' ? 'style="display:none;"' : ''; ?>>
                            <?php esc_html_e( 'Get your API key from', 'contentreply-ai' ); ?> <a href="https://platform.openai.com/api-keys" target="_blank">platform.openai.com</a>
                        </span>
                    </p>
                </td>
            </tr>
            <tr>
                <th><label for="wpsc_ai_model"><?php esc_html_e( 'AI Model', 'contentreply-ai' ); ?></label></th>
                <td>
                    <select id="wpsc_ai_model" name="wpsc_ai_model">
                        <optgroup label="Groq (Free)" class="wpsc-models-groq">
                            <option value="llama-3.3-70b-versatile" <?php selected( get_option( 'wpsc_ai_model' ), 'llama-3.3-70b-versatile' ); ?>>Llama 3.3 70B (Recommended - Best Free)</option>
                            <option value="llama-3.1-8b-instant" <?php selected( get_option( 'wpsc_ai_model' ), 'llama-3.1-8b-instant' ); ?>>Llama 3.1 8B (Fastest)</option>
                            <option value="mixtral-8x7b-32768" <?php selected( get_option( 'wpsc_ai_model' ), 'mixtral-8x7b-32768' ); ?>>Mixtral 8x7B (Good Balance)</option>
                        </optgroup>
                        <optgroup label="Google Gemini" class="wpsc-models-gemini">
                            <option value="gemini-2.0-flash" <?php selected( get_option( 'wpsc_ai_model' ), 'gemini-2.0-flash' ); ?>>Gemini 2.0 Flash (Fast & Free)</option>
                            <option value="gemini-1.5-flash" <?php selected( get_option( 'wpsc_ai_model' ), 'gemini-1.5-flash' ); ?>>Gemini 1.5 Flash (Fast & Free)</option>
                            <option value="gemini-1.5-pro" <?php selected( get_option( 'wpsc_ai_model' ), 'gemini-1.5-pro' ); ?>>Gemini 1.5 Pro (Best Quality - Free tier)</option>
                        </optgroup>
                        <optgroup label="OpenAI (Paid)" class="wpsc-models-openai">
                            <option value="gpt-4o-mini" <?php selected( get_option( 'wpsc_ai_model' ), 'gpt-4o-mini' ); ?>>GPT-4o Mini (Fast & Cheap)</option>
                            <option value="gpt-4o" <?php selected( get_option( 'wpsc_ai_model' ), 'gpt-4o' ); ?>>GPT-4o (Best Quality)</option>
                            <option value="gpt-3.5-turbo" <?php selected( get_option( 'wpsc_ai_model' ), 'gpt-3.5-turbo' ); ?>>GPT-3.5 Turbo (Cheapest)</option>
                        </optgroup>
                    </select>
                </td>
            </tr>
            <tr>
                <th><label for="wpsc_max_tokens"><?php esc_html_e( 'Max Response Tokens', 'contentreply-ai' ); ?></label></th>
                <td>
                    <input type="number" id="wpsc_max_tokens" name="wpsc_max_tokens" value="<?php echo esc_attr( get_option( 'wpsc_max_tokens', 500 ) ); ?>" min="100" max="2000" />
                    <p class="description"><?php esc_html_e( 'Higher = longer responses but more API cost.', 'contentreply-ai' ); ?></p>
                </td>
            </tr>

            <!-- Widget Appearance -->
            <tr>
                <th colspan="2"><h2><?php esc_html_e( 'Widget Appearance', 'contentreply-ai' ); ?></h2></th>
            </tr>
            <tr>
                <th><label for="wpsc_bot_name"><?php esc_html_e( 'Bot Name', 'contentreply-ai' ); ?></label></th>
                <td><input type="text" id="wpsc_bot_name" name="wpsc_bot_name" value="<?php echo esc_attr( get_option( 'wpsc_bot_name', 'Support Bot' ) ); ?>" class="regular-text" /></td>
            </tr>
            <tr>
                <th><label for="wpsc_welcome_message"><?php esc_html_e( 'Welcome Message', 'contentreply-ai' ); ?></label></th>
                <td><input type="text" id="wpsc_welcome_message" name="wpsc_welcome_message" value="<?php echo esc_attr( get_option( 'wpsc_welcome_message' ) ); ?>" class="large-text" /></td>
            </tr>
            <tr>
                <th><label for="wpsc_primary_color"><?php esc_html_e( 'Primary Color', 'contentreply-ai' ); ?></label></th>
                <td><input type="color" id="wpsc_primary_color" name="wpsc_primary_color" value="<?php echo esc_attr( get_option( 'wpsc_primary_color', '#2e7d32' ) ); ?>" /></td>
            </tr>
            <tr>
                <th><label for="wpsc_widget_position"><?php esc_html_e( 'Widget Position', 'contentreply-ai' ); ?></label></th>
                <td>
                    <select id="wpsc_widget_position" name="wpsc_widget_position">
                        <option value="bottom-right" <?php selected( get_option( 'wpsc_widget_position' ), 'bottom-right' ); ?>>Bottom Right</option>
                        <option value="bottom-left" <?php selected( get_option( 'wpsc_widget_position' ), 'bottom-left' ); ?>>Bottom Left</option>
                    </select>
                </td>
            </tr>

            <!-- Content & Behavior -->
            <tr>
                <th colspan="2"><h2><?php esc_html_e( 'Content & Behavior', 'contentreply-ai' ); ?></h2></th>
            </tr>
            <tr>
                <th><label for="wpsc_fallback_message"><?php esc_html_e( 'Fallback Message', 'contentreply-ai' ); ?></label></th>
                <td><input type="text" id="wpsc_fallback_message" name="wpsc_fallback_message" value="<?php echo esc_attr( get_option( 'wpsc_fallback_message' ) ); ?>" class="large-text" />
                <p class="description"><?php esc_html_e( 'Shown when AI cannot find an answer.', 'contentreply-ai' ); ?></p></td>
            </tr>
            <tr>
                <th><label for="wpsc_contact_url"><?php esc_html_e( 'Contact Page URL', 'contentreply-ai' ); ?></label></th>
                <td><input type="text" id="wpsc_contact_url" name="wpsc_contact_url" value="<?php echo esc_attr( get_option( 'wpsc_contact_url', '/contact' ) ); ?>" class="regular-text" /></td>
            </tr>
            <tr>
                <th><label for="wpsc_auto_index"><?php esc_html_e( 'Auto Re-index', 'contentreply-ai' ); ?></label></th>
                <td>
                    <select id="wpsc_auto_index" name="wpsc_auto_index">
                        <option value="disabled" <?php selected( get_option( 'wpsc_auto_index', 'disabled' ), 'disabled' ); ?>><?php esc_html_e( 'Disabled (Manual only)', 'contentreply-ai' ); ?></option>
                        <option value="daily" <?php selected( get_option( 'wpsc_auto_index', 'disabled' ), 'daily' ); ?>><?php esc_html_e( 'Daily', 'contentreply-ai' ); ?></option>
                        <option value="weekly" <?php selected( get_option( 'wpsc_auto_index', 'disabled' ), 'weekly' ); ?>><?php esc_html_e( 'Weekly', 'contentreply-ai' ); ?></option>
                    </select>
                    <p class="description"><?php esc_html_e( 'Automatically re-index site content on a schedule. Useful if you update content frequently.', 'contentreply-ai' ); ?></p>
                </td>
            </tr>
            <tr>
                <th><label><?php esc_html_e( 'Index Post Types', 'contentreply-ai' ); ?></label></th>
                <td>
                    <?php
                    $wpsc_selected_types = get_option( 'wpsc_post_types', array( 'page', 'post' ) );
                    $wpsc_all_types = get_post_types( array( 'public' => true ), 'objects' );
                    foreach ( $wpsc_all_types as $type ) :
                    ?>
                    <label style="display:block;margin-bottom:6px;">
                        <input type="checkbox" name="wpsc_post_types[]" value="<?php echo esc_attr( $type->name ); ?>"
                            <?php checked( in_array( $type->name, (array) $wpsc_selected_types ) ); ?> />
                        <?php echo esc_html( $type->labels->singular_name ); ?>
                    </label>
                    <?php endforeach; ?>
                </td>
            </tr>
            <tr>
                <th><label for="wpsc_show_on_mobile"><?php esc_html_e( 'Show on Mobile', 'contentreply-ai' ); ?></label></th>
                <td><input type="checkbox" id="wpsc_show_on_mobile" name="wpsc_show_on_mobile" value="1" <?php checked( get_option( 'wpsc_show_on_mobile', true ) ); ?> /></td>
            </tr>
            <tr>
                <th><label for="wpsc_enabled"><?php esc_html_e( 'Enable Chatbot', 'contentreply-ai' ); ?></label></th>
                <td><input type="checkbox" id="wpsc_enabled" name="wpsc_enabled" value="1" <?php checked( get_option( 'wpsc_enabled', true ) ); ?> /></td>
            </tr>
            <tr>
                <th><label for="wpsc_demo_mode"><?php esc_html_e( 'Demo Mode', 'contentreply-ai' ); ?></label></th>
                <td>
                    <input type="checkbox" id="wpsc_demo_mode" name="wpsc_demo_mode" value="1" <?php checked( get_option( 'wpsc_demo_mode', false ) ); ?> />
                    <p class="description"><?php esc_html_e( 'Enable demo mode to test the widget without an API key. Bot will return sample responses.', 'contentreply-ai' ); ?></p>
                </td>
            </tr>
        </table>

        <?php submit_button(); ?>
    </form>
</div>
