<?php // phpcs:ignore
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * Rendered HTML for the Algolia Agent Chat block.
 *
 * @package WLC_Algolia
 *
 * @param array    $attributes Block attributes.
 * @param string   $content    Block content.
 * @param WP_Block $block      Block object.
 */

( static function () use ( $attributes, $content, $block ) {

$wlc_algolia_block_wrapper_attributes = get_block_wrapper_attributes(); // phpcs:ignore

	// Agent ID is a UUID, so we need to preserve the full string.
	$wlc_algolia_agent_id_raw = isset( $attributes['agentId'] ) ? trim( (string) $attributes['agentId'] ) : '';
	$wlc_algolia_agent_id     = preg_match( '/^[a-f0-9\-]{36}$/i', $wlc_algolia_agent_id_raw ) ? $wlc_algolia_agent_id_raw : '';
	$wlc_algolia_placeholder  = isset( $attributes['placeholder'] ) ? sanitize_text_field( (string) $attributes['placeholder'] ) : 'Ask me anything...';
	$wlc_algolia_show_header  = ! empty( $attributes['showHeader'] );
	$wlc_algolia_header_text  = isset( $attributes['headerText'] ) ? sanitize_text_field( (string) $attributes['headerText'] ) : 'AI Assistant';
	$wlc_algolia_accent_color = isset( $attributes['accentColor'] ) ? sanitize_hex_color( (string) $attributes['accentColor'] ) : '#DB002F';
	$wlc_algolia_height       = isset( $attributes['height'] ) ? sanitize_text_field( (string) $attributes['height'] ) : '600px';

	// Get Algolia API keys.
	// According to Algolia docs, Agent Studio API uses Search API Key (not Write API Key).
	$wlc_algolia_general_settings = get_option( 'wlc_algolia_settings_general', array() );
	$wlc_algolia_app_id           = '';
	$wlc_algolia_search_api_key   = '';

	if ( is_array( $wlc_algolia_general_settings ) && isset( $wlc_algolia_general_settings['general'] ) ) {
		$wlc_algolia_app_id         = isset( $wlc_algolia_general_settings['general']['application_id'] ) ? sanitize_text_field( (string) $wlc_algolia_general_settings['general']['application_id'] ) : '';
		$wlc_algolia_search_api_key = isset( $wlc_algolia_general_settings['general']['search_api_key'] ) ? sanitize_text_field( (string) $wlc_algolia_general_settings['general']['search_api_key'] ) : '';
	}

	if ( empty( $wlc_algolia_agent_id ) || empty( $wlc_algolia_app_id ) || empty( $wlc_algolia_search_api_key ) ) {
		?>
	<div <?php echo $wlc_algolia_block_wrapper_attributes; // phpcs:ignore ?>>
		<div class="wp-block-wlc-algolia-agent-chat__error">
			<?php if ( empty( $wlc_algolia_agent_id ) ) : ?>
				<p><?php esc_html_e( 'Please configure Agent ID in the block settings.', 'wlc-search-integration-with-algolia' ); ?></p>
			<?php else : ?>
				<p><?php esc_html_e( 'Please configure Algolia API keys in the plugin settings.', 'wlc-search-integration-with-algolia' ); ?></p>
			<?php endif; ?>
		</div>
	</div>
		<?php
		return;
	}

	$wlc_algolia_config = array(
		'agentId'     => $wlc_algolia_agent_id,
		'appId'       => $wlc_algolia_app_id,
		'apiKey'      => $wlc_algolia_search_api_key,
		'placeholder' => $wlc_algolia_placeholder,
		'showHeader'  => $wlc_algolia_show_header,
		'headerText'  => $wlc_algolia_header_text,
		'accentColor' => $wlc_algolia_accent_color ? $wlc_algolia_accent_color : '#DB002F',
		'height'      => $wlc_algolia_height,
	);

	?>
<div <?php echo $wlc_algolia_block_wrapper_attributes; // phpcs:ignore ?>
	data-config="<?php echo esc_attr( wp_json_encode( $wlc_algolia_config ) ); ?>"
	style="
		--wlc-accent: <?php echo esc_attr( $wlc_algolia_accent_color ? $wlc_algolia_accent_color : '#DB002F' ); ?>;
		--wlc-chat-height: <?php echo esc_attr( $wlc_algolia_height ); ?>;
	"
>
	<div class="wp-block-wlc-algolia-agent-chat__container">
		<div class="wp-block-wlc-algolia-agent-chat__chat" data-role="chat-container"></div>
	</div>
</div>
	<?php
} )();
