<?php
/**
 * AIPKit Global Settings Module
 */

use WPAICG\AIPKIT_AI_Settings;
use WPAICG\AIPKit_Providers;
use WPAICG\aipkit_dashboard;
use WPAICG\AIPKit_Role_Manager;
use WPAICG\Core\Providers\Google\GoogleSettingsHandler;
use WPAICG\Images\AIPKit_Image_Settings_Ajax_Handler;

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- This file only uses local helper/template variables and does not define public globals.

// Ensure GoogleSettingsHandler is loaded before use
$google_settings_handler_path = WPAICG_PLUGIN_DIR . 'classes/core/providers/google/bootstrap-provider-strategy.php';
if (!class_exists(GoogleSettingsHandler::class) && file_exists($google_settings_handler_path)) {
    require_once $google_settings_handler_path;
} elseif (!class_exists(GoogleSettingsHandler::class)) {
    echo '<div class="notice notice-error"><p>Error: Google Settings component failed to load. Safety settings cannot be displayed.</p></div>';
}

// --- Variable Definitions ---
$aipkit_options = get_option('aipkit_options', array());
$aipkit_options = is_array($aipkit_options) ? $aipkit_options : [];

// Force the "providers" array to exist
AIPKit_Providers::get_all_providers();

$ai_params = AIPKIT_AI_Settings::get_ai_parameters();
$all_api_keys = AIPKIT_AI_Settings::get_api_keys();
$public_api_key = $all_api_keys['public_api_key'] ?? '';

$is_pro = class_exists('\WPAICG\aipkit_dashboard') && aipkit_dashboard::is_pro_plan();
$module_settings = aipkit_dashboard::get_module_settings();
$can_manage_modules = AIPKit_Role_Manager::user_can_manage_settings();

$safety_settings = class_exists(GoogleSettingsHandler::class) ? GoogleSettingsHandler::get_safety_settings() : [];
$category_thresholds = array();
if (is_array($safety_settings)) {
    foreach ($safety_settings as $setting) {
        if (isset($setting['category'], $setting['threshold'])) {
            $category_thresholds[$setting['category']] = $setting['threshold'];
        }
    }
}

$current_provider = AIPKit_Providers::get_current_provider();

$openai_data     = AIPKit_Providers::get_provider_data('OpenAI');
$openrouter_data = AIPKit_Providers::get_provider_data('OpenRouter');
$google_data     = AIPKit_Providers::get_provider_data('Google');
$azure_data      = AIPKit_Providers::get_provider_data('Azure');
$claude_data     = AIPKit_Providers::get_provider_data('Claude');
$deepseek_data   = AIPKit_Providers::get_provider_data('DeepSeek');
$xai_data        = AIPKit_Providers::get_provider_data('xAI');
$ollama_data     = AIPKit_Providers::get_provider_data('Ollama');
$elevenlabs_data = AIPKit_Providers::get_provider_data('ElevenLabs');
$replicate_data  = AIPKit_Providers::get_provider_data('Replicate');
$pexels_data     = AIPKit_Providers::get_provider_data('Pexels');
$pixabay_data    = AIPKit_Providers::get_provider_data('Pixabay');
$pinecone_data   = AIPKit_Providers::get_provider_data('Pinecone');
$qdrant_data     = AIPKit_Providers::get_provider_data('Qdrant');
$chroma_data     = AIPKit_Providers::get_provider_data('Chroma');

$elevenlabs_voice_list = AIPKit_Providers::get_elevenlabs_voices();
$elevenlabs_model_list = AIPKit_Providers::get_elevenlabs_models();
$replicate_model_list  = AIPKit_Providers::get_replicate_models();
$pinecone_index_list   = AIPKit_Providers::get_pinecone_indexes();
$qdrant_collection_list = AIPKit_Providers::get_qdrant_collections();
$chroma_collection_list = AIPKit_Providers::get_chroma_collections();
$image_generator_settings = class_exists(AIPKit_Image_Settings_Ajax_Handler::class)
    ? AIPKit_Image_Settings_Ajax_Handler::get_settings()
    : [];
$replicate_image_settings = isset($image_generator_settings['replicate']) && is_array($image_generator_settings['replicate'])
    ? $image_generator_settings['replicate']
    : [];
