<?php
/**
 * This file is reponsible for creating the chatbot interface.
 *
 * @package Webdigit
 */

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

use Orhanerday\OpenAi\OpenAi;

/**
 * Retrieve the active post ids.
 */
function wdgpt_get_active_post_ids() {
	$args = array(
		'post_type'      => 'any',
		'post_status'    => 'publish',
		'posts_per_page' => -1,
		'meta_query'     => array(
			array(
				'key'   => 'wdgpt_is_active',
				'value' => 'true',
			),
		),
		'fields'         => 'ids',
	);
	return get_posts( $args );
}

/**
 * Checks if the OpenAI API key is set.
 *
 * @return bool True if the API key is set, false otherwise.
 */
function wdgpt_is_api_key_set() {
	$api_key = get_option( 'wd_openai_api_key' );
	return ! empty( $api_key );
}

/**
 * Checks if the OpenAI API is down.
 *
 * @return bool True if the API is down, false otherwise.
 */
function wdgpt_is_api_down() {
	try {
		$openai_api_key = get_option( 'wd_openai_api_key', '' );
		if ( empty( $openai_api_key ) ) {
			return false;
		}
		$openai_client = new OpenAI( $openai_api_key );
		$response      = $openai_client->listModels();
		if ( $response === false ) {
			return false;
		}
		$models = json_decode( $response, true );
		// Verification if the OpenAI API is down.
		if ( isset( $models['error'] ) ) {
			$bad_gateway = strpos( strtolower( $models['error']['message'] ), 'bad gateway' );
			if ( $bad_gateway !== false ) {
				return true;
			}
		}
	} catch ( \Exception $e ) {
		// Log error but don't crash the page
		if ( WDGPT_DEBUG_MODE ) {
			WDGPT_Error_Logs::wdgpt_log_error( 'Error checking API status: ' . $e->getMessage(), 500, 'wdgpt_is_api_down' );
		}
		return false;
	}
	return false;
}

/**
 * Checks if the chatbot should be displayed.
 *
 * @return bool True if the chatbot should be displayed, false otherwise.
 */
function wdgpt_should_display() {
	$enable_bot                  = get_option( 'wdgpt_enable_chatbot', 'on' );
	$active_posts                = wdgpt_get_active_post_ids();
	$database_update             = new WDGPT_Database_Updater( wdgpt_chatbot()->get_version() );
	$has_pending_database_update = $database_update->should_disable_plugin();
	if ( WDGPT_DEBUG_MODE ) {
		WDGPT_Error_Logs::wdgpt_log_error( 'Enable bot : ' . $enable_bot, 200, 'wdgpt_should_display' );
		WDGPT_Error_Logs::wdgpt_log_error( 'Active posts : ' . count( $active_posts ) . ' (Must be greater than 0)', 200, 'wdgpt_should_display' );
		WDGPT_Error_Logs::wdgpt_log_error( 'Pending update : ' . $has_pending_database_update, 200, 'wdgpt_should_display' );
		try {
			$api_down = wdgpt_is_api_down();
			WDGPT_Error_Logs::wdgpt_log_error( 'OPENAI API down : ' . ( $api_down ? 'YES' : 'NO' ), 200, 'wdgpt_should_display' );
		} catch ( \Exception $e ) {
			WDGPT_Error_Logs::wdgpt_log_error( 'Error checking API status in should_display: ' . $e->getMessage(), 500, 'wdgpt_should_display' );
		}
		WDGPT_Error_Logs::wdgpt_log_error( 'allow_url_fopen enabled: ' . ( ini_get( 'allow_url_fopen' ) ? 'YES' : 'NO' ), 200, 'wdgpt_debug' );
	}

	// Safely check API status - if it fails, assume API is not down
	$is_api_down = false;
	try {
		$is_api_down = wdgpt_is_api_down();
	} catch ( \Exception $e ) {
		// If checking API status fails, don't block the chatbot
		if ( WDGPT_DEBUG_MODE ) {
			WDGPT_Error_Logs::wdgpt_log_error( 'Error in wdgpt_is_api_down: ' . $e->getMessage(), 500, 'wdgpt_should_display' );
		}
	}

	return 'on' === $enable_bot &&
			count( $active_posts ) > 0 &&
			wdgpt_is_api_key_set() &&
			! $has_pending_database_update &&
			! $is_api_down &&
			ini_get( 'allow_url_fopen' );
}

/**
 * Displays the chatbot footer.
 */
function wdgpt_display_chatbot_footer() {
	// Application du filtre d'accès (addon usage control)
	$can_display = apply_filters(
		'wdgpt_can_display_chatbot',
		array(
			'can_display' => true,
			'message'     => '',
		)
	);

	if ( ! $can_display['can_display'] ) {
		echo '<div class="wdgpt-chatbot-restricted">' . esc_html( $can_display['message'] ) . '</div>';
		return;
	}

	if ( wdgpt_should_display() ) {
		wdgpt_chatbot_ui();
	}
}

/**
 * Displays the chatbot inside the footer.
 * Special rule to add dummy messages if the chatbot is not displayed on the front page, such as a preview in the admin panel.
 *
 * @param bool $is_front True if the chatbot is displayed on the front page, false otherwise.
 *
 * @return void
 */
