<?php

/**
 * TTW base logic;
 * All plugin functions collected in one place and packaged in static calls.
 */
final class VAPPV_TTW_Core {

	/**
	 * ajax_pack_success
	 * Pack successfully called function JSON results
	 *
	 * @param mixed $ajax_call_results Results of the function call
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function ajax_pack_success( $ajax_call_results ): array {
		return array(
			'AJAXCallResults' => 'SUCCESS',
			'Data'            => $ajax_call_results
		);
	}

	/**
	 * ajax_pack_error
	 * Pack unsuccesfully called function JSON results
	 *
	 * @param string $custom_error_code Custom error code
	 * @param mixed $error_details Custom error details
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function ajax_pack_error( string $custom_error_code, $error_details ): array {
		return array(
			'AJAXCallResults' => 'ERROR',
			'TTWErrorCode'    => esc_html( $custom_error_code ),
			'ErrorDetails'    => $error_details
		);
	}

	/**
	 * Add new log entry
	 *
	 * @param string $log_entry_type Log entry type [ "info", "error" ]
	 * @param string $information Any information to save into logs
	 * @param array $extra_details_array Array with detailed information
	 *
	 * @return void
	 * @throws Exception
	 */
	public static function add_log_entry(
		string $log_entry_type,
		string $information,
		array $extra_details_array = null
	) {

		global $wpdb;

		$db_tables_prefix = $wpdb->prefix;
		$logs_tablename   = $db_tables_prefix . 'vappv_ttw_logs';

		$wpdb->insert(
			$logs_tablename,
			array(
				'log_date'      => gmdate( 'Y-m-d H:i:s' ),
				'log_type'      => $log_entry_type,
				'information'   => $information,
				'extra_details' => ( ! is_null( $extra_details_array ) ? wp_json_encode( $extra_details_array ) : null )
			)
		);

	}

	/**
	 * Create GUID text number
	 * @return string
	 * @throws Exception
	 */
	public static function create_guid(): string {
		if ( true === function_exists( 'com_create_guid' ) ) {
			return trim( com_create_guid(), '{}' );
		}
		return sprintf( '%04X%04X-%04X-%04X-%04X-%04X%04X%04X', wp_rand( 0, 65535 ), wp_rand( 0, 65535 ), wp_rand( 0, 65535 ), wp_rand( 16384, 20479 ), wp_rand( 32768, 49151 ), wp_rand( 0, 65535 ), wp_rand( 0, 65535 ), wp_rand( 0, 65535 ) );
	}

	/* --------------------------------------------------------------- */
	/*                        Public functions                         */
	/* --------------------------------------------------------------- */

	/**
	 * Send message to AI as user.
	 *
	 * @param string $user_name User name
	 * @param string $user_email User Email
	 * @param string $user_guid User GUID
	 * @param string $message_guid Message GUID
	 * @param string $message_text Message text
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function send_message_to_ai_as_user(
		string $user_name,
		string $user_email,
		string $user_guid,
		string $message_guid,
		string $message_text
	): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->send_message_to_ai_as_user(
				$user_name,
				$user_email,
				$user_guid,
				$message_guid,
				$message_text
			);

		}
		catch( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::send_message_to_ai_as_user]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Try to extract unanswered message responses as admin. Get results as JSON.
	 *
	 * @param string $user_guid User GUID
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function try_extract_message_responses_from_ai_as_user( string $user_guid ): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->try_extract_response_from_ai_as_user( $user_guid );

		}
		catch( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::try_extract_message_responses_from_ai_as_user]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * List messages as user
	 *
	 * @param string $user_guid User GUID
	 * @param string $data_direction Data direction { "backward" || "forward" }
	 * @param int $last_known_id ID of the last known message
	 * @param int $data_limit Data limit
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function list_all_chat_messages_as_user(
		string $user_guid,
		string $data_direction,
		int $last_known_id,
		int $data_limit
	): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->list_all_chat_messages_as_user(
				$user_guid,
				$data_direction,
				$last_known_id,
				$data_limit
			);

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::list_all_chat_messages_as_user]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Mark that user followed specified URL in specified message
	 *
	 * @param integer $message_id Message ID
	 * @param string $message_guid Message GUID
	 * @param string $followed_url Followed URL text
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function set_urls_followed_into_message(
		int $message_id,
		string $message_guid,
		string $followed_url
	): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->set_urls_followed_into_message(
				$message_id,
				$message_guid,
				$followed_url
			);

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::set_urls_followed_into_message]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/* --------------------------------------------------------------- */
	/*                        Admin functions                          */
	/* --------------------------------------------------------------- */