$replicate_disable_safety_checker = array_key_exists('disable_safety_checker', $replicate_image_settings)
    ? (bool) $replicate_image_settings['disable_safety_checker']
    : true;

$temperature       = $ai_params['temperature'];
$top_p             = $ai_params['top_p'];
$openai_store_conversation = isset($openai_data['store_conversation']) ? $openai_data['store_conversation'] : '0';

$safety_thresholds = array(
    'BLOCK_NONE'             => 'Block None',
    'BLOCK_LOW_AND_ABOVE'    => 'Block Few',
    'BLOCK_MEDIUM_AND_ABOVE' => 'Block Some',
    'BLOCK_ONLY_HIGH'        => 'Block Most',
);

$openai_defaults     = AIPKit_Providers::get_provider_defaults('OpenAI');
$openrouter_defaults = AIPKit_Providers::get_provider_defaults('OpenRouter');
$google_defaults     = AIPKit_Providers::get_provider_defaults('Google');
$azure_defaults      = AIPKit_Providers::get_provider_defaults('Azure');
$claude_defaults     = AIPKit_Providers::get_provider_defaults('Claude');
$deepseek_defaults   = AIPKit_Providers::get_provider_defaults('DeepSeek');
$xai_defaults        = AIPKit_Providers::get_provider_defaults('xAI');
$ollama_defaults     = AIPKit_Providers::get_provider_defaults('Ollama');
$pinecone_defaults   = AIPKit_Providers::get_provider_defaults('Pinecone');
$qdrant_defaults     = AIPKit_Providers::get_provider_defaults('Qdrant');
$chroma_defaults     = AIPKit_Providers::get_provider_defaults('Chroma');

$providers = ['OpenAI', 'Google', 'Claude', 'OpenRouter', 'Azure', 'Ollama', 'DeepSeek', 'xAI'];
$provider_select_options = class_exists('\\WPAICG\\AIPKit_Provider_Model_List_Builder')
    ? \WPAICG\AIPKit_Provider_Model_List_Builder::get_provider_options($providers, $is_pro)
    : [];