function wdgpt_chatbot_ui() {
	$chatbot_name    = '' === get_option( 'wdgpt_name', 'Pixel' ) ? 'Pixel' : get_option( 'wdgpt_name', 'Pixel' );
		$image_src   = '';
		$wdgpt_image = get_option( 'wdgpt_image_name' );
	if ( $wdgpt_image ) {
		$upload_dir = wp_upload_dir();
		$file_path  = $upload_dir['basedir'] . '/' . $wdgpt_image;
		if ( file_exists( $file_path ) ) {
			$image_src = $upload_dir['baseurl'] . '/' . $wdgpt_image;
		} else {
			$image_src = WD_CHATBOT_URL . '/img/SmartSearchWP-logo.png';
		}
	} else {
		$image_src = WD_CHATBOT_URL . '/img/SmartSearchWP-logo.png';
	}
	?>
		<div id="chat-circle" class="btn btn-raised <?php echo esc_attr( 'wdgpt-' . get_option( 'wdgpt_chat_position', 'bottom-right' ) ); ?>">
			<div id="chat-overlay"></div>
			<img id="pluginimg" src="<?php echo esc_attr( $image_src ); ?>"></img>
			<?php
			if ( get_option( 'wdgpt_enable_chatbot_bubble', 'on' ) === 'on' ) {
				$current_locale     = get_locale();
				$bubble_text_option = 'wdgpt_chat_bubble_typing_text_' . $current_locale;
				$bubble_text        = get_option( $bubble_text_option, __( 'Hello, may I help you?', 'webdigit-chatbot' ) );

				// Debug logging for locale and bubble text
				if ( WDGPT_DEBUG_MODE ) {
					WDGPT_Error_Logs::wdgpt_log_error( 'Bubble - Current locale: ' . $current_locale, 200, 'wdgpt_chatbot_ui' );
					WDGPT_Error_Logs::wdgpt_log_error( 'Bubble - Option name: ' . $bubble_text_option, 200, 'wdgpt_chatbot_ui' );
					WDGPT_Error_Logs::wdgpt_log_error( 'Bubble - Text value: ' . $bubble_text, 200, 'wdgpt_chatbot_ui' );
					WDGPT_Error_Logs::wdgpt_log_error( 'Bubble - Text empty: ' . ( empty( $bubble_text ) ? 'YES' : 'NO' ), 200, 'wdgpt_chatbot_ui' );
				}
				?>
				<div class="chat-bubble">
					<div class="text">
						<span class="typing"><?php echo esc_html( $bubble_text ); ?></span>
					</div>
				</div>
				<?php
			}
			?>
		</div>
		<div id="chatbot-container">
			<div id="chatbot-header">
				<img id="pluginimg" src="<?php echo esc_attr( $image_src ); ?>"></img>
				<div id="chatbot-title"><?php echo esc_attr( $chatbot_name ); ?></div>
				<div id="chatbot-resize"><i class="fa-solid fa-expand"></i></div>
				<div id="chatbot-reset"><i class="fa-solid fa-trash-can"></i></div>
				<div id="chatbot-close"><i class="fa fa-times" aria-hidden="true"></i></div>
			</div>
			<div id="chatbot-body">
				<div id="chatbot-messages">
					<?php
					$current_locale   = get_locale();
					$greetings_option = 'wdgpt_greetings_message_' . $current_locale;
					$greetings_text   = get_option( $greetings_option, __( 'Bonjour, je suis SmartSearchWP, comment puis-je vous aider ?', 'webdigit-chatbot' ) );

					// Debug logging for locale and greetings text
					if ( WDGPT_DEBUG_MODE ) {
						WDGPT_Error_Logs::wdgpt_log_error( 'Greetings - Current locale: ' . $current_locale, 200, 'wdgpt_chatbot_ui' );
						WDGPT_Error_Logs::wdgpt_log_error( 'Greetings - Option name: ' . $greetings_option, 200, 'wdgpt_chatbot_ui' );
						WDGPT_Error_Logs::wdgpt_log_error( 'Greetings - Text value: ' . $greetings_text, 200, 'wdgpt_chatbot_ui' );
						WDGPT_Error_Logs::wdgpt_log_error( 'Greetings - Text empty: ' . ( empty( $greetings_text ) ? 'YES' : 'NO' ), 200, 'wdgpt_chatbot_ui' );
					}
					?>
					<div class="chatbot-message assistant">
						<div>
							<img class="chatbot-message-img" src="<?php echo esc_attr( $image_src ); ?>" alt="Chatbot Image">
							<span class="response assistant"><?php echo esc_html( $greetings_text ); ?></span>
						</div>
					</div>
				</div>
				<div id="chatbot-input-container">
					<button id="wdgpt-speech-to-text" class="chatbot-btn-speech"><i class="fa fa-microphone" aria-hidden="true"></i></button>
					<input type="text" id="chatbot-input" placeholder="<?php esc_html_e( 'Tapez votre message ici...', 'webdigit-chatbot' ); ?>">
					<button id="chatbot-send" class="chatbot-btn-send"><i class="fa fa-paper-plane" aria-hidden="true"></i></button>
				</div>
				<div id="chatbot-disclaimer">
					<p><?php esc_html_e( 'Powered by artificial intelligence, the bot can make mistakes. Consider checking important information.', 'webdigit-chatbot' ); ?></p>
				</div>
			</div>
		</div>
		<?php
}
