<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly

if(!class_exists('qcld_wpopenai_addons')){


    /**
     * Main Class.
     */
    final class qcld_wpopenai_addons
    {
        private $id = 'Open AI';

        /**
         * WoowbotPro version.
         *
         * @var string
         */
        public $version = '1.0.6';
        
        /**
         * WoowbotPro helper.
         *
         * @var object
         */
        public $helper;

        /**
         * The single instance of the class.
         *
         * @var qcld_wb_Chatbot
         * @since 1.0.0
         */
        protected static $_instance = null;
        
        /**
         * Main WoowbotInstance.
         *
         * Ensures only one instance of Woowbotis loaded or can be loaded.
         *
         * @return qcld_wb_Chatbot - Main instance.
         * @since 1.0.0
         * @static
         */
        public static function instance() {
            if ( is_null( self::$_instance ) ) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        public $response_list;

        /**
         *  Constructor
         */
        public function __construct()
        {
            $this->define_constants();
            $this->includes();
            add_action('wp_ajax_openai_settings_option', [$this, 'openai_settings_option_callback']);
            add_action('wp_ajax_update_settings_option', [$this, 'qcld_update_settings_option_callback']);
            add_action('wp_ajax_qcld_rag_settings_option', array($this, 'rag_settings_option_callback'));
            add_action('wp_ajax_qcld_openai_response',[$this,'qcld_openai_response_callback']);
            add_action('wp_ajax_nopriv_qcld_openai_response', [$this, 'qcld_openai_response_callback']);
            add_action('wp_ajax_qcld_stream_openai', [$this, 'qcld_stream_openai_callback']);
            add_action('wp_ajax_nopriv_qcld_stream_openai', [$this, 'qcld_stream_openai_callback']);
            add_action('wp_ajax_wpbot_wizard_save', array($this, 'wpbot_wizard_save_callback'));
            add_action('wp_ajax_wpbot_wizard_verify_key', array($this, 'wpbot_wizard_verify_key_callback'));

            if( (get_option('enable_product_details_from_ai') == 1) && (get_option('qcld_openai_enabled') == 1) && (get_option('qcld_open_ai_api_key') != '' ) ){
                 add_action('wp_ajax_qcld_woo_get_product_details', [$this, 'qcld_woo_get_product_details']);
                 add_action('wp_ajax_nopriv_qcld_woo_get_product_details', [$this, 'qcld_woo_get_product_details']);
            }
            add_action('wp_ajax_openai_troubleshooting',[$this,'openai_troubleshooting']);
            if (is_admin() && !empty($_GET["page"]) && (($_GET["page"] == "openai-panel_dashboard") || ($_GET["page"] == "openai-panel_file") || ($_GET["page"] == "openai-panel_help"))) {
                add_action('admin_enqueue_scripts', array($this, 'qcld_wb_chatbot_admin_scripts'));
            }
     
        }

        
        /**
         * Define WoowbotConstants.
         *
         * @return void
         * @since 1.0.0
         */
        public function define_constants() {
            if( ! defined( 'QCLD_openai_addon_VERSION' ) ){
                define('QCLD_openai_addon_VERSION', $this->version);
            }
           //define('QCLD_openai_addon_REQUIRED_wpCOMMERCE_VERSION', 2.2);

            if( ! defined( 'QCLD_openai_addon_PLUGIN_DIR_PATH' ) ){
                define('QCLD_openai_addon_PLUGIN_DIR_PATH', plugin_dir_path(__FILE__));
            }
            if( ! defined( 'QCLD_openai_addon_PLUGIN_URL' ) ){
                define('QCLD_openai_addon_PLUGIN_URL', plugin_dir_url(__FILE__));
            }
            if( ! defined( 'QCLD_openai_addon_IMG_URL' ) ){
                define('QCLD_openai_addon_IMG_URL', QCLD_openai_addon_PLUGIN_URL . "images/");
            }
            if( ! defined( 'QCLD_openai_addon_IMG_ABSOLUTE_PATH' ) ){
                define('QCLD_openai_addon_IMG_ABSOLUTE_PATH', plugin_dir_path(__FILE__) . "images");
            }

        }


        public function qcld_wb_chatbot_admin_scripts(){
            // wp_register_style('qlcd-open-ai-bootstap', QCLD_openai_addon_PLUGIN_URL . 'css/openai-bootstrap.css', '', QCLD_openai_addon_VERSION, 'screen');
            // wp_enqueue_style('qlcd-open-ai-bootstap');
            // wp_register_style('qlcd-open-ai-admin-style', QCLD_openai_addon_PLUGIN_URL . 'css/openai-admin-style.css', '', QCLD_openai_addon_VERSION, 'screen');
            // wp_enqueue_style('qlcd-open-ai-admin-style');
            // wp_register_script('qlcd-openai_collapse', QCLD_openai_addon_PLUGIN_URL . 'js/collapse.js', array('jquery'),'',QCLD_openai_addon_VERSION,true);
            // wp_enqueue_script('qlcd-openai_collapse');
            // wp_register_script('qlcd-openai_settings', QCLD_openai_addon_PLUGIN_URL . 'js/openai_settings.js', array('jquery'),'',QCLD_openai_addon_VERSION,true);
            // wp_enqueue_script('qlcd-openai_settings');
            
            // wp_localize_script( 'qlcd-openai_settings', 'openai_ajax', array(
            //     'url' => admin_url( 'admin-ajax.php' ),
            // ) );
            
        }
        /**
         * Include all required files
         *
         * since 1.0.0
         *
         * @return void
         */
        public function includes() {
            require_once( QCLD_WOOCHATBOT_PLUGIN_DIR_FULL_PATH . "includes/ai_integration/openai/qcld_wp_OpenAI.php" );
            require_once( QCLD_WOOCHATBOT_PLUGIN_DIR_FULL_PATH . "includes/ai_integration/openai/OpenAi_WPBot_Menu.php" );
            require_once( QCLD_WOOCHATBOT_PLUGIN_DIR_FULL_PATH . "includes/Parsedown.php" );
            
        }
  
      
        public function buildFormBody( $fields, $boundary )
        {
            $body = '';
            foreach ( $fields as $name => $value ) {
            if ( $name == 'data' ) {
                continue;
            }
            $body .= "--$boundary\r\n";
            $body .= "Content-Disposition: form-data; name=\"$name\"";
            if ( $name == 'file' ) {
                $body .= "; filename=\"{$value}\"\r\n";
                $body .= "Content-Type: application/json\r\n\r\n";
                $body .= $fields['data'] . "\r\n";
            }else {
                $body .= "\r\n\r\n$value\r\n";
            }
            }
            $body .= "--$boundary--\r\n";
            return $body;
        }


        public function qcld_sanitize_text_or_array_field($array_or_string) {
            if( is_string($array_or_string) ){
                $array_or_string = sanitize_text_field($array_or_string);
            }elseif( is_array($array_or_string) ){
                foreach ( $array_or_string as $key => &$value ) {
                    if ( is_array( $value ) ) {
                        $value = $this->qcld_sanitize_text_or_array_field($value);
                    }
                    else {
                        $value = sanitize_text_field( $value );
                    }
                }
            }

            return $array_or_string;
        }

        public function relevant_pagelink($search_query){
			
			$stopwords = explode( ',', get_option('qlcd_wp_chatbot_stop_words') );

            $finalQueryWordsWithoutStopWords = $this->qcpd_remove_wa_stopwords( strtolower($search_query), $stopwords );

            $cleanWordsWithoutPunctuationMarks = preg_replace('/[\p{P}]/u', '', $finalQueryWordsWithoutStopWords);

            $q = trim($cleanWordsWithoutPunctuationMarks);
        
            $links = [];
			
            $post_type_array = get_option('qcld_openai_relevant_post');
        
            //Proceeding with traditional search
        
                $the_query = new WP_Query( array( 'post_status' => 'publish', 'posts_per_page' => 5, 's' => esc_attr( $q ), 'post_type' => $post_type_array ) );
        
                if( $the_query->have_posts() ){
        
                    while( $the_query->have_posts() ){ 
        
                        $the_query->the_post();
        
                        $url  = esc_url( get_permalink() );
        
                        $link = '<li><mark><a style="color: #000" href=' . $url . '>' . get_the_title() . '</a><mark></li>';
        
                        array_push($links, $link);
        
                    } //End of WHILE
        
                    wp_reset_postdata();  
        
                } //End of IF
        
            $links = array_unique($links);
        
            return $links;
            
        } 
       
        public function response_form_file($keyword){
            $max_tokens =  (int)get_option( 'openai_max_tokens');
            $temp = (float)get_option( 'openai_temperature');
            $frequency_penalty = (float)get_option( 'frequency_penalty');
            $presence_penalty = (float)get_option( 'presence_penalty');
            $engines = explode('-',get_option( 'openai_engines'));
            if($engines[0] != 'gpt'){
               // $prompts = $this->get_prompt($keyword);
            }
         
            $request_body = [
                "prompt" =>   $keyword,
                "model" => get_option( 'qcld_openai_custom_model'),
                "max_tokens" => $max_tokens,
                "temperature" => 0,
                "top_p" => 1,
                "stop" => [], 
                "presence_penalty" => 0,
                "frequency_penalty"=> 0,
                "best_of"=> 1,
            ];
            $postFields = json_encode($request_body);
            $OpenAI =  new qcld_wp_OpenAI();
            $result = $OpenAI->get_response($postFields);

            return $result;
        }
        public function get_prompt($keyword){
          $openai_include_keyword =  get_option( 'openai_include_keyword'); 
          $openai_exclude_keyword = get_option( 'openai_exclude_keyword'); 
          $qcld_openai_prompt = get_option('qcld_openai_prompt',true);
      
        }
        public function include_exclude_prompt($keyword){
            $openai_include_keyword = strtolower(get_option('openai_include_keyword'));
            $openai_exclude_keyword = strtolower(get_option('openai_exclude_keyword'));
           
            if((get_option('openai_include_keyword')  != '') || (get_option('openai_exclude_keyword')  == '')){
                $prompts    = 'If the query is not relevant  to one of the keywords: '.$openai_include_keyword .' then only say DUH. Provide a response only if the following query is relevant to one of the keywords: '.$openai_include_keyword .' The actual query is as follows: '. $keyword;
                return $prompts;
            }else if((get_option('openai_include_keyword')  == '') || (get_option('openai_exclude_keyword')  != '')){
                
                $prompts = 'If the query is relevant to one of the keywords: ' .$openai_exclude_keyword . ',  then do not respond and only say "DUH."   The actual query is as follows: '. $keyword. '?/n';
                return $prompts;
            }else if((get_option('openai_include_keyword')  != '') || (get_option('openai_exclude_keyword')  != '')){
                $prompts    = 'If the query is not relevant  to one of the keywords: '.$openai_include_keyword .' then only say "DUH." Provide a response only if the following query is relevant to one of the keywords: '.$openai_include_keyword .' The actual query is as follows: '. $keyword;
                return $prompts;
            }
        }
        public function qcld_include_keyword_exist( $keyword ){
            $keyword = isset($keyword) ? $keyword : '';
            $openai_include_keywords = strtolower(get_option('openai_include_keyword'));
            if(!empty($keyword)){
                $openai_include_keyword = ( isset( $openai_include_keywords ) ?  $openai_include_keywords : '');
    
                if( !empty($openai_include_keyword)){
                    $include_items = explode(',', $openai_include_keyword);
                    if(!empty($include_items)){
                        foreach($include_items as $k => $item){
                            if((strpos($keyword,trim($item)) !== false) && !empty($item)){
                                return true;
                            }
                        }
                    }
                    return false;
                }
            }
        
            return false;
    
        }

        public function qcld_openai_response_callback() {
              if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'wp_chatbot' ) ) {
                
                    wp_send_json_error([
                        'status'  => 'error',
                        'message' => esc_html__( 'Security check failed. Unauthorized request.', 'woowbot-woocommerce-chatbot' )
                    ]);
                    wp_die();
                }
                $response['status'] = 'success';
                $response['message'] ='A preset message';
                $OpenAI =  new qcld_wp_OpenAI();
                $gptkeyword = [];
                $keyword = sanitize_text_field($_POST['keyword']);
                $relevant_pagelink = $this->relevant_pagelink($keyword);

                // Build context-aware system instructions
                $system_content = get_option('qcld_openai_system_content');

               
                // RAG Integration
                if (get_option('is_page_rag_enabled') == '1') {
                    $rag_context_text = Qcld_Bot_Rag::instance()->run_rag_search($keyword);
                    if (!empty($rag_context_text) && $rag_context_text != "No knowledge base found.") {
                        if( get_option( 'qcldai_product_card_enable' ) == 1 ) {
                            $rag_context = "Relevant Knowledge Base Information:\n";
                            $rag_context .= $rag_context_text;
                            $rag_context .= "\n\nUse the above information to answer the user's question. If the answer is not in the Knowledge Base, rely on your general knowledge but mention that this information is not in the local knowledge base. Also, if the user query seems to be about a product, retrun the product ID in the following format: [woowbot_product_id:123] where 123 are the relevant product IDs.don't show the product ID as a lebel, only return the product ID in the specified format if the query is about a product.";
                            $system_content .= "\n\n" . $rag_context;
                        }else{
                            $rag_context = "Relevant Knowledge Base Information:\n";
                            $rag_context .= $rag_context_text;
                            $rag_context .= "\n\nUse the above information to answer the user's question. If the answer is not in the Knowledge Base, rely on your general knowledge but mention that this information is not in the local knowledge base.";
                            $system_content .= "\n\n" . $rag_context;

                        }
                    }
                }

                if ( get_option('context_awareness_enabled') == '1' ) {
                    $site_name = get_bloginfo('name');
                    $site_desc = get_bloginfo('description');
                    
                    // Get current page URL and title more reliably
                    $current_url = '';
                    $page_title = '';
                    $page_summary = '';
                    
                    // Try to get from referrer first
                    $ref = wp_get_referer();
                    if ( ! $ref && isset($_SERVER['HTTP_REFERER']) ) {
                        $ref = esc_url_raw( $_SERVER['HTTP_REFERER'] );
                    }
                    
                    if ( $ref ) {
                        $current_url = $ref;
                        
                        // Try to get post/page by URL
                        $post_id = url_to_postid( $ref );
                        if ( $post_id ) {
                            $page_title = get_the_title( $post_id );
                            $raw_content = get_post_field( 'post_content', $post_id );
                            $text_content = wp_strip_all_tags( $raw_content );
                            $page_summary = wp_trim_words( $text_content, 120, '…' );
                        } else {
                            // If not a post/page, try to extract title from URL or use current page
                            $parsed_url = parse_url( $ref );
                            if ( isset($parsed_url['path']) ) {
                                $path = trim($parsed_url['path'], '/');
                                if ( ! empty($path) ) {
                                    // Try to get title from current page if we're on it
                                    if ( is_singular() ) {
                                        $page_title = get_the_title();
                                        $raw_content = get_the_content();
                                        $text_content = wp_strip_all_tags( $raw_content );
                                        $page_summary = wp_trim_words( $text_content, 120, '…' );
                                    } elseif ( is_archive() ) {
                                        $page_title = get_the_archive_title();
                                    } elseif ( is_search() ) {
                                        $page_title = 'Search Results';
                                    } elseif ( is_404() ) {
                                        $page_title = 'Page Not Found';
                                    }
                                }
                            }
                        }
                    } else {
                        // Fallback to current page info.
                        $scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
                        $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
                        $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
                        $current_url = esc_url_raw($scheme . '://' . $host . $request_uri);
                        
                        if ( is_singular() ) {
                            $page_title = get_the_title();
                            $raw_content = get_the_content();
                            $text_content = wp_strip_all_tags( $raw_content );
                            $page_summary = wp_trim_words( $text_content, 120, '…' );
                        } elseif ( is_archive() ) {
                            $page_title = get_the_archive_title();
                        } elseif ( is_search() ) {
                            $page_title = 'Search Results';
                        } elseif ( is_404() ) {
                            $page_title = 'Page Not Found';
                        }
                    }

                    $context_bits = array();
                    if ( $site_name ) { $context_bits[] = 'Site: ' . $site_name; }
                    if ( $site_desc ) { $context_bits[] = 'Tagline: ' . $site_desc; }
                    if ( $page_title ) { $context_bits[] = 'Page title: ' . $page_title; }
                    if ( $current_url ) { $context_bits[] = 'URL: ' . $current_url; }
                    if ( $page_summary ) { $context_bits[] = 'Page summary: ' . $page_summary; }

                    if ( ! empty( $context_bits ) ) {
                        $context_info = 'Context Information: ' . implode( '. ', $context_bits ) . '. Please use this context to provide more relevant and accurate responses.';
                        $system_content = $system_content . "\n\n" . $context_info;
                    }
                }

                $relevant_pagelink = array_slice($relevant_pagelink, 0, 5, true);

                if( (get_option('page_suggestion_enabled') == '1') && count($relevant_pagelink) > 0 ){
					
                    $relevant_post_link = get_option('qlcd_wp_chatbot_relevant_post_link_openai');
                    
                    if(is_array($relevant_post_link )){
                        $relevant_pagelinks = '<br><br><p><em>'. implode('', $relevant_post_link) .'</em></p><ul style="list-style: disc;padding-left: 10px;">'. implode(" ", $relevant_pagelink). '</ul>';
                   }else{
                        $relevant_pagelinks = '<br><br><p><em>'. $relevant_post_link .'</em></p><ul style="list-style: disc;padding-left: 10px;">'. implode(" ", $relevant_pagelink) .'</ul>';
                   }
                }else{
                    $relevant_pagelinks = '';
                }
              

                        array_push( $gptkeyword, array(
                            "role" => "system",
                            "content" =>   $system_content
                        ));
                        array_push($gptkeyword, array(
                            "role" => "user",
                            "content" =>  $keyword
                        ));
                        if(((get_option('openai_include_keyword')  != '') ||  (get_option('openai_exclude_keyword')  != '')) && (get_option('qcld_openai_relevant_enabled') == '1') ){
                            $prompts =  $this->include_exclude_prompt($keyword);
                        
                            $gptkeyword = [];
                            array_push($gptkeyword, array(
                                "role" => "user",
                                "content" =>  $prompts,
                            ));
                        }else if(((get_option('openai_include_keyword')  != '') ||  (get_option('openai_exclude_keyword')  != '')) && (get_option('qcld_openai_relevant_enabled') == '0')){
                            if($this->qcld_include_keyword_exist($keyword) == false){
                            
                                $response['message'] = 'Sorry, No result found!';
                                echo wp_json_encode($response);
                                wp_die();
                            }else{
                                array_push($gptkeyword, array(
                                    "role" => "user",
                                    "content" =>  $keyword
                                ));
                            }
                            
                        }
                        $res = $OpenAI->gptcomplete(
                            $gptkeyword
                        );   
                        $mess = json_decode($res); 
                        $Qcld_Parsedown = new Qcld_Parsedown();
                        $msg = $mess->output[0]->content[0]->text;
                        if( $msg == null || empty($msg) ){
                            $msg = $mess->output[1]->content[0]->text;
                        }
                        $msg = $Qcld_Parsedown->text($msg);
                  
                        $response['message'] = $msg ;
                        if(($response['message'] == 'DUH.') || ($response['message'] == 'DUH')){
                            $response['message'] = 'Sorry, No result found!';
                        }else{
                            $Qcld_Parsedown = new Qcld_Parsedown();
                            $msg = $mess->output[0]->content[0]->text;
                            if( $msg == null || empty($msg) ){
                                $msg = $mess->output[1]->content[0]->text;
                            }
                            $msg = $Qcld_Parsedown->text($msg);
                            // Replace product placeholders like [woowbot_product_id:123,456]
                            $msg = $this->replace_product_placeholders($msg);
                            $response['message'] = $msg . $relevant_pagelinks;
                        }
                do_action('qcld_openai_user_rate_cal', 1);
                echo wp_json_encode($response);
                wp_die();
            //}
        }
        public function openai_settings_option_callback() {
		    $nonce =  sanitize_text_field($_POST['nonce']);

            if (! wp_verify_nonce($nonce,'ajax_object')) {
                wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'woowbot-woocommerce-chatbot')));
                wp_die();

            }else{
               
                $api_key = sanitize_text_field($_POST['api_key']);
                $openai_engines = sanitize_text_field($_POST['openai_engines']);

                $qcld_openai_prompt =  isset( $_POST['qcld_openai_prompt'] ) ? sanitize_text_field($_POST['qcld_openai_prompt']) : '';
                

                $max_tokens = sanitize_text_field($_POST['max_tokens']);
                $qcld_openai_suffix = (!empty($_POST['qcld_openai_suffix'])) ? sanitize_text_field($_POST['qcld_openai_suffix']) : '';

                $qcld_openai_custom_model = isset( $_POST['qcld_openai_custom_model'] ) ?  sanitize_text_field($_POST['qcld_openai_custom_model']) : '';

                

                $frequency_penalty = sanitize_text_field($_POST['frequency_penalty']);
                $presence_penalty = sanitize_text_field($_POST['presence_penalty']);
                $temperature = sanitize_text_field($_POST['temperature']);
                $qcld_openai_enabled = sanitize_text_field($_POST['qcld_openai_enabled']);
                
                $suggestion_enabled = sanitize_text_field($_POST['is_page_suggestion_enabled']);
                $context_awareness_enabled = sanitize_text_field($_POST['is_context_awareness_enabled']);
                $is_page_rag_enabled = sanitize_text_field($_POST['is_page_rag_enabled']);
                $is_product_card_enabled = sanitize_text_field($_POST['is_product_card_enabled']);


                $is_relevant_enabled = sanitize_text_field($_POST['is_relevant_enabled']);
                $file_id = (!empty($_POST['file_id'])) ? sanitize_text_field($_POST['file_id']) : '';

                $qcld_openai_prompt_custom = isset( $_POST['qcld_openai_prompt_custom'] ) ? sanitize_text_field($_POST['qcld_openai_prompt_custom']) : '';
           
                $openai_post_types = array();
                if (isset($_POST['openai_post_type'])) {
                    $raw_post_types = wp_unslash($_POST['openai_post_type']);
                    if (is_array($raw_post_types)) {
                        $openai_post_types = array_map('sanitize_text_field', $raw_post_types);
                    } else {
                        $openai_post_types = sanitize_text_field($raw_post_types);
                    }
                }
                update_option('qcld_openai_relevant_post', $openai_post_types);
                
                $conversation_continuity = sanitize_text_field($_POST['conversation_continuity']);
				$qcld_openai_system_content = sanitize_text_field($_POST['qcld_openai_system_content']);
                $qcld_openai_append_content = sanitize_text_field($_POST['qcld_openai_append_content']);

				/* Customized by Kadir on 05-12-2023 : To set empty value for API field */
                $disable_ss = isset( $_POST['disable_ss'] ) ? sanitize_text_field( $_POST['disable_ss'] ) : ''; 
                $is_stream_enabled = isset($_POST['is_stream_enabled']) ? sanitize_text_field(wp_unslash($_POST['is_stream_enabled'])) : '0';
                update_option('qcld_openai_stream_enabled', $is_stream_enabled);
                if($api_key  != ''){
                    update_option( 'qcld_open_ai_api_key', $api_key );
                }
                else{
                    delete_option( 'qcld_open_ai_api_key');
                }
                
                /* Ends: Customized by Kadir on 05-12-2023 : To set empty value for API field */
				
                if($openai_engines  != ''){
                    update_option( 'openai_engines', $openai_engines );
                }
                if($conversation_continuity  != ''){
                    update_option( 'conversation_continuity', $conversation_continuity );
                }
                update_option( 'openai_max_tokens', $max_tokens );
                
                if($qcld_openai_suffix != ''){
                update_option('qcld_openai_suffix', $qcld_openai_suffix);
                }
                if($frequency_penalty  != ''){
                update_option( 'frequency_penalty', $frequency_penalty );
                }
                if($presence_penalty  != ''){
                    update_option( 'presence_penalty', $presence_penalty );
                }
                if($temperature  != ''){
                    update_option( 'openai_temperature', $temperature );
                }
                if($qcld_openai_prompt_custom  != ''){
                    update_option('qcld_openai_prompt_custom', $qcld_openai_prompt_custom );
                }
                update_option('qcld_openai_custom_model',$qcld_openai_custom_model);
                update_option( 'qcld_openai_system_content', stripslashes( $qcld_openai_system_content) );
                update_option( 'qcld_openai_append_content', stripslashes( $qcld_openai_append_content) );
                update_option( 'qcldai_product_card_enable', $is_product_card_enabled );
                update_option( 'qcld_openai_enabled',$qcld_openai_enabled );
                if( $qcld_openai_enabled == 1 ){

                    update_option('qcld_openrouter_enabled',0);
                    update_option('qcld_gemini_enabled',0);
                }
               
                update_option('qcld_openai_relevant_enabled',$is_relevant_enabled);
                update_option('page_suggestion_enabled',$suggestion_enabled);
                update_option('context_awareness_enabled',$context_awareness_enabled);
                update_option('is_page_rag_enabled',$is_page_rag_enabled);


                if($file_id  != ''){
                    update_option('file_id',$file_id);
                }
                $openai_include_keyword = sanitize_text_field($_POST['openai_include_keyword']);
                update_option('openai_include_keyword',$openai_include_keyword);
                $openai_exclude_keyword = sanitize_text_field($_POST['openai_exclude_keyword']);
                update_option('openai_exclude_keyword',$openai_exclude_keyword);
				
				
				/* Customized by Kadir on 05-12-2023 : To Disable Site Search*/
                //Disable Site Search
                if( $disable_ss == 1 ){
                    update_option('disable_wp_chatbot_site_search',1);
                    update_option('enable_wp_chatbot_post_content', '');
                }
                /* Ends: Customized by Kadir on 05-12-2023 : To Disable Site Search*/
                if($qcld_openai_prompt != ''){
                    update_option('qcld_openai_prompt', $qcld_openai_prompt);
                }
            }
               $OpenAI = new qcld_wp_OpenAI();
				$gptkeyword = array();
				array_push(
					$gptkeyword,
					array(
						'role'    => 'user',
						'content' => 'Is our request comes from openAI ?',
					)
				);
				$res = $OpenAI->gptcomplete(
					$gptkeyword
				);

				if ( empty( json_decode( $res )->error ) ) {
					$mess = json_decode( $res );
					$msg  = preg_replace( "/\r\n|\r|\n/", '<br/>', $mess->output[0]->content[0]->text );
					wp_send_json(
						array(
							'success' => true,
							'title'   => esc_html__( 'success', 'woowbot-woocommerce-chatbot' ),
							'icon'    => esc_html__( 'success', 'woowbot-woocommerce-chatbot' ),
							'msg'     => esc_html( $msg ),
						)
					);
				} else {

					wp_send_json(
						array(
							'success' => true,
							'title'   => esc_html__( 'Error', 'woowbot-woocommerce-chatbot' ),
							'icon'    => esc_html__( 'error', 'woowbot-woocommerce-chatbot' ),
							'msg'     => esc_html( json_decode( $res )->error->message ),
						)
					);
				}
            
             //   echo wp_json_encode($ai_enabled);wp_die();
            
        }
        public function rag_settings_option_callback()
		{
            // Security: verify nonce (CSRF protection) and capability (authorization).
            $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
            if ( ! wp_verify_nonce( $nonce, 'wp_chatbot' ) || ! current_user_can( 'manage_options' ) ) {
                wp_send_json_error( array( 'message' => esc_html__( 'Security check failed', 'woowbot-woocommerce-chatbot' ) ) );
                wp_die();
            }
            if( (get_option('is_page_rag_enabled') == '1' && get_option('qcld_open_ai_api_key')) || (get_option('qcld_gemini_rag_enabled') == '1' && get_option('qcld_gemini_api_key'))){
                $rag_embed_pages = sanitize_text_field($_POST['rag_embed_pages'] ?? 0);
                $rag_embed_posts = sanitize_text_field($_POST['rag_embed_posts'] ?? 0);
                $rag_embed_str = sanitize_text_field($_POST['rag_embed_str'] ?? 0);
                $rag_auto_sync_enabled = sanitize_text_field($_POST['rag_auto_sync_enabled'] ?? 0);
            
                $rag_embed_cpts = isset($_POST['rag_embed_cpts']) ? $_POST['rag_embed_cpts'] : [];
                if(is_array($rag_embed_cpts)){
                    $rag_embed_cpts = array_map('sanitize_text_field', $rag_embed_cpts);
                }

                if (isset($_POST['is_page_rag_enabled'])) {
                    $is_rag_enabled = sanitize_text_field($_POST['is_page_rag_enabled']);
                    update_option('is_page_rag_enabled', $is_rag_enabled);
                    if($is_rag_enabled == 1){
                        update_option('is_asst_enabled', 0);
                    }
                }

                update_option('rag_embed_pages', $rag_embed_pages);
                update_option('rag_embed_str', $rag_embed_str);
                update_option('rag_embed_posts', $rag_embed_posts);
                update_option('rag_auto_sync_enabled', $rag_auto_sync_enabled);
                update_option('rag_embed_cpts', $rag_embed_cpts);

                echo json_encode(array('status' => 'success'));
                wp_die();
            }else{
                if( !get_option('qcld_open_ai_api_key') || !get_option('qcld_gemini_api_key') ){
                    wp_send_json_success(array('status' => 'error', 'message' => esc_html__('RAG cannot be enabled without an API key.', 'woowbot-woocommerce-chatbot')));
                }else if( get_option('is_page_rag_enabled') != '1' && get_option('qcld_gemini_rag_enabled') != '1' ){
                    wp_send_json_success(array('status' => 'error', 'message' => esc_html__('Please enable RAG in settings to save RAG related options.', 'woowbot-woocommerce-chatbot')));
                } 
                wp_die(); 
            }
		}
        public function qcld_update_settings_option_callback(){
            // Verify nonce for CSRF protection
            $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
            if (!wp_verify_nonce($nonce, 'ajax_object')) {
                wp_send_json_error(array('message' => esc_html__('Security check failed', 'woowbot-woocommerce-chatbot')));
                wp_die();
            }
        
            // Check user capability - only administrators can modify settings
            if (!current_user_can('manage_options')) {
                wp_send_json_error(array('message' => esc_html__('Unauthorized access', 'woowbot-woocommerce-chatbot')));
                wp_die();
            }
            
            // Proceed with option updates
            update_option('disable_wp_chatbot_site_search', 1);
            update_option('enable_wp_chatbot_post_content', '');
            
            // Send success response
            wp_send_json_success(array('message' => esc_html__('Settings updated successfully', 'woowbot-woocommerce-chatbot')));
            wp_die();
        }
        public function qcpd_remove_wa_stopwords($query, $stopwords){
			
            return preg_replace('/\b('.implode('|',$stopwords).')\b/','',$query);
			
        }

        /**
         * Replace product placeholders produced by AI with product HTML.
         * Pattern: [woowbot_product_id:123] or [woowbot_product_id:123,456]
         */
        public function replace_product_placeholders($msg){
            if (strpos($msg, '[woowbot_product_id:') === false) {
                return $msg;
            }
            if (preg_match_all('/\[woowbot_product_id:([0-9,\s]+)\]/', $msg, $matches)) {
                foreach ($matches[0] as $i => $placeholder) {
                    $ids_str = $matches[1][$i];
                    $ids = array_map('intval', array_filter(array_map('trim', explode(',', $ids_str))));
                    if (empty($ids)) continue;
                    $html = '<div class="woo-chatbot-featured-products"><ul class="woo-chatbot-products">';
                    foreach ($ids as $id) {
                        $product = wc_get_product($id);
                        if (!$product) continue;
                        $image = get_the_post_thumbnail($id, 'shop_catalog');
                        if (empty($image)) {
                            $image = woocommerce_placeholder_img('shop_catalog');
                        }
                        $html .= '<li class="woo-chatbot-product">';
                        $html .= '<a target="_blank" href="' . get_permalink($id) . '" title="' . esc_attr($product->get_name()) . '">';
                        $html .= $image . '</a><div class="woo-chatbot-product-summary"><div class="woo-chatbot-product-table"><div class="woo-chatbot-product-table-cell">';
                        $html .= '<h3 class="woo-chatbot-product-title"><a target="_blank" href="' . get_permalink($id) . '">' . esc_attr($product->get_name()) . '</a></h3>';
                        $html .= '<div class="price">' . $product->get_price_html() . '</div>';
                        if ($product->is_type('simple')) {
                            $add_to_cart_url = esc_url( add_query_arg('add-to-cart', $id, home_url()) );
                            $html .= '<a target="_blank" href="' . $add_to_cart_url . '" class="woo-chatbot-button woo-chatbot-button-cart add_to_cart_button ajax_add_to_cart" data-quantity="1" data-product_id="' . $id . '">' . esc_html__('Add to Cart','woowbot-woocommerce-chatbot') . '</a>';
                        } else {
                            $html .= '<a target="_blank" href="' . get_permalink($id) . '" class="woo-chatbot-button woo-chatbot-button-cart">' . esc_html__('View Detail','woowbot-woocommerce-chatbot') . '</a>';
                        }
                        $html .= '</div></div></div></li>';
                    }
                    $html .= '</ul></div>';
                    $msg = str_replace($placeholder, $html, $msg);
                }
            }
            return $msg;
        }
		public function openai_troubleshooting() {
			$nonce  = sanitize_text_field( $_POST['nonce'] );
			$OpenAI = new qcld_wp_OpenAI();
			if ( ! wp_verify_nonce( $nonce, 'ajax_object' ) ) {
				wp_send_json(
					array(
						'success' => false,
						'msg'     => esc_html__( 'Failed in Security check', 'woowbot-woocommerce-chatbot' ),
					)
				);
				wp_die();

			} elseif ( ! current_user_can( 'manage_options' ) ) {
				wp_send_json(
					array(
						'success' => false,
						'msg'     => esc_html__( 'Unauthorized user', 'woowbot-woocommerce-chatbot' ),
					)
				);
				wp_die();
			} else {
				$gptkeyword = array();
				array_push(
					$gptkeyword,
					array(
						'role'    => 'user',
						'content' => 'If you get this query respond in plain text: "Congrats! You are connected to AI." Otherwise, respond with "Connection to AI failed."',
					)
				);
				$res = $OpenAI->gptcomplete(
					$gptkeyword
				);
               
				if ( empty( json_decode( $res )->error ) ) {
					$mess = json_decode( $res );
                    if(isset($mess->output[0]->content[0]->text)){
					    $msg  = preg_replace( "/\r\n|\r|\n/", '<br/>', $mess->output[0]->content[0]->text );
                    }else{
                        $msg = preg_replace( "/\r\n|\r|\n/", '<br/>', $mess->output[1]->content[0]->text );;
                    }
					wp_send_json(
						array(
							'success' => true,
							'title'   => esc_html__( 'success', 'woowbot-woocommerce-chatbot' ),
							'icon'    => esc_html__( 'success', 'woowbot-woocommerce-chatbot' ),
							'msg'     => esc_html( $msg ),
						)
					);
				} else {

					wp_send_json(
						array(
							'success' => true,
							'title'   => esc_html__( 'Error', 'woowbot-woocommerce-chatbot' ),
							'icon'    => esc_html__( 'error', 'woowbot-woocommerce-chatbot' ),
							'msg'     => esc_html( json_decode( $res )->error->message ),
						)
					);
				}
			}
		}
        public function qcld_woo_get_product_details() {
            $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
            if (!wp_verify_nonce($nonce, 'wp_chatbot')) {
                wp_send_json_error(array('message' => esc_html__('Security check failed', 'woowbot-woocommerce-chatbot')));
                wp_die();
            }
            $product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
            if ($product_id <= 0) {
                wp_send_json_error(array('message' => esc_html__('Invalid product ID', 'woowbot-woocommerce-chatbot')));
                wp_die();
            }
            $product = wc_get_product($product_id);
            if (!$product) {
                wp_send_json_error(array('message' => esc_html__('Product not found', 'woowbot-woocommerce-chatbot')));
                wp_die();
            }
            $response['status'] = 'success';
                $response['message'] ='A preset message';
                $OpenAI =  new qcld_wp_OpenAI();
                $gptkeyword = [];
                $keyword = sanitize_text_field($product->get_name());

                // Build context-aware system instructions
                $system_content = get_option('qcld_openai_system_content');
                
                // RAG Integration
                if (get_option('is_page_rag_enabled') == '1') {
                    $rag_context_text = Qcld_Bot_Rag::instance()->run_rag_search($keyword);
                    if (!empty($rag_context_text) && $rag_context_text != "No knowledge base found.") {
                         $rag_context = "Relevant Knowledge Base Information:\n";
                         $rag_context .= $rag_context_text;
                         $rag_context .= "\n\nUse the above information to answer the user's question. If the answer is not in the Knowledge Base, rely on your general knowledge but mention that this information is not in the local knowledge base.";
                         $system_content .= "\n\n" . $rag_context;
                    }
                }

                if ( get_option('context_awareness_enabled') == '1' ) {
                    $site_name = get_bloginfo('name');
                    $site_desc = get_bloginfo('description');
                    
                    // Get current page URL and title more reliably
                    $current_url = '';
                    $page_title = '';
                    $page_summary = '';
                    
                    // Try to get from referrer first
                    $ref = wp_get_referer();
                    if ( ! $ref && isset($_SERVER['HTTP_REFERER']) ) {
                        $ref = esc_url_raw( $_SERVER['HTTP_REFERER'] );
                    }
                    
                    if ( $ref ) {
                        $current_url = $ref;
                        
                        // Try to get post/page by URL
                        $post_id = url_to_postid( $ref );
                        if ( $post_id ) {
                            $page_title = get_the_title( $post_id );
                            $raw_content = get_post_field( 'post_content', $post_id );
                            $text_content = wp_strip_all_tags( $raw_content );
                            $page_summary = wp_trim_words( $text_content, 120, '…' );
                        } else {
                            // If not a post/page, try to extract title from URL or use current page
                            $parsed_url = parse_url( $ref );
                            if ( isset($parsed_url['path']) ) {
                                $path = trim($parsed_url['path'], '/');
                                if ( ! empty($path) ) {
                                    // Try to get title from current page if we're on it
                                    if ( is_singular() ) {
                                        $page_title = get_the_title();
                                        $raw_content = get_the_content();
                                        $text_content = wp_strip_all_tags( $raw_content );
                                        $page_summary = wp_trim_words( $text_content, 120, '…' );
                                    } elseif ( is_archive() ) {
                                        $page_title = get_the_archive_title();
                                    } elseif ( is_search() ) {
                                        $page_title = 'Search Results';
                                    } elseif ( is_404() ) {
                                        $page_title = 'Page Not Found';
                                    }
                                }
                            }
                        }
                    } else {
                        // Fallback to current page info.
                        $scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
                        $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
                        $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
                        $current_url = esc_url_raw($scheme . '://' . $host . $request_uri);
                        
                        if ( is_singular() ) {
                            $page_title = get_the_title();
                            $raw_content = get_the_content();
                            $text_content = wp_strip_all_tags( $raw_content );
                            $page_summary = wp_trim_words( $text_content, 120, '…' );
                        } elseif ( is_archive() ) {
                            $page_title = get_the_archive_title();
                        } elseif ( is_search() ) {
                            $page_title = 'Search Results';
                        } elseif ( is_404() ) {
                            $page_title = 'Page Not Found';
                        }
                    }

                    $context_bits = array();
                    if ( $site_name ) { $context_bits[] = 'Site: ' . $site_name; }
                    if ( $site_desc ) { $context_bits[] = 'Tagline: ' . $site_desc; }
                    if ( $page_title ) { $context_bits[] = 'Page title: ' . $page_title; }
                    if ( $current_url ) { $context_bits[] = 'URL: ' . $current_url; }
                    if ( $page_summary ) { $context_bits[] = 'Page summary: ' . $page_summary; }

                    if ( ! empty( $context_bits ) ) {
                        $context_info = 'Context Information: ' . implode( '. ', $context_bits ) . '. Please use this context to provide more relevant and accurate responses.';
                        $system_content = $system_content . "\n\n" . $context_info;
                    }
                }
                array_push( $gptkeyword, array(
                    "role" => "system",
                    "content" =>   $system_content
                ));
                array_push($gptkeyword, array(
                    "role" => "user",
                    "content" =>  $keyword
                ));
                
                $res = $OpenAI->gptcomplete(
                    $gptkeyword
                );   
                $mess = json_decode($res); 
                $Qcld_Parsedown = new Qcld_Parsedown();
                $msg = $mess->output[0]->content[0]->text;
                if( $msg == null || empty($msg) ){
                    $msg = $mess->output[1]->content[0]->text;
                }
                $msg = $Qcld_Parsedown->text($msg);
            
                $response['message'] = $msg ;
                if(($response['message'] == 'DUH.') || ($response['message'] == 'DUH')){
                    $response['message'] = 'Sorry, No result found!';
                }else{
                    $Qcld_Parsedown = new Qcld_Parsedown();
                    $msg = $mess->output[0]->content[0]->text;
                    if( $msg == null || empty($msg) ){
                        $msg = $mess->output[1]->content[0]->text;
                    }
                    $msg = $Qcld_Parsedown->text($msg);
                    $response['message'] = $msg;
                }
                do_action('qcld_openai_user_rate_cal', 1);
                echo wp_json_encode($response);
                wp_die();
         
        
            // wp_send_json_success(array('data' => $product_data));
            // wp_die();
        }
        public function qcld_stream_openai_callback() {

            if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wp_chatbot' ) ) {
                echo "data: [ERROR] Security check failed.\n\n";
                flush();
                wp_die();
            }
            if ( get_option( 'is_rate_limiting_enabled' ) == '1' ) {
                do_action( 'rate_limit_checker' );
            }

            if ( function_exists( 'apache_setenv' ) ) {
                @apache_setenv( 'no-gzip', 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
            }
            @ini_set( 'zlib.output_compression', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
            @ini_set( 'implicit_flush', 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
            while ( ob_get_level() ) {
                ob_end_clean();
            }
            ob_implicit_flush( 1 );
            header( 'Content-Type: text/event-stream' );
            header( 'Cache-Control: no-cache' );
            header( 'Connection: keep-alive' );

            $api_key = trim( get_option( 'qcld_open_ai_api_key' ) );
            $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';

            if ( empty( $api_key ) || empty( $keyword ) ) {
                echo "data: [ERROR] Missing API key or message.\n\n";
                flush();
                wp_die();
            }

            $system_content = get_option( 'qcld_openai_system_content', 'You are a helpful assistant.' );

            // RAG integration
            if ( get_option( 'is_page_rag_enabled' ) == '1' ) {
                if ( class_exists( 'Qcld_Bot_Rag' ) ) {
                    $rag_context_text = Qcld_Bot_Rag::instance()->run_rag_search( $keyword );
                    if ( ! empty( $rag_context_text ) && $rag_context_text !== 'No knowledge base found.' ) {
                        $system_content .= "\n\nRelevant Knowledge Base:\n" . $rag_context_text;
                    }
                }
            }

            // Context awareness
            if ( get_option( 'context_awareness_enabled' ) == '1' ) {
                $site_name = get_bloginfo( 'name' );
                $site_desc = get_bloginfo( 'description' );
                $context_bits = [];
                if ( $site_name ) { $context_bits[] = 'Site: ' . $site_name; }
                if ( $site_desc ) { $context_bits[] = 'Tagline: ' . $site_desc; }
                $ref = wp_get_referer();
                if ( ! $ref && isset( $_SERVER['HTTP_REFERER'] ) ) {
                    $ref = esc_url_raw( $_SERVER['HTTP_REFERER'] );
                }
                if ( $ref ) {
                    $context_bits[] = 'URL: ' . $ref;
                    $post_id = url_to_postid( $ref );
                    if ( $post_id ) {
                        $context_bits[] = 'Page title: ' . get_the_title( $post_id );
                    }
                }
                if ( ! empty( $context_bits ) ) {
                    $system_content .= "\n\nContext: " . implode( '. ', $context_bits );
                }
            }

            $model = get_option( 'qcld_openai_custom_model' );
            if ( empty( $model ) ) {
                $model = get_option( 'openai_engines', 'gpt-4o' );
            }

            $messages = [
                [ 'role' => 'system', 'content' => $system_content ],
                [ 'role' => 'user',   'content' => $keyword ],
            ];

            $headers = [
                'Content-Type: application/json',
                'Authorization: Bearer ' . $api_key,
            ];

            $post_data = wp_json_encode( [
                'model'    => $model,
                'messages' => $messages,
                'stream'   => true,
            ] );
            $ch = curl_init( 'https://api.openai.com/v1/chat/completions' );
            curl_setopt( $ch, CURLOPT_POST, true );
            curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data );
            curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
            curl_setopt( $ch, CURLOPT_WRITEFUNCTION, function ( $ch, $chunk ) {
                echo $chunk; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                echo "\n: " . str_repeat( ' ', 1024 ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                flush();
                return strlen( $chunk );
            } );
            curl_exec( $ch );
            if ( curl_errno( $ch ) ) {
                echo 'data: [ERROR] ' . esc_html( curl_error( $ch ) ) . "\n\n";
                flush();
            }
            curl_close( $ch );
            do_action( 'qcld_openai_user_rate_cal', 1 );
            exit;
        }
        public function wpbot_wizard_save_callback()
		{
			$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';

			if (! wp_verify_nonce($nonce, 'wp_chatbot')) {
				wp_send_json_error(esc_html__('Failed in Security check', 'woowbot-woocommerce-chatbot'));
				wp_die();
			}

			if (!current_user_can('manage_options')) {
				wp_send_json_error(esc_html__('Insufficient permissions', 'woowbot-woocommerce-chatbot'));
				wp_die();
			}

			$is_skipped = isset($_POST['is_skipped']) ? intval($_POST['is_skipped']) : 0;
			
			if ($is_skipped) {
				wp_send_json_success();
				wp_die();
			}

			// 1. Save AI Provider and API key
			$provider = isset($_POST['ai_provider']) ? sanitize_text_field($_POST['ai_provider']) : '';
			$api_key  = isset($_POST['api_key']) ? sanitize_text_field($_POST['api_key']) : '';

			if ($provider === 'openai') {
				update_option('qcld_open_ai_api_key', $api_key);
			} elseif ($provider === 'gemini') {
				update_option('qcld_gemini_api_key', $api_key);
			} 

			// 2. Enable AI for selected provider automatically
			// Reset all providers first
			update_option('ai_enabled', 0);
			update_option('qcld_gemini_enabled', 0);
			update_option('qcld_grok_enabled', 0);
			update_option('qcld_openrouter_enabled', 0);
			update_option('enable_wp_chatbot_dailogflow', 0);

			if ($provider === 'openai') {
				update_option('qcld_openai_enabled', 1);
			} elseif ($provider === 'gemini') {
				update_option('qcld_gemini_enabled', 1);
			} 

			// 3. Auto-enable Streaming by default
			update_option('is_stream_enabled', 1);

			// 4. Auto-enable RAG by default (globally & for provider-specific config)
			update_option('is_page_rag_enabled', 1);
			update_option('qcld_gemini_rag_enabled', ($provider === 'gemini') ? 1 : 0);
			update_option('qcld_grok_rag_enabled', ($provider === 'grok') ? 1 : 0);
			update_option('qcld_openrouter_rag_enabled', ($provider === 'openrouter') ? 1 : 0);

			// 5. Knowledge Base settings (default Top K to 5)
			update_option('rag_top_k', 5);

			// Auto-initialize system content defaults if empty
			if (empty(get_option('qcld_openai_system_content'))) {
				update_option('qcld_openai_system_content', 'You are a helpful and intelligent assistant for a WordPress chatbot. Always reply to the user query accurately, clearly, and briefly.');
			}
			if (empty(get_option('qcld_gemini_system_content'))) {
				update_option('qcld_gemini_system_content', 'You are a helpful assistant.');
			}
			if (empty(get_option('qcld_grok_system_content'))) {
				update_option('qcld_grok_system_content', 'You are a helpful and intelligent assistant for the website "' . site_url() . '". Use live website data and the provided context to respond accurately and briefly. Stay relevant and do not introduce additional topics.');
			}

			// 6. Embed sources
			$rag_embed_pages = isset($_POST['rag_embed_pages']) ? intval($_POST['rag_embed_pages']) : 0;
			update_option('rag_embed_pages', $rag_embed_pages);

			$rag_embed_posts = isset($_POST['rag_embed_posts']) ? intval($_POST['rag_embed_posts']) : 0;
			update_option('rag_embed_posts', $rag_embed_posts);

			$rag_embed_cpts = isset($_POST['rag_embed_cpts']) ? array_map('sanitize_text_field', $_POST['rag_embed_cpts']) : array();
			update_option('rag_embed_cpts', $rag_embed_cpts);

			// Mark wizard as completed permanently in DB
			update_option('wpbot_ai_setup_wizard_done', 1);

			wp_send_json_success(esc_html__('AI Setup configuration saved successfully.', 'woowbot-woocommerce-chatbot'));
			wp_die();
		}

		public function wpbot_wizard_verify_key_callback()
		{
			$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';

			if (! wp_verify_nonce($nonce, 'wp_chatbot')) {
				wp_send_json_error(esc_html__('Failed in Security check', 'woowbot-woocommerce-chatbot'));
				wp_die();
			}

			if (!current_user_can('manage_options')) {
				wp_send_json_error(esc_html__('Insufficient permissions', 'woowbot-woocommerce-chatbot'));
				wp_die();
			}

			$provider = isset($_POST['ai_provider']) ? sanitize_text_field($_POST['ai_provider']) : '';
			$api_key  = isset($_POST['api_key']) ? sanitize_text_field($_POST['api_key']) : '';

			if ($provider === 'openai') {
				$url = 'https://api.openai.com/v1/models';
				$args = array(
					'headers' => array(
						'Authorization' => 'Bearer ' . $api_key,
					),
					'timeout' => 15,
				);
			} elseif ($provider === 'gemini') {
				$url = 'https://generativelanguage.googleapis.com/v1beta/models?key=' . $api_key;
				$args = array(
					'timeout' => 15,
				);
			} elseif ($provider === 'grok') {
				$url = 'https://api.x.ai/v1/models';
				$args = array(
					'headers' => array(
						'Authorization' => 'Bearer ' . $api_key,
					),
					'timeout' => 15,
				);
			} elseif ($provider === 'openrouter') {
				$url = 'https://openrouter.ai/api/v1/models';
				$args = array(
					'headers' => array(
						'Authorization' => 'Bearer ' . $api_key,
					),
					'timeout' => 15,
				);
			}

			$response = wp_remote_get($url, $args);

			if (is_wp_error($response)) {
				wp_send_json_error($response->get_error_message());
				wp_die();
			}

			$code = wp_remote_retrieve_response_code($response);
			if ($code === 200) {
				wp_send_json_success();
			} else {
				$body = wp_remote_retrieve_body($response);
				$data = json_decode($body, true);
				$msg = '';
				if (isset($data['error']['message'])) {
					$msg = $data['error']['message'];
				} elseif (isset($data['error']['metadata']['message'])) {
					$msg = $data['error']['metadata']['message'];
				} elseif (isset($data['error'])) {
					$msg = is_string($data['error']) ? $data['error'] : json_encode($data['error']);
				} else {
					$msg = 'HTTP Status ' . $code;
				}
				wp_send_json_error($msg);
			}
			wp_die();
		}
      

    }

    /**
     * @return qcld_wpopenai_addon
     */
    if(!function_exists('qcld_wpopenai_addons')){
        function qcld_openais() {
            $qcld_wpopenai_addon = new qcld_wpopenai_addons();
            return $qcld_wpopenai_addon->instance();
        
        }
    }
  
    //fire off the plugin
    qcld_openais();

}