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

function click_n_chat_get_ai_action_handler() {  
	
	if (!isset($_POST['security']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['security'])), 'ajax-call-nounce')) {
        wp_send_json_error('invalid nonce');
        wp_die();
    }
	
	global $wpdb;
	 
	$message = sanitize_textarea_field($_POST['message']);

	$click_n_chat_setting_chatgpt = get_option('click_n_chat_setting_chatgpt');
	$click_n_chat_setting_popup = get_option('click_n_chat_setting_popup');
	
	
	$reply_message = "";
	if($click_n_chat_setting_chatgpt->include_auto_reply == "1")
	{
		$click_n_chat_setting_autoreply = get_option('click_n_chat_setting_autoreply');
		
		$table_name = $wpdb->prefix . 'cnc_auto_reply';
		$autoreplies = $wpdb->get_results("SELECT * FROM $table_name");
		$response = (object)click_n_chat_calculate_similarities_ai($autoreplies, $message);
		
		if($response->query_similarity >= $click_n_chat_setting_autoreply->matching_percenage  || $response->keyword_similarity >= $click_n_chat_setting_autoreply->matching_percenage)
		{
			$reply_message = $response->reply;
		}
	}
	 
	if($reply_message == "")
	{
		$ai = new Click_N_Chat_AI_Provider($settings);
		$reply_message = $ai->reply($message);
	}
 
	wp_send_json([
        'reply' => $reply_message,
    ]);
}  

function click_n_chat_calculate_similarities_ai($array, $user_keyword) {
    $results = [];

    // Compare each query and keyword with the user_keyword
    foreach ($array as $item) {
        $query_similarity = 0;
        $keyword_similarity = 0;

        // Calculate similarity percentages
        similar_text(strtolower($item->query), strtolower($user_keyword), $query_similarity);
        similar_text(strtolower($item->keyword), strtolower($user_keyword), $keyword_similarity);

        // Add the results to the array
        $results[] = [
            'query' => $item->query,
            'keyword' => $item->keyword,
			'reply' => $item->reply,
            'query_similarity' => $query_similarity,
            'keyword_similarity' => $keyword_similarity
        ];
    }

    // Sort the results by query_similarity and keyword_similarity in descending order
    usort($results, function($a, $b) {
        // Compare by query_similarity first
        if ($a['query_similarity'] == $b['query_similarity']) {
            // If query_similarity is the same, compare by keyword_similarity
            return $b['keyword_similarity'] <=> $a['keyword_similarity'];
        }
        return $b['query_similarity'] <=> $a['query_similarity'];
    });

    return $results[0];
}		
 
add_action('wp_ajax_click_n_chat_get_ai_action', 'click_n_chat_get_ai_action_handler');  
add_action('wp_ajax_nopriv_click_n_chat_get_ai_action', 'click_n_chat_get_ai_action_handler'); // For non-logged in users  