	/**
	 * Update API key to specified
	 * @param string $openai_api_address OpenAI API address
	 * @param string $api_key OpenAI API key
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function update_api_key( string $openai_api_address, string $api_key ): array {

		try {

			$ttw_openai_api_controller = new VAPPV_TTW_OpenAI_API_Connector( $openai_api_address, $api_key );
			return $ttw_openai_api_controller->update_api_key( $api_key );

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::update_api_key]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Get log entries from the DB. Get results as JSON.
	 *
	 * @param integer $offset Data offset
	 * @param integer $limit Data limit
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function get_log_entries( int $offset, int $limit ): array {

		try {

			if ( !is_numeric( $offset ) ) {
				$offset = 0;
			}
			if ( !is_numeric( $limit ) ) {
				$limit = 20;
			}

			global $wpdb;

			$db_tables_prefix = $wpdb->prefix;
			$logs_tablename = "{$db_tables_prefix}vappv_ttw_logs";

			$data_rows = $wpdb->get_results(
				$wpdb->prepare( 'SELECT * FROM %i ORDER BY `id` DESC LIMIT %d, %d', array(
					$logs_tablename,
					$offset,
					$limit
				) ),
				ARRAY_A
			);
			$stats_rows = $wpdb->get_results(
				$wpdb->prepare( 'SELECT COUNT(*) AS `items_total_count` FROM %i', array( $logs_tablename ) ),
				ARRAY_A
			);

			$results_logs_and_pagination = array(
				'data' => $data_rows,
				'pagination' => VAPPV_TTW_UI_Data_Paginator::paginate( $stats_rows[0]['items_total_count'], $offset, $limit )
			);

			return VAPPV_TTW_Core::ajax_pack_success( array(
				'Logs' => $results_logs_and_pagination
			) );

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::get_log_entries]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Update all plugin settings
	 *
	 * @param string $settings_chatgpt_model ChatGPT model gpt3, gpt4, etc.)
	 * @param float $settings_chatgpt_temperature ChatGPT's temperature from 0.1 to 1. Higher = more creativity. Lower = more precision.
	 * @param int $settings_max_characters_per_user_question Max characters per one user question
	 * @param int $settings_max_words_to_answer The maximum number of words the AI can use to respond to the user.
	 * @param string $settings_website_contacts_page Link to the website contacts page
	 * @param string $settings_any_direct_instructions Any direct instructions to the AI
	 * @param string $settings_primary_language Primary conversation language
	 * @param int $settings_humor_level AI humor level from 0% to 100%
	 * @param int $settings_try_intercept_leaving_users Try intercept leaving users (not used at the moment)
	 * @param string $settings_chat_bot_name AI name
	 * @param string $settings_robot_messages_post_scriptum Post scriptum after each AI message
	 * @param int $settings_dialogue_lifetime_minutes Dialogue lifetime, minutes
	 * @param int $settings_is_chatbot_displayed_right_now Is chatbot displayed at the moment
	 * @param string $settings_chat_bubble_image_url URL to the chat bubble image
	 * @param string $settings_website_contacts_company_email Company contact Email
	 * @param string $settings_website_contacts_company_phone Company contact phone
	 * @param string $settings_website_contacts_company_address Company address
	 * @param int $settings_max_input_words_per_day_by_all_users Max input words per day from all users (limit)
	 * @param int $settings_max_input_characters_per_day_by_all_users Max input characters per day from all users (limit)
	 * @param int $settings_max_output_characters_per_day_by_ai Max output characters per day by AI (limit)
	 * @param int $settings_max_output_tokens_per_day_by_ai Max output tokens per day by AI (limit)
	 * @param int $settings_sugar_limits_summary Summary limit (syntax sugar for settings page)
	 * @param string $settings_prohibited_topics_and_words Prohibited topics and words
	 * @param string $settings_plugin_appearance_custom_css Plugin custom CSS
	 * @param int $settings_show_promo_popup_after_n_seconds Show promo popup after N seconds
	 * @param int $settings_show_promo_popup_time_frequency_n_minutes Promo popup re-display frequency, minutes
	 * @param int $settings_dialogue_lifetime_max_messages Dialogue lifetime (messages count)
	 * @param string $settings_welcome_message_html AI welcome message text
	 * @param string $settings_store_db_on_deletion Is it necessary to keep plugin data on deletion / updating ({ "yes" / "no" })
	 * @param string $settings_show_powered_by_ttw_public_label Is it necessary to show "Powered by TalkToWebsite.com" label
	 * @param string $settings_chat_restricted_pages_urlmasks List of URL masks (substrings) to restrict chat bubble displaying
	 * @param string $settings_openai_api_address OpenAI endpoint API address (added for requests proxying)
	 * @param string $settings_chat_bubble_ui_position Chat bubble position (top left, top center, top right, bottom left, bottom center, bototm right)
	 * @param int $settings_chat_bubble_ui_margin Margin between chat bubble and the screen
	 * @param string $settings_ai_character
	 * @return array
	 * @throws Exception
	 * @throws Throwable
	 */
	public static function update_plugin_settings(
		string $settings_chatgpt_model,
		float $settings_chatgpt_temperature,
		int $settings_max_characters_per_user_question,
		int $settings_max_words_to_answer,
		string $settings_website_contacts_page,
		string $settings_any_direct_instructions,
		string $settings_primary_language,
		int $settings_humor_level,
		int $settings_try_intercept_leaving_users,
		string $settings_chat_bot_name,
		string $settings_robot_messages_post_scriptum,
		int $settings_dialogue_lifetime_minutes,
		int $settings_is_chatbot_displayed_right_now,
		string $settings_chat_bubble_image_url,
		string $settings_website_contacts_company_email,
		string $settings_website_contacts_company_phone,
		string $settings_website_contacts_company_address,
		int $settings_max_input_words_per_day_by_all_users,
		int $settings_max_input_characters_per_day_by_all_users,
		int $settings_max_output_characters_per_day_by_ai,
		int $settings_max_output_tokens_per_day_by_ai,
		int $settings_sugar_limits_summary,
		string $settings_prohibited_topics_and_words,
		string $settings_plugin_appearance_custom_css,
		int $settings_show_promo_popup_after_n_seconds,
		int $settings_show_promo_popup_time_frequency_n_minutes,
		int $settings_dialogue_lifetime_max_messages,
		string $settings_welcome_message_html,
		string $settings_store_db_on_deletion,
		string $settings_show_powered_by_ttw_public_label,
		string $settings_chat_restricted_pages_urlmasks,
		string $settings_openai_api_address,
		string $settings_chat_bubble_ui_position,
		int $settings_chat_bubble_ui_margin,
		string $settings_ai_character
	): array {

		try {

			$settings_controller = new VAPPV_TTW_Settings();
			return $settings_controller->update_all_plugin_settings(
				$settings_chatgpt_model,
				$settings_chatgpt_temperature,
				$settings_max_characters_per_user_question,
				$settings_max_words_to_answer,
				$settings_website_contacts_page,
				$settings_any_direct_instructions,
				$settings_primary_language,
				$settings_humor_level,
				$settings_try_intercept_leaving_users,
				$settings_chat_bot_name,
				$settings_robot_messages_post_scriptum,
				$settings_dialogue_lifetime_minutes,
				$settings_is_chatbot_displayed_right_now,
				$settings_chat_bubble_image_url,
				$settings_website_contacts_company_email,
				$settings_website_contacts_company_phone,
				$settings_website_contacts_company_address,
				$settings_max_input_words_per_day_by_all_users,
				$settings_max_input_characters_per_day_by_all_users,
				$settings_max_output_characters_per_day_by_ai,
				$settings_max_output_tokens_per_day_by_ai,
				$settings_sugar_limits_summary,
				$settings_prohibited_topics_and_words,
				$settings_plugin_appearance_custom_css,
				$settings_show_promo_popup_after_n_seconds,
				$settings_show_promo_popup_time_frequency_n_minutes,
				$settings_dialogue_lifetime_max_messages,
				$settings_welcome_message_html,
				$settings_store_db_on_deletion,
				$settings_show_powered_by_ttw_public_label,
				$settings_chat_restricted_pages_urlmasks,
				$settings_openai_api_address,
				$settings_chat_bubble_ui_position,
				$settings_chat_bubble_ui_margin,
				$settings_ai_character
			);

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::update_plugin_settings]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Check is plugin activated, initialized and active at the moment.
	 * @return array
	 * @throws Exception
	 */
	public static function is_plugin_active_right_now(): array {

		try {

			$settings_controller = new VAPPV_TTW_Settings();
			return $settings_controller->is_plugin_active_right_now();

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::is_plugin_active_right_now]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Get TTW training items (pages and posts)
	 * @return array
	 * @throws Exception
	 */
	public static function get_training_items(): array {

		try {

			$ttw_traning_items_controller = new VAPPV_TTW_Training();
			return $ttw_traning_items_controller->get_training_items();

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::get_training_items]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Train AI on the selected materials
	 *
	 * @param array $selected_materials Selected materials JSON data
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function train_ai( array $selected_materials ): array {

		try {

			$ttw_traning_items_controller = new VAPPV_TTW_Training();
			return $ttw_traning_items_controller->train_ai( $selected_materials );

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::train_ai]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Send message to AI as admin
	 *
	 * @param string $message_guid Message GUID
	 * @param string $message_text Message text
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function send_message_to_ai_as_admin( string $message_guid, string $message_text ): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->send_message_to_ai_as_admin( $message_guid, $message_text );

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::send_message_to_ai_as_admin]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * Try to extract unanswered message responses as admin
	 * @return array
	 * @throws Exception
	 */
	public static function try_extract_message_responses_from_ai_as_admin(): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->try_extract_response_from_ai_as_admin();

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::try_extract_message_responses_from_ai_as_admin]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * List messages list as admin
	 *
	 * @param string $data_direction Direction of the data extraction = { "forward" | "backward" }
	 * @param integer $last_known_id ID of the last known message
	 * @param integer $data_limit Data limit
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function list_testing_messages_as_admin(
		string $data_direction,
		int $last_known_id,
		int $data_limit
	): array {

		try {

			$ttw_chat_message_controller = new VAPPV_TTW_Chat_Messages();
			return $ttw_chat_message_controller->list_testing_messages_as_admin( $data_direction, $last_known_id, $data_limit );

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::list_testing_messages_as_admin]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * List dialogues as administrator. Get results as JSON.
	 *
	 * @param int $offset Data offset
	 * @param int $limit Data limit
	 *
	 * @return array
	 * @throws Exception
	 */
	public static function list_dialogues_as_admin( int $offset, int $limit ): array {

		try {

			$ttw_dialogues_controller = new VAPPV_TTW_Chat_Dialogues();
			return $ttw_dialogues_controller->list_dialogues_as_admin( $offset, $limit );

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::list_dialogues_as_admin]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * QuickAction: enable the plugin
	 * @return array
	 * @throws Exception
	 */
	public static function quick_action_enable_plugin(): array {

		try {

			$settings_controller = new VAPPV_TTW_Settings();
			return $settings_controller->quick_action_enable_plugin();

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::quick_action_enable_plugin]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * QuickAction: disable the plugin
	 * @return array
	 * @throws Exception
	 */
	public static function quick_action_disable_plugin(): array {

		try {

			$settings_controller = new VAPPV_TTW_Settings();
			return $settings_controller->quick_action_disable_plugin();

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::quick_action_disable_plugin]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}

	/**
	 * List OpenAI available models
	 * @return array
	 * @throws Exception
	 */
	public static function list_available_gpt_models(): array {

		try {

			return VAPPV_TTW_Common::list_available_gpt_models();

		}
		catch ( Throwable $t ) {

			VAPPV_TTW_Core::add_log_entry(
				'error',
				sprintf(
					'%1$s // %2$s // %3$s',
					esc_html( __( 'Critical error during plugin function call.', 'talk-to-website' ) ),
					'[VAPPV_TTW_Core::list_available_gpt_models]',
					esc_html( $t->getMessage() )
				)
			);

			return VAPPV_TTW_Core::ajax_pack_error(
				'CORE_CALL_ERROR',
				esc_html(
					__( 'Critical error when calling plugin functions. Please check plugin logs.', 'talk-to-website' )
				)
			);

		}

	}
}

/*
	© 2024 Copyright Vitalii A. Popov. All rights reserved.
*/

?>