?>
<div class="aipkit_container aipkit_settings_main_container" id="aipkit_settings_container">
    <div class="aipkit_container-header">
        <div class="aipkit_container-header-left">
            <div class="aipkit_settings_header_copy">
                <div class="aipkit_settings_header_title_row">
                    <h2 class="aipkit_container-title"><?php esc_html_e('Settings', 'gpt3-ai-content-generator'); ?></h2>
                    <div
                        id="aipkit_settings_global_messages"
                        class="aipkit_settings_messages aipkit_global_status_area aipkit_settings_header_status"
                        role="status"
                        aria-live="polite"
                    ></div>
                </div>
                <p class="aipkit_settings_header_hint"><?php esc_html_e('Configure AI defaults, integrations, apps, security, developer tools, and maintenance.', 'gpt3-ai-content-generator'); ?></p>
            </div>
        </div>
    </div>
    <div class="aipkit_container-body">
    <section class="aipkit_settings_simple_panel aipkit_settings_pages_shell">
        <div class="aipkit_settings_page_nav_row">
            <nav
                class="aipkit_settings_page_nav"
                role="tablist"
                aria-label="<?php esc_attr_e('Settings sections', 'gpt3-ai-content-generator'); ?>"
            >
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link is-active aipkit_active"
                    id="aipkit_settings_page_tab_ai"
                    role="tab"
                    aria-selected="true"
                    aria-controls="aipkit_settings_page_panel_ai"
                    data-aipkit-settings-page-link="ai"
                >
                    <?php esc_html_e('AI', 'gpt3-ai-content-generator'); ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_modules"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_modules"
                    data-aipkit-settings-page-link="modules"
                    tabindex="-1"
                >
                    <?php esc_html_e('Modules', 'gpt3-ai-content-generator'); ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_integrations"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_integrations"
                    data-aipkit-settings-page-link="integrations"
                    tabindex="-1"
                >
                    <?php esc_html_e('Integrations', 'gpt3-ai-content-generator'); ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_apps"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_apps"
                    data-aipkit-settings-page-link="apps"
                    tabindex="-1"
                >
                    <span class="aipkit_settings_page_nav_link_label"><?php esc_html_e('Apps', 'gpt3-ai-content-generator'); ?></span>
                    <?php if (!$is_pro) : ?>
                        <span class="aipkit_settings_apps_upsell_badge"><?php esc_html_e('Pro', 'gpt3-ai-content-generator'); ?></span>
                    <?php endif; ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_utilities"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_utilities"
                    data-aipkit-settings-page-link="utilities"
                    tabindex="-1"
                >
                    <?php esc_html_e('Utilities', 'gpt3-ai-content-generator'); ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_security"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_security"
                    data-aipkit-settings-page-link="security"
                    tabindex="-1"
                >
                    <?php esc_html_e('Security', 'gpt3-ai-content-generator'); ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_api"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_api"
                    data-aipkit-settings-page-link="api"
                    tabindex="-1"
                >
                    <?php esc_html_e('Developers', 'gpt3-ai-content-generator'); ?>
                </button>
                <button
                    type="button"
                    class="aipkit_settings_page_nav_link"
                    id="aipkit_settings_page_tab_others"
                    role="tab"
                    aria-selected="false"
                    aria-controls="aipkit_settings_page_panel_others"
                    data-aipkit-settings-page-link="others"
                    tabindex="-1"
                >
                    <?php esc_html_e('Others', 'gpt3-ai-content-generator'); ?>
                </button>
            </nav>
        </div>

        <div class="aipkit_settings_pages">
            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_ai"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_ai"
                data-aipkit-settings-page="ai"
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('AI Settings', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Set your default provider, model, API key, and advanced options.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--ai" id="aipkit_settings_provider_panel">
                    <?php include __DIR__ . '/partials/settings-provider-select.php'; ?>
                    <?php include __DIR__ . '/partials/settings-api-keys.php'; ?>
                    <?php include __DIR__ . '/partials/settings-models.php'; ?>
                    <?php include __DIR__ . '/partials/settings-advanced-panel.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_integrations"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_integrations"
                data-aipkit-settings-page="integrations"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Integrations', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Manage provider credentials and sync controls for connected services.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--integrations">
                    <?php include __DIR__ . '/partials/settings-integrations-page.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_security"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_security"
                data-aipkit-settings-page="security"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Security Settings', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Manage global word and IP blocklists shared across supported modules.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--security">
                    <?php include __DIR__ . '/partials/settings-security-page.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_api"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_api"
                data-aipkit-settings-page="api"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Developer Settings', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Manage REST API access and outbound event webhook endpoints for developer workflows.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--api">
                    <?php include __DIR__ . '/partials/settings-api-page.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_apps"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_apps"
                data-aipkit-settings-page="apps"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Apps', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Manage reusable app connections and event-driven recipe automations.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--apps">
                    <?php include __DIR__ . '/partials/settings-apps-page.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_modules"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_modules"
                data-aipkit-settings-page="modules"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Modules', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Choose which AI Puffer tools appear in the top navigation.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--modules">
                    <?php include __DIR__ . '/partials/settings-modules-page.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_utilities"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_utilities"
                data-aipkit-settings-page="utilities"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Utilities', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Manage AI Puffer tools that appear inside WordPress editors and content lists.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--utilities">
                    <?php include __DIR__ . '/partials/settings-utilities-page.php'; ?>
                </div>
            </section>

            <section
                class="aipkit_settings_page_section"
                id="aipkit_settings_page_panel_others"
                role="tabpanel"
                aria-labelledby="aipkit_settings_page_tab_others"
                data-aipkit-settings-page="others"
                hidden
            >
                <header class="aipkit_settings_page_header">
                    <h3 class="aipkit_settings_page_title"><?php esc_html_e('Other Settings', 'gpt3-ai-content-generator'); ?></h3>
                    <p class="aipkit_settings_page_helper"><?php esc_html_e('Manage backups and maintenance actions.', 'gpt3-ai-content-generator'); ?></p>
                </header>

                <div class="aipkit_settings_simple_form aipkit_settings_simple_form--others">
                    <?php include __DIR__ . '/partials/settings-others-page.php'; ?>
                </div>
            </section>
        </div>
    </section>
    </div>
</div>

<div id="aipkit_google_tts_voices_json_main" style="display:none;" data-voices="<?php
    $google_voices_main = class_exists(GoogleSettingsHandler::class) ? GoogleSettingsHandler::get_synced_google_tts_voices() : [];
echo esc_attr(wp_json_encode($google_voices_main ?: []));
?>"></div>